app/soc/views/site/home.py
changeset 206 832335761384
parent 164 afdf502c6cc4
child 225 2590d6b83568
equal deleted inserted replaced
205:4a86df751222 206:832335761384
    57     """
    57     """
    58     #: db.Model subclass for which the form will gather information
    58     #: db.Model subclass for which the form will gather information
    59     model = soc.models.document.Document
    59     model = soc.models.document.Document
    60     
    60     
    61     #: list of model fields which will *not* be gathered by the form
    61     #: list of model fields which will *not* be gathered by the form
    62     exclude = ['user','modified','created','link_name']
    62     exclude = ['user','modified','created','link_name', 'inheritance_line']
       
    63 
    63 
    64 
    64 class SiteSettingsForm(forms_helpers.DbModelForm):
    65 class SiteSettingsForm(forms_helpers.DbModelForm):
    65   """Django form displayed when creating or editing Site Settings.
    66   """Django form displayed when creating or editing Site Settings.
    66   """
    67   """
    67   class Meta:
    68   class Meta:
    68     """Inner Meta class that defines some behavior for the form.
    69     """Inner Meta class that defines some behavior for the form.
    69     """
    70     """
    70     #: db.Model subclass for which the form will gather information
    71     #: db.Model subclass for which the form will gather information
    71     model = soc.models.site_settings.SiteSettings
    72     model = soc.models.site_settings.SiteSettings
    72 
    73 
       
    74     #: list of model fields which will *not* be gathered by the form
       
    75     exclude = ['inheritance_line', 'home']
       
    76 
    73   def clean_feed_url(self):
    77   def clean_feed_url(self):
    74     feed_url = self.cleaned_data.get('feed_url')
    78     feed_url = self.cleaned_data.get('feed_url')
    75 
    79 
    76     if feed_url == '':
    80     if feed_url == '':
    77       # feed url not supplied (which is OK), so do not try to validate it
    81       # feed url not supplied (which is OK), so do not try to validate it
    80     if not feed.isFeedURLValid(feed_url):
    84     if not feed.isFeedURLValid(feed_url):
    81       raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
    85       raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
    82 
    86 
    83     return feed_url
    87     return feed_url
    84 
    88 
    85 DEF_SITE_HOME_PATH = 'site/home'
    89 
       
    90 DEF_SITE_SETTINGS_PATH = 'site'
       
    91 DEF_SITE_HOME_DOC_LINK_NAME = 'home'
       
    92 
    86 DEF_SITE_HOME_PUBLIC_TMPL = 'soc/site/home/public.html'
    93 DEF_SITE_HOME_PUBLIC_TMPL = 'soc/site/home/public.html'
    87 
    94 
    88 def public(request, template=DEF_SITE_HOME_PUBLIC_TMPL):
    95 def public(request, template=DEF_SITE_HOME_PUBLIC_TMPL):
    89   """How the "general public" sees the Melange site home page.
    96   """How the "general public" sees the Melange site home page.
    90 
    97 
    96     A subclass of django.http.HttpResponse with generated template.
   103     A subclass of django.http.HttpResponse with generated template.
    97   """
   104   """
    98   # create default template context for use with any templates
   105   # create default template context for use with any templates
    99   context = response_helpers.getUniversalContext(request)
   106   context = response_helpers.getUniversalContext(request)
   100   
   107   
   101   document = soc.logic.document.getDocumentFromPath(DEF_SITE_HOME_PATH)
   108   site_settings = soc.logic.site.settings.getSiteSettings(DEF_SITE_SETTINGS_PATH)
   102   site_settings = soc.logic.site.settings.getSiteSettingsFromPath(DEF_SITE_HOME_PATH)
   109 
   103   
       
   104   if document:
       
   105     document.content = template_helpers.unescape(document.content)
       
   106     context.update({'site_document': document})
       
   107   
       
   108   if site_settings:
   110   if site_settings:
   109     context.update({'site_settings': site_settings})
   111     context.update({'site_settings': site_settings})
   110     
   112     site_doc = site_settings.home
       
   113   
       
   114     if site_doc:
       
   115       site_doc.content = template_helpers.unescape(site_doc.content)
       
   116       context.update({'site_document': site_doc})
       
   117 
   111   return response_helpers.respond(request, template, context)
   118   return response_helpers.respond(request, template, context)
   112 
   119 
   113 
   120 
   114 DEF_SITE_HOME_EDIT_TMPL = 'soc/site/home/edit.html'
   121 DEF_SITE_HOME_EDIT_TMPL = 'soc/site/home/edit.html'
   115 
   122 
   127   context = response_helpers.getUniversalContext(request)
   134   context = response_helpers.getUniversalContext(request)
   128   
   135   
   129   logged_in_id = users.get_current_user()
   136   logged_in_id = users.get_current_user()
   130   
   137   
   131   alt_response = simple.getAltResponseIfNotDeveloper(request, context, 
   138   alt_response = simple.getAltResponseIfNotDeveloper(request, context, 
   132                                                         id = logged_in_id)
   139                                                      id=logged_in_id)
   133   if alt_response:
   140   if alt_response:
   134     # not a developer
   141     # not a developer
   135     return alt_response
   142     return alt_response
   136   
   143   
   137   alt_response = simple.getAltResponseIfNotLoggedIn(request, context, 
   144   alt_response = simple.getAltResponseIfNotLoggedIn(request, context, 
   138                                                         id = logged_in_id)
   145                                                     id=logged_in_id)
   139   if alt_response:
   146   if alt_response:
   140     # not logged in
   147     # not logged in
   141     return alt_response
   148     return alt_response
   142   
   149   
   143   alt_response = simple.getAltResponseIfNotUser(request, context, 
   150   alt_response = simple.getAltResponseIfNotUser(request, context, 
   154     document_form = DocumentForm(request.POST)
   161     document_form = DocumentForm(request.POST)
   155     settings_form = SiteSettingsForm(request.POST)
   162     settings_form = SiteSettingsForm(request.POST)
   156 
   163 
   157     if document_form.is_valid() and settings_form.is_valid():
   164     if document_form.is_valid() and settings_form.is_valid():
   158       title = document_form.cleaned_data.get('title')
   165       title = document_form.cleaned_data.get('title')
   159       link_name = DEF_SITE_HOME_PATH
   166       link_name = DEF_SITE_HOME_DOC_LINK_NAME
   160       short_name = document_form.cleaned_data.get('short_name')
   167       short_name = document_form.cleaned_data.get('short_name')
       
   168       abstract = document_form.cleaned_data.get('abstract')
   161       content = document_form.cleaned_data.get('content')
   169       content = document_form.cleaned_data.get('content')
   162       
   170       
       
   171       site_doc = soc.logic.document.updateOrCreateDocument(
       
   172           DEF_SITE_SETTINGS_PATH, link_name=link_name, title=title,
       
   173           short_name=short_name, abstract=abstract, content=content,
       
   174           user=id_user.getUserFromId(logged_in_id))
       
   175       
   163       feed_url = settings_form.cleaned_data.get('feed_url')
   176       feed_url = settings_form.cleaned_data.get('feed_url')
   164       
   177 
   165       document = soc.logic.document.updateOrCreateDocumentFromPath(
   178       site_settings = soc.logic.site.settings.updateOrCreateSiteSettings(
   166                                   DEF_SITE_HOME_PATH,
   179           DEF_SITE_SETTINGS_PATH, home=site_doc, feed_url=feed_url)
   167                                   link_name = link_name,
       
   168                                   title = title,
       
   169                                   short_name = short_name,
       
   170                                   content = content,
       
   171                                   user = id_user.getUserFromId(logged_in_id))
       
   172 
       
   173       site_settings = soc.logic.site.settings.updateOrCreateSiteSettingsFromPath(
       
   174                                   DEF_SITE_HOME_PATH,
       
   175                                   feed_url = feed_url)
       
   176       
   180       
   177       context.update({'submit_message': 'Site Settings saved.'})
   181       context.update({'submit_message': 'Site Settings saved.'})
   178   else: # request.method == 'GET'
   182   else: # request.method == 'GET'
   179     # try to fetch Document entity by unique key_name   
   183     # try to fetch SiteSettings entity by unique key_name
   180     document = soc.logic.document.getDocumentFromPath(DEF_SITE_HOME_PATH)
   184     site_settings = soc.logic.site.settings.getSiteSettings(
   181 
   185         DEF_SITE_SETTINGS_PATH)
   182     if document:
   186 
       
   187     if site_settings:
       
   188       # populate form with the existing SiteSettings entity
       
   189       settings_form = SiteSettingsForm(instance=site_settings)
       
   190       site_doc = site_settings.home
       
   191     else:
       
   192       # no SiteSettings entity exists for this key_name, so show a blank form
       
   193       settings_form = SiteSettingsForm()
       
   194       site_doc = None
       
   195 
       
   196     if site_doc:
   183       # populate form with the existing Document entity
   197       # populate form with the existing Document entity
   184       document_form = DocumentForm(instance=document)
   198       document_form = DocumentForm(instance=site_doc)
   185     else:
   199     else:
   186       # no Document entity exists for this key_name, so show a blank form
   200       # no Document entity exists for this key_name, so show a blank form
   187       document_form = DocumentForm()
   201       document_form = DocumentForm()
   188       
   202       
   189     # try to fetch SiteSettings entity by unique key_name   
       
   190     site_settings = soc.logic.site.settings.getSiteSettingsFromPath(
       
   191                                                         DEF_SITE_HOME_PATH)
       
   192 
       
   193     if site_settings:
       
   194       # populate form with the existing SiteSettings entity
       
   195       settings_form = SiteSettingsForm(instance=site_settings)
       
   196     else:
       
   197       # no SiteSettings entity exists for this key_name, so show a blank form
       
   198       settings_form = SiteSettingsForm()
       
   199     
       
   200   context.update({'document_form': document_form,
   203   context.update({'document_form': document_form,
   201                   'settings_form': settings_form })
   204                   'settings_form': settings_form })
   202   
   205   
   203   return response_helpers.respond(request, template, context)
   206   return response_helpers.respond(request, template, context)