app/soc/views/site/docs/edit.py
changeset 338 0d78f41dde9b
parent 329 2d90d49ce78a
child 342 72482d8e5b34
equal deleted inserted replaced
337:cb7d22b1e7d8 338:0d78f41dde9b
    30 
    30 
    31 from soc.logic import models
    31 from soc.logic import models
    32 from soc.logic import out_of_band
    32 from soc.logic import out_of_band
    33 from soc.logic import path_link_name
    33 from soc.logic import path_link_name
    34 from soc.logic.models import document
    34 from soc.logic.models import document
    35 from soc.logic.site import id_user
       
    36 
    35 
    37 from soc.views import helper
    36 from soc.views import helper
    38 from soc.views import simple
    37 from soc.views import simple
    39 from soc.views.helper import access
    38 from soc.views.helper import access
    40 from soc.views.user import profile
    39 from soc.views.user import profile
    45 import soc.views.helper.responses
    44 import soc.views.helper.responses
    46 import soc.views.helper.widgets
    45 import soc.views.helper.widgets
    47 import soc.views.out_of_band
    46 import soc.views.out_of_band
    48 
    47 
    49 
    48 
    50 class EditForm(helper.forms.DbModelForm):
       
    51   """Django form displayed when Developer edits a Document.
       
    52   """
       
    53   doc_key_name = forms.fields.CharField(widget=forms.HiddenInput)
       
    54   content = forms.fields.CharField(widget=helper.widgets.TinyMCE())
       
    55   
       
    56   class Meta:
       
    57     model = soc.models.document.Document
       
    58     
       
    59     #: list of model fields which will *not* be gathered by the form
       
    60     exclude = ['inheritance_line', 'user', 'created', 'modified']
       
    61  
       
    62   def clean_partial_path(self):
       
    63     partial_path = self.cleaned_data.get('partial_path')
       
    64     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
    65     return partial_path
       
    66 
       
    67   def clean_link_name(self):
       
    68     link_name = self.cleaned_data.get('link_name')
       
    69     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
    70     return link_name
       
    71 
       
    72 
       
    73 DEF_SITE_DOCS_EDIT_TMPL = 'soc/site/docs/edit.html'
       
    74 DEF_CREATE_NEW_DOC_MSG = ' You can create a new document by visiting the' \
    49 DEF_CREATE_NEW_DOC_MSG = ' You can create a new document by visiting the' \
    75                          ' <a href="/site/docs/edit">Create ' \
    50                          ' <a href="/site/docs/edit">Create ' \
    76                          'a New Document</a> page.'
    51                          'a New Document</a> page.'
    77 
    52 
    78 SUBMIT_MESSAGES = (
    53 SUBMIT_MESSAGES = (
    98   properties['link_name'] = link_name
    73   properties['link_name'] = link_name
    99   properties['title'] = form.cleaned_data.get('title')
    74   properties['title'] = form.cleaned_data.get('title')
   100   properties['short_name'] = form.cleaned_data.get('short_name')
    75   properties['short_name'] = form.cleaned_data.get('short_name')
   101   properties['abstract'] = form.cleaned_data.get('abstract')
    76   properties['abstract'] = form.cleaned_data.get('abstract')
   102   properties['content'] = form.cleaned_data.get('content')
    77   properties['content'] = form.cleaned_data.get('content')
   103   properties['user'] = models.user.logic.getFromFields(email=email)
    78   properties['founder'] = models.user.logic.getFromFields(email=email)
   104   properties['is_featured'] = form.cleaned_data.get('is_featured')
    79   properties['is_featured'] = form.cleaned_data.get('is_featured')
   105 
    80 
   106   doc = document.logic.updateOrCreateFromFields(properties,
    81   doc = document.logic.updateOrCreateFromFields(properties,
   107                                                 partial_path=partial_path,
    82                                                 partial_path=partial_path,
   108                                                 link_name=link_name)
    83                                                 link_name=link_name)
   109   return doc
    84   return doc
       
    85 
       
    86 
       
    87 class CreateForm(helper.forms.DbModelForm):
       
    88   """Django form displayed when Developer creates a Document.
       
    89   """
       
    90   content = forms.fields.CharField(widget=helper.widgets.TinyMCE(
       
    91       attrs={'rows':10, 'cols':40}))
       
    92 
       
    93   class Meta:
       
    94     model = soc.models.document.Document
       
    95 
       
    96     #: list of model fields which will *not* be gathered by the form
       
    97     exclude = ['inheritance_line', 'founder', 'created', 'modified']
       
    98 
       
    99   def clean_partial_path(self):
       
   100     partial_path = self.cleaned_data.get('partial_path')
       
   101     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
   102     return partial_path
       
   103 
       
   104   def clean_link_name(self):
       
   105     link_name = self.cleaned_data.get('link_name')
       
   106     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
   107     return link_name
       
   108 
       
   109 
       
   110 DEF_SITE_DOCS_CREATE_TMPL = 'soc/site/docs/edit.html'
       
   111 
       
   112 def create(request, template=DEF_SITE_DOCS_CREATE_TMPL):
       
   113   """View for a Developer to create a new Document entity.
       
   114 
       
   115   Args:
       
   116     request: the standard django request object
       
   117     template: the "sibling" template (or a search list of such templates)
       
   118       from which to construct the public.html template name (or names)
       
   119 
       
   120   Returns:
       
   121     A subclass of django.http.HttpResponse which either contains the form to
       
   122     be filled out, or a redirect to the correct view in the interface.
       
   123   """
       
   124 
       
   125   try:
       
   126     access.checkIsDeveloper(request)
       
   127   except  soc.views.out_of_band.AccessViolationResponse, alt_response:
       
   128     return alt_response.response()
       
   129 
       
   130   # create default template context for use with any templates
       
   131   context = helper.responses.getUniversalContext(request)
       
   132 
       
   133   if request.method == 'POST':
       
   134     form = CreateForm(request.POST)
       
   135 
       
   136     if form.is_valid():
       
   137       doc = getDocForForm(form)
       
   138 
       
   139       if not doc:
       
   140         return http.HttpResponseRedirect('/')
       
   141 
       
   142       new_path = path_link_name.combinePath([doc.partial_path, doc.link_name])
       
   143 
       
   144       # redirect to new /site/docs/edit/new_path?s=0
       
   145       # (causes 'Profile saved' message to be displayed)
       
   146       return helper.responses.redirectToChangedSuffix(
       
   147           request, None, new_path,
       
   148           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
       
   149   else: # method == 'GET':
       
   150     # no link name specified, so start with an empty form
       
   151     form = CreateForm()
       
   152 
       
   153   context['form'] = form
       
   154 
       
   155   return helper.responses.respond(request, template, context)
       
   156 
       
   157 
       
   158 DEF_SITE_DOCS_EDIT_TMPL = 'soc/site/docs/edit.html'
       
   159 
       
   160 class EditForm(CreateForm):
       
   161   """Django form displayed when Developer edits a Document.
       
   162   """
       
   163   doc_key_name = forms.fields.CharField(widget=forms.HiddenInput)
       
   164   created_by = forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(),
       
   165                                       required=False)
   110 
   166 
   111 
   167 
   112 def edit(request, partial_path=None, link_name=None,
   168 def edit(request, partial_path=None, link_name=None,
   113          template=DEF_SITE_DOCS_EDIT_TMPL):
   169          template=DEF_SITE_DOCS_EDIT_TMPL):
   114   """View for a Developer to modify the properties of a Document Model entity.
   170   """View for a Developer to modify the properties of a Document Model entity.
   132   except  soc.views.out_of_band.AccessViolationResponse, alt_response:
   188   except  soc.views.out_of_band.AccessViolationResponse, alt_response:
   133     return alt_response.response()
   189     return alt_response.response()
   134 
   190 
   135 # create default template context for use with any templates
   191 # create default template context for use with any templates
   136   context = helper.responses.getUniversalContext(request)
   192   context = helper.responses.getUniversalContext(request)
   137   logged_in_id = users.get_current_user()
       
   138 
   193 
   139   doc = None  # assume that no Document entity will be found
   194   doc = None  # assume that no Document entity will be found
   140 
   195 
   141   path = path_link_name.combinePath([partial_path, link_name])
   196   path = path_link_name.combinePath([partial_path, link_name])
   142 
   197 
   182         context['notice'] = (
   237         context['notice'] = (
   183             helper.requests.getSingleIndexedParamValue(
   238             helper.requests.getSingleIndexedParamValue(
   184                 request, profile.SUBMIT_MSG_PARAM_NAME,
   239                 request, profile.SUBMIT_MSG_PARAM_NAME,
   185                 values=SUBMIT_MESSAGES))
   240                 values=SUBMIT_MESSAGES))
   186 
   241 
   187         # populate form with the existing User entity
   242         # populate form with the existing Document entity
       
   243         founder_link_name = doc.founder.link_name
   188         form = EditForm(initial={'doc_key_name': doc.key().name(),
   244         form = EditForm(initial={'doc_key_name': doc.key().name(),
   189             'title': doc.title, 'partial_path': doc.partial_path,
   245             'title': doc.title, 'partial_path': doc.partial_path,
   190             'link_name': doc.link_name, 'short_name': doc.short_name,
   246             'link_name': doc.link_name, 'short_name': doc.short_name,
   191             'abstract': doc.abstract, 'content': doc.content,
   247             'abstract': doc.abstract, 'content': doc.content,
   192             'user': doc.user, 'is_featured': doc.is_featured})       
   248             'founder': doc.founder, 'is_featured': doc.is_featured,
       
   249             'created_by': founder_link_name})       
   193       else:
   250       else:
   194         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   251         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   195           # redirect to aggressively remove 'Profile saved' query parameter
   252           # redirect to aggressively remove 'Profile saved' query parameter
   196           return http.HttpResponseRedirect(request.path)
   253           return http.HttpResponseRedirect(request.path)
   197           
   254           
   210                   'existing_doc': doc})
   267                   'existing_doc': doc})
   211 
   268 
   212   return helper.responses.respond(request, template, context)
   269   return helper.responses.respond(request, template, context)
   213 
   270 
   214 
   271 
   215 class CreateForm(helper.forms.DbModelForm):
       
   216   """Django form displayed when Developer creates a Document.
       
   217   """
       
   218   content = forms.fields.CharField(widget=helper.widgets.TinyMCE())
       
   219   
       
   220   class Meta:
       
   221     model = soc.models.document.Document
       
   222     
       
   223     #: list of model fields which will *not* be gathered by the form
       
   224     exclude = ['inheritance_line', 'user', 'created', 'modified']
       
   225  
       
   226   def clean_partial_path(self):
       
   227     partial_path = self.cleaned_data.get('partial_path')
       
   228     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
   229     return partial_path
       
   230 
       
   231   def clean_link_name(self):
       
   232     link_name = self.cleaned_data.get('link_name')
       
   233     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
   234     return link_name
       
   235 
       
   236 
       
   237 DEF_SITE_DOCS_CREATE_TMPL = 'soc/site/docs/edit.html'
       
   238 
       
   239 def create(request, template=DEF_SITE_DOCS_CREATE_TMPL):
       
   240   """View for a Developer to create a new Document entity.
       
   241 
       
   242   Args:
       
   243     request: the standard django request object
       
   244     template: the "sibling" template (or a search list of such templates)
       
   245       from which to construct the public.html template name (or names)
       
   246 
       
   247   Returns:
       
   248     A subclass of django.http.HttpResponse which either contains the form to
       
   249     be filled out, or a redirect to the correct view in the interface.
       
   250   """
       
   251 
       
   252   try:
       
   253     access.checkIsDeveloper(request)
       
   254   except  soc.views.out_of_band.AccessViolationResponse, alt_response:
       
   255     return alt_response.response()
       
   256 
       
   257   # create default template context for use with any templates
       
   258   context = helper.responses.getUniversalContext(request)
       
   259   logged_in_id = users.get_current_user()
       
   260 
       
   261   if request.method == 'POST':
       
   262     form = CreateForm(request.POST)
       
   263 
       
   264     if form.is_valid():
       
   265       doc = getDocForForm(form)
       
   266 
       
   267       if not doc:
       
   268         return http.HttpResponseRedirect('/')
       
   269 
       
   270       new_path = path_link_name.combinePath([doc.partial_path, doc.link_name])
       
   271         
       
   272       # redirect to new /site/docs/edit/new_path?s=0
       
   273       # (causes 'Profile saved' message to be displayed)
       
   274       return helper.responses.redirectToChangedSuffix(
       
   275           request, None, new_path,
       
   276           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
       
   277   else: # method == 'GET':
       
   278     # no link name specified, so start with an empty form
       
   279     form = CreateForm()
       
   280 
       
   281   context['form'] = form
       
   282 
       
   283   return helper.responses.respond(request, template, context)
       
   284 
       
   285 
       
   286 def delete(request, partial_path=None, link_name=None,
   272 def delete(request, partial_path=None, link_name=None,
   287            template=DEF_SITE_DOCS_EDIT_TMPL):
   273            template=DEF_SITE_DOCS_EDIT_TMPL):
   288   """Request handler for a Developer to delete Document Model entity.
   274   """Request handler for a Developer to delete Document Model entity.
   289 
   275 
   290   Args:
   276   Args: