app/soc/views/document/edit.py
changeset 512 aae25d2b4464
parent 500 44ea4620c5c0
child 513 3c1e16637ad7
equal deleted inserted replaced
511:52557918ec8f 512:aae25d2b4464
    61   """
    61   """
    62 
    62 
    63   user = users.get_current_user()
    63   user = users.get_current_user()
    64 
    64 
    65   partial_path = form.cleaned_data.get('partial_path')
    65   partial_path = form.cleaned_data.get('partial_path')
    66   link_name = form.cleaned_data.get('link_name')
    66   link_id = form.cleaned_data.get('link_id')
    67 
    67 
    68   properties = {}
    68   properties = {}
    69   properties['partial_path'] = partial_path
    69   properties['partial_path'] = partial_path
    70   properties['link_name'] = link_name
    70   properties['link_id'] = link_id
    71   properties['title'] = form.cleaned_data.get('title')
    71   properties['title'] = form.cleaned_data.get('title')
    72   properties['short_name'] = form.cleaned_data.get('short_name')
    72   properties['short_name'] = form.cleaned_data.get('short_name')
    73   properties['content'] = form.cleaned_data.get('content')
    73   properties['content'] = form.cleaned_data.get('content')
    74   properties['author'] = models.user.logic.getForFields({'id': user}, unique=True)
    74   properties['author'] = models.user.logic.getForFields({'id': user}, unique=True)
    75   properties['is_featured'] = form.cleaned_data.get('is_featured')
    75   properties['is_featured'] = form.cleaned_data.get('is_featured')
    92     #: list of model fields which will *not* be gathered by the form
    92     #: list of model fields which will *not* be gathered by the form
    93     exclude = ['inheritance_line', 'author', 'created', 'modified']
    93     exclude = ['inheritance_line', 'author', 'created', 'modified']
    94 
    94 
    95   def clean_partial_path(self):
    95   def clean_partial_path(self):
    96     partial_path = self.cleaned_data.get('partial_path')
    96     partial_path = self.cleaned_data.get('partial_path')
    97     # TODO(tlarsen): combine path and link_name and check for uniqueness
    97     # TODO(tlarsen): combine path and link_id and check for uniqueness
    98     return partial_path
    98     return partial_path
    99 
    99 
   100   def clean_link_name(self):
   100   def clean_link_id(self):
   101     link_name = self.cleaned_data.get('link_name')
   101     link_id = self.cleaned_data.get('link_id')
   102     # TODO(tlarsen): combine path and link_name and check for uniqueness
   102     # TODO(tlarsen): combine path and link_id and check for uniqueness
   103     return link_name
   103     return link_id
   104 
   104 
   105 
   105 
   106 DEF_DOCS_CREATE_TMPL = 'soc/models/edit.html'
   106 DEF_DOCS_CREATE_TMPL = 'soc/models/edit.html'
   107 
   107 
   108 @decorators.view
   108 @decorators.view
   138       doc = getDocForForm(form)
   138       doc = getDocForForm(form)
   139 
   139 
   140       if not doc:
   140       if not doc:
   141         return http.HttpResponseRedirect('/')
   141         return http.HttpResponseRedirect('/')
   142 
   142 
   143       new_path = path_link_name.combinePath([doc.partial_path, doc.link_name])
   143       new_path = path_link_name.combinePath([doc.partial_path, doc.link_id])
   144 
   144 
   145       # redirect to new /document/edit/new_path?s=0
   145       # redirect to new /document/edit/new_path?s=0
   146       # (causes 'Profile saved' message to be displayed)
   146       # (causes 'Profile saved' message to be displayed)
   147       return helper.responses.redirectToChangedSuffix(
   147       return helper.responses.redirectToChangedSuffix(
   148           request, None, new_path,
   148           request, None, new_path,
   149           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
   149           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
   150   else: # method == 'GET':
   150   else: # method == 'GET':
   151     # no link name specified, so start with an empty form
   151     # no link ID specified, so start with an empty form
   152     form = CreateForm()
   152     form = CreateForm()
   153 
   153 
   154   context['form'] = form
   154   context['form'] = form
   155 
   155 
   156   return helper.responses.respond(request, template, context)
   156   return helper.responses.respond(request, template, context)
   165   created_by = forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(),
   165   created_by = forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(),
   166                                       required=False)
   166                                       required=False)
   167 
   167 
   168 
   168 
   169 @decorators.view
   169 @decorators.view
   170 def edit(request, page_name=None, partial_path=None, link_name=None,
   170 def edit(request, page_name=None, partial_path=None, link_id=None,
   171          template=DEF_DOCS_EDIT_TMPL):
   171          template=DEF_DOCS_EDIT_TMPL):
   172   """View to modify the properties of a Document Model entity.
   172   """View to modify the properties of a Document Model entity.
   173 
   173 
   174   Args:
   174   Args:
   175     request: the standard django request object
   175     request: the standard django request object
   176     page_name: the page name displayed in templates as page and header title
   176     page_name: the page name displayed in templates as page and header title
   177     partial_path: the Document's site-unique "path" extracted from the URL,
   177     partial_path: the Document's site-unique "path" extracted from the URL,
   178       minus the trailing link_name
   178       minus the trailing link_id
   179     link_name: the last portion of the Document's site-unique "path"
   179     link_id: the last portion of the Document's site-unique "path"
   180       extracted from the URL
   180       extracted from the URL
   181     template: the "sibling" template (or a search list of such templates)
   181     template: the "sibling" template (or a search list of such templates)
   182       from which to construct the public.html template name (or names)
   182       from which to construct the public.html template name (or names)
   183 
   183 
   184   Returns:
   184   Returns:
   197   context = helper.responses.getUniversalContext(request)
   197   context = helper.responses.getUniversalContext(request)
   198   context['page_name'] = page_name
   198   context['page_name'] = page_name
   199 
   199 
   200   doc = None  # assume that no Document entity will be found
   200   doc = None  # assume that no Document entity will be found
   201 
   201 
   202   path = path_link_name.combinePath([partial_path, link_name])
   202   path = path_link_name.combinePath([partial_path, link_id])
   203 
   203 
   204   # try to fetch Document entity corresponding to path if one exists    
   204   # try to fetch Document entity corresponding to path if one exists    
   205   try:
   205   try:
   206     if path:
   206     if path:
   207       doc = document.logic.getFromFields(partial_path=partial_path,
   207       doc = document.logic.getFromFields(partial_path=partial_path,
   208                                          link_name=link_name)
   208                                          link_id=link_id)
   209   except out_of_band.ErrorResponse, error:
   209   except out_of_band.ErrorResponse, error:
   210     # show custom 404 page when path doesn't exist in Datastore
   210     # show custom 404 page when path doesn't exist in Datastore
   211     error.message = error.message + DEF_CREATE_NEW_DOC_MSG
   211     error.message = error.message + DEF_CREATE_NEW_DOC_MSG
   212     return simple.errorResponse(request, page_name, error, template, context)
   212     return simple.errorResponse(request, page_name, error, template, context)
   213 
   213 
   218       doc = getDocForForm(form)
   218       doc = getDocForForm(form)
   219       
   219       
   220       if not doc:
   220       if not doc:
   221         return http.HttpResponseRedirect('/')
   221         return http.HttpResponseRedirect('/')
   222 
   222 
   223       new_path = path_link_name.combinePath([doc.partial_path, doc.link_name])
   223       new_path = path_link_name.combinePath([doc.partial_path, doc.link_id])
   224         
   224         
   225       # redirect to new /document/edit/new_path?s=0
   225       # redirect to new /document/edit/new_path?s=0
   226       # (causes 'Profile saved' message to be displayed)
   226       # (causes 'Profile saved' message to be displayed)
   227       return helper.responses.redirectToChangedSuffix(
   227       return helper.responses.redirectToChangedSuffix(
   228           request, path, new_path,
   228           request, path, new_path,
   244             helper.requests.getSingleIndexedParamValue(
   244             helper.requests.getSingleIndexedParamValue(
   245                 request, profile.SUBMIT_MSG_PARAM_NAME,
   245                 request, profile.SUBMIT_MSG_PARAM_NAME,
   246                 values=SUBMIT_MESSAGES))
   246                 values=SUBMIT_MESSAGES))
   247 
   247 
   248         # populate form with the existing Document entity
   248         # populate form with the existing Document entity
   249         author_link_name = doc.author.link_name
   249         author_link_id = doc.author.link_id
   250         form = EditForm(initial={'doc_key_name': doc.key().name(),
   250         form = EditForm(initial={'doc_key_name': doc.key().name(),
   251             'title': doc.title, 'partial_path': doc.partial_path,
   251             'title': doc.title, 'partial_path': doc.partial_path,
   252             'link_name': doc.link_name, 'short_name': doc.short_name,
   252             'link_id': doc.link_id, 'short_name': doc.short_name,
   253             'content': doc.content, 'author': doc.author,
   253             'content': doc.content, 'author': doc.author,
   254             'is_featured': doc.is_featured, 'created_by': author_link_name})
   254             'is_featured': doc.is_featured, 'created_by': author_link_id})
   255       else:
   255       else:
   256         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   256         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   257           # redirect to aggressively remove 'Profile saved' query parameter
   257           # redirect to aggressively remove 'Profile saved' query parameter
   258           return http.HttpResponseRedirect(request.path)
   258           return http.HttpResponseRedirect(request.path)
   259           
   259           
   260         context['lookup_error'] = ugettext_lazy(
   260         context['lookup_error'] = ugettext_lazy(
   261             'Document with that path not found.')
   261             'Document with that path not found.')
   262         form = EditForm(initial={'link_name': link_name})
   262         form = EditForm(initial={'link_id': link_id})
   263     else:  # no link name specified in the URL
   263     else:  # no link ID specified in the URL
   264       if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   264       if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   265         # redirect to aggressively remove 'Profile saved' query parameter
   265         # redirect to aggressively remove 'Profile saved' query parameter
   266         return http.HttpResponseRedirect(request.path)
   266         return http.HttpResponseRedirect(request.path)
   267 
   267 
   268       # no link name specified, so start with an empty form
   268       # no link ID specified, so start with an empty form
   269       form = EditForm()
   269       form = EditForm()
   270 
   270 
   271   context.update({'form': form,
   271   context.update({'form': form,
   272                   'entity': doc})
   272                   'entity': doc})
   273 
   273 
   274   return helper.responses.respond(request, template, context)
   274   return helper.responses.respond(request, template, context)
   275 
   275 
   276 
   276 
   277 @decorators.view
   277 @decorators.view
   278 def delete(request, page_name=None, partial_path=None, link_name=None,
   278 def delete(request, page_name=None, partial_path=None, link_id=None,
   279            template=DEF_DOCS_EDIT_TMPL):
   279            template=DEF_DOCS_EDIT_TMPL):
   280   """Request handler to delete Document Model entity.
   280   """Request handler to delete Document Model entity.
   281 
   281 
   282   Args:
   282   Args:
   283     request: the standard django request object
   283     request: the standard django request object
   284     page_name: the page name displayed in templates as page and header title
   284     page_name: the page name displayed in templates as page and header title
   285     partial_path: the Document's site-unique "path" extracted from the URL,
   285     partial_path: the Document's site-unique "path" extracted from the URL,
   286       minus the trailing link_name
   286       minus the trailing link_id
   287     link_name: the last portion of the Document's site-unique "path"
   287     link_id: the last portion of the Document's site-unique "path"
   288       extracted from the URL
   288       extracted from the URL
   289     template: the "sibling" template (or a search list of such templates)
   289     template: the "sibling" template (or a search list of such templates)
   290       from which to construct the public.html template name (or names)
   290       from which to construct the public.html template name (or names)
   291 
   291 
   292   Returns:
   292   Returns:
   304   # create default template context for use with any templates
   304   # create default template context for use with any templates
   305   context = helper.responses.getUniversalContext(request)
   305   context = helper.responses.getUniversalContext(request)
   306   context['page_name'] = page_name
   306   context['page_name'] = page_name
   307 
   307 
   308   existing_doc = None
   308   existing_doc = None
   309   path = path_link_name.combinePath([partial_path, link_name])
   309   path = path_link_name.combinePath([partial_path, link_id])
   310 
   310 
   311   # try to fetch Document entity corresponding to path if one exists    
   311   # try to fetch Document entity corresponding to path if one exists    
   312   try:
   312   try:
   313     if path:
   313     if path:
   314       existing_doc = document.logic.getFromFields(partial_path=partial_path,
   314       existing_doc = document.logic.getFromFields(partial_path=partial_path,
   315                                                   link_name=link_name)
   315                                                   link_id=link_id)
   316   except out_of_band.ErrorResponse, error:
   316   except out_of_band.ErrorResponse, error:
   317     # show custom 404 page when path doesn't exist in Datastore
   317     # show custom 404 page when path doesn't exist in Datastore
   318     error.message = error.message + DEF_CREATE_NEW_DOC_MSG
   318     error.message = error.message + DEF_CREATE_NEW_DOC_MSG
   319     return simple.errorResponse(request, page_name, error, template, context)
   319     return simple.errorResponse(request, page_name, error, template, context)
   320 
   320