app/soc/views/settings.py
changeset 513 3c1e16637ad7
parent 512 aae25d2b4464
equal deleted inserted replaced
512:aae25d2b4464 513:3c1e16637ad7
    83 
    83 
    84 class DocSelectForm(helper.forms.BaseForm):
    84 class DocSelectForm(helper.forms.BaseForm):
    85   """Django form displayed to select a Document.
    85   """Django form displayed to select a Document.
    86   """
    86   """
    87 
    87 
    88   # TODO(tlarsen): partial_path will be a hard-coded read-only
    88   # TODO(tlarsen): scope_path will be a hard-coded read-only
    89   #   field for some (most?) User Roles
    89   #   field for some (most?) User Roles
    90   doc_partial_path = forms.CharField(required=False,
    90   doc_scope_path = forms.CharField(required=False,
    91       label=soc.models.work.Work.partial_path.verbose_name,
    91       label=soc.models.work.Work.scope_path.verbose_name,
    92       help_text=soc.models.work.Work.partial_path.help_text)
    92       help_text=soc.models.work.Work.scope_path.help_text)
    93 
    93 
    94   # TODO(tlarsen): actually, using these two text fields to specify
    94   # TODO(tlarsen): actually, using these two text fields to specify
    95   #   the Document is pretty cheesy; this needs to be some much better
    95   #   the Document is pretty cheesy; this needs to be some much better
    96   #   Role-scoped Document selector that we don't have yet
    96   #   Role-scoped Document selector that we don't have yet
    97   doc_link_id = forms.CharField(required=False,
    97   doc_link_id = forms.CharField(required=False,
   103 
   103 
   104 
   104 
   105 DEF_HOME_EDIT_TMPL = 'soc/site_settings/edit.html'
   105 DEF_HOME_EDIT_TMPL = 'soc/site_settings/edit.html'
   106 
   106 
   107 @decorators.view
   107 @decorators.view
   108 def edit(request, page_name=None, partial_path=None, link_id=None, 
   108 def edit(request, page_name=None, scope_path=None, link_id=None, 
   109          logic=models.home_settings.logic,
   109          logic=models.home_settings.logic,
   110          settings_form_class=SettingsForm,
   110          settings_form_class=SettingsForm,
   111          template=DEF_HOME_EDIT_TMPL):
   111          template=DEF_HOME_EDIT_TMPL):
   112   """View for authorized User to edit contents of a home page.
   112   """View for authorized User to edit contents of a home page.
   113 
   113 
   148       # Ask for all the fields and pull them out 
   148       # Ask for all the fields and pull them out 
   149       for field in settings_form.cleaned_data:
   149       for field in settings_form.cleaned_data:
   150         value = settings_form.cleaned_data.get(field)
   150         value = settings_form.cleaned_data.get(field)
   151         fields[field] = value
   151         fields[field] = value
   152 
   152 
   153       doc_partial_path = doc_select_form.cleaned_data.get('doc_partial_path')
   153       doc_scope_path = doc_select_form.cleaned_data.get('doc_scope_path')
   154       doc_link_id = doc_select_form.cleaned_data.get('doc_link_id')
   154       doc_link_id = doc_select_form.cleaned_data.get('doc_link_id')
   155 
   155 
   156       home_doc = document.logic.getFromFields(
   156       home_doc = document.logic.getFromFields(
   157           partial_path=doc_partial_path, link_id=doc_link_id)
   157           scope_path=doc_scope_path, link_id=doc_link_id)
   158 
   158 
   159       if home_doc:
   159       if home_doc:
   160         fields['home'] = home_doc
   160         fields['home'] = home_doc
   161         context['notice'] = ugettext_lazy('Settings saved.')
   161         context['notice'] = ugettext_lazy('Settings saved.')
   162       else:
   162       else:
   169       
   169       
   170       if settings.home:
   170       if settings.home:
   171         home_doc = settings.home
   171         home_doc = settings.home
   172   else: # request.method == 'GET'
   172   else: # request.method == 'GET'
   173     # try to fetch HomeSettings entity by unique key_name
   173     # try to fetch HomeSettings entity by unique key_name
   174     settings = logic.getFromFields(partial_path=partial_path, 
   174     settings = logic.getFromFields(scope_path=scope_path, 
   175                                    link_id=link_id)
   175                                    link_id=link_id)
   176 
   176 
   177     if settings:
   177     if settings:
   178       # populate form with the existing HomeSettings entity
   178       # populate form with the existing HomeSettings entity
   179       settings_form = settings_form_class(instance=settings)
   179       settings_form = settings_form_class(instance=settings)
   184       except db.Error:
   184       except db.Error:
   185         pass
   185         pass
   186     
   186     
   187       if home_doc:
   187       if home_doc:
   188         doc_select_form = DocSelectForm(initial={
   188         doc_select_form = DocSelectForm(initial={
   189             'doc_partial_path': home_doc.partial_path,
   189             'doc_scope_path': home_doc.scope_path,
   190             'doc_link_id': home_doc.link_id})
   190             'doc_link_id': home_doc.link_id})
   191       else:
   191       else:
   192         doc_select_form = DocSelectForm()
   192         doc_select_form = DocSelectForm()
   193     else:
   193     else:
   194       # no SiteSettings entity exists for this key_name, so show a blank form
   194       # no SiteSettings entity exists for this key_name, so show a blank form