app/soc/views/document/edit.py
changeset 513 3c1e16637ad7
parent 512 aae25d2b4464
equal deleted inserted replaced
512:aae25d2b4464 513:3c1e16637ad7
    60   """Extracts doc fields from a form and creates a new doc from it.
    60   """Extracts doc fields from a form and creates a new doc from it.
    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   scope_path = form.cleaned_data.get('scope_path')
    66   link_id = form.cleaned_data.get('link_id')
    66   link_id = form.cleaned_data.get('link_id')
    67 
    67 
    68   properties = {}
    68   properties = {}
    69   properties['partial_path'] = partial_path
    69   properties['scope_path'] = scope_path
    70   properties['link_id'] = link_id
    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)
    90     model = soc.models.document.Document
    90     model = soc.models.document.Document
    91 
    91 
    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_scope_path(self):
    96     partial_path = self.cleaned_data.get('partial_path')
    96     scope_path = self.cleaned_data.get('scope_path')
    97     # TODO(tlarsen): combine path and link_id and check for uniqueness
    97     # TODO(tlarsen): combine path and link_id and check for uniqueness
    98     return partial_path
    98     return scope_path
    99 
    99 
   100   def clean_link_id(self):
   100   def clean_link_id(self):
   101     link_id = self.cleaned_data.get('link_id')
   101     link_id = self.cleaned_data.get('link_id')
   102     # TODO(tlarsen): combine path and link_id and check for uniqueness
   102     # TODO(tlarsen): combine path and link_id and check for uniqueness
   103     return link_id
   103     return link_id
   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_id])
   143       new_path = path_link_name.combinePath([doc.scope_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,
   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_id=None,
   170 def edit(request, page_name=None, scope_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     scope_path: the Document's site-unique "path" extracted from the URL,
   178       minus the trailing link_id
   178       minus the trailing link_id
   179     link_id: 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)
   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_id])
   202   path = path_link_name.combinePath([scope_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(scope_path=scope_path,
   208                                          link_id=link_id)
   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)
   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_id])
   223       new_path = path_link_name.combinePath([doc.scope_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,
   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_id = doc.author.link_id
   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, 'scope_path': doc.scope_path,
   252             'link_id': doc.link_id, '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_id})
   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):
   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_id=None,
   278 def delete(request, page_name=None, scope_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     scope_path: the Document's site-unique "path" extracted from the URL,
   286       minus the trailing link_id
   286       minus the trailing link_id
   287     link_id: 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)
   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_id])
   309   path = path_link_name.combinePath([scope_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(scope_path=scope_path,
   315                                                   link_id=link_id)
   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)