app/soc/views/site/home.py
changeset 141 e120c24b89e2
parent 88 9a8180f0941a
child 142 d88c7dbea0e8
equal deleted inserted replaced
140:c3d098d6fafa 141:e120c24b89e2
    18 
    18 
    19 public: how the general public sees the site home page of a Melange
    19 public: how the general public sees the site home page of a Melange
    20   site
    20   site
    21   
    21   
    22 There may (eventually) be different views of the site home page for
    22 There may (eventually) be different views of the site home page for
    23 logged-in Users (such as a debug() view for logged-in Developers). 
    23 logged-in Users (such as a edit() view for logged-in Developers). 
    24 """
    24 """
    25 
    25 
    26 __authors__ = [
    26 __authors__ = [
    27   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    27   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    28   ]
    28   ]
    29 
    29 
    30 
    30 
    31 from google.appengine.api import users
    31 from google.appengine.api import users
       
    32 
       
    33 from django import http
       
    34 from django import shortcuts
       
    35 from django import newforms as forms
       
    36 
       
    37 from soc.logic import out_of_band
       
    38 from soc.logic import feed
       
    39 from soc.logic.site import id_user
       
    40 from soc.views import simple
       
    41 from soc.views.helpers import forms_helpers
    32 from soc.views.helpers import response_helpers
    42 from soc.views.helpers import response_helpers
    33 
    43 from soc.views.helpers import template_helpers
    34 
    44 from soc.views.helpers import widgets
    35 def public(request, template='soc/site/home/public.html'):
    45 
       
    46 import soc.models.site_settings
       
    47 import soc.models.document
       
    48 import soc.logic.document
       
    49 import soc.logic.site.settings
       
    50 
       
    51 
       
    52 class DocumentForm(forms_helpers.DbModelForm):
       
    53   content = forms.fields.CharField(widget=widgets.TinyMCE())
       
    54   #link_name = forms.CharField(widget=forms.TextInput(
       
    55   #                                attrs={'readonly':'true'}))
       
    56   class Meta:
       
    57     """Inner Meta class that defines some behavior for the form.
       
    58     """
       
    59     #: db.Model subclass for which the form will gather information
       
    60     model = soc.models.document.Document
       
    61     
       
    62     #: list of model fields which will *not* be gathered by the form
       
    63     exclude = ['user','modified','created','link_name']
       
    64 
       
    65 class SiteSettingsForm(forms_helpers.DbModelForm):
       
    66   """Django form displayed when creating or editing Site Settings.
       
    67   """
       
    68   class Meta:
       
    69     """Inner Meta class that defines some behavior for the form.
       
    70     """
       
    71     #: db.Model subclass for which the form will gather information
       
    72     model = soc.models.site_settings.SiteSettings
       
    73 
       
    74   def clean_feed_url(self):
       
    75     feed_url = self.cleaned_data.get('feed_url')
       
    76 
       
    77     if feed_url == '':
       
    78       # feed url not supplied (which is OK), so do not try to validate it
       
    79       return None
       
    80     
       
    81     if not feed.isFeedURLValid(feed_url):
       
    82       raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
       
    83 
       
    84     return feed_url
       
    85 
       
    86 DEF_SITE_HOME_PATH = 'site/home'
       
    87 DEF_SITE_HOME_PUBLIC_TMPL = 'soc/site/home/public.html'
       
    88 
       
    89 def public(request, template=DEF_SITE_HOME_PUBLIC_TMPL):
    36   """How the "general public" sees the Melange site home page.
    90   """How the "general public" sees the Melange site home page.
    37 
    91 
    38   Args:
    92   Args:
    39     request: the standard django request object.
    93     request: the standard django request object.
    40     template: the template path to use for rendering the template.
    94     template: the template path to use for rendering the template.
    41 
    95 
    42   Returns:
    96   Returns:
    43     A subclass of django.http.HttpResponse with generated template.
    97     A subclass of django.http.HttpResponse with generated template.
    44   """
    98   """
    45   return response_helpers.respond(request,
    99   # create default template context for use with any templates
    46       template, {'template': template})
   100   context = response_helpers.getUniversalContext(request)
       
   101   
       
   102   document = soc.logic.document.getDocumentFromPath(DEF_SITE_HOME_PATH)
       
   103   site_settings = soc.logic.site.settings.getSiteSettingsFromPath(DEF_SITE_HOME_PATH)
       
   104   
       
   105   if document:
       
   106     document.content = template_helpers.unescape(document.content)
       
   107     context.update({'site_document': document})
       
   108   
       
   109   if site_settings:
       
   110     context.update({'site_settings': site_settings})
       
   111     
       
   112   return response_helpers.respond(request, template, context)
       
   113 
       
   114 
       
   115 DEF_SITE_HOME_EDIT_TMPL = 'soc/site/home/edit.html'
       
   116 
       
   117 def edit(request, template=DEF_SITE_HOME_EDIT_TMPL):
       
   118   """View for Developer to edit content of Melange site home page.
       
   119 
       
   120   Args:
       
   121     request: the standard django request object.
       
   122     template: the template path to use for rendering the template.
       
   123 
       
   124   Returns:
       
   125     A subclass of django.http.HttpResponse with generated template.
       
   126   """
       
   127   # create default template context for use with any templates
       
   128   context = response_helpers.getUniversalContext(request)
       
   129   
       
   130   logged_in_id = users.get_current_user()
       
   131   
       
   132   alt_response = simple.getAltResponseIfNotDeveloper(request, context, 
       
   133                                                         id = logged_in_id)
       
   134   if alt_response:
       
   135     # not a developer
       
   136     return alt_response
       
   137   
       
   138   alt_response = simple.getAltResponseIfNotLoggedIn(request, context, 
       
   139                                                         id = logged_in_id)
       
   140   if alt_response:
       
   141     # not logged in
       
   142     return alt_response
       
   143   
       
   144   alt_response = simple.getAltResponseIfNotUser(request, context, 
       
   145                                                         id = logged_in_id)
       
   146   if alt_response:
       
   147     # no existing User entity for logged in Google Account. User entity is 
       
   148     # required for creating Documents
       
   149     return alt_response
       
   150                              
       
   151   settings_form = None
       
   152   document_form = None
       
   153 
       
   154   if request.method == 'POST':
       
   155     document_form = DocumentForm(request.POST)
       
   156     settings_form = SiteSettingsForm(request.POST)
       
   157 
       
   158     if document_form.is_valid() and settings_form.is_valid():
       
   159       title = document_form.cleaned_data.get('title')
       
   160       link_name = DEF_SITE_HOME_PATH
       
   161       short_name = document_form.cleaned_data.get('short_name')
       
   162       content = document_form.cleaned_data.get('content')
       
   163       
       
   164       feed_url = settings_form.cleaned_data.get('feed_url')
       
   165       
       
   166       document = soc.logic.document.updateOrCreateDocumentFromPath(
       
   167                                   DEF_SITE_HOME_PATH,
       
   168                                   link_name = link_name,
       
   169                                   title = title,
       
   170                                   short_name = short_name,
       
   171                                   content = content,
       
   172                                   user = id_user.getUserFromId(logged_in_id))
       
   173 
       
   174       site_settings = soc.logic.site.settings.updateOrCreateSiteSettingsFromPath(
       
   175                                   DEF_SITE_HOME_PATH,
       
   176                                   feed_url = feed_url)
       
   177       
       
   178       context.update({'submit_message': 'Site Settings saved.'})
       
   179   else: # request.method == 'GET'
       
   180     # try to fetch Document entity by unique key_name   
       
   181     document = soc.logic.document.getDocumentFromPath(DEF_SITE_HOME_PATH)
       
   182 
       
   183     if document:
       
   184       # populate form with the existing Document entity
       
   185       document_form = DocumentForm(instance=document)
       
   186     else:
       
   187       # no Document entity exists for this key_name, so show a blank form
       
   188       document_form = DocumentForm()
       
   189       
       
   190     # try to fetch SiteSettings entity by unique key_name   
       
   191     site_settings = soc.logic.site.settings.getSiteSettingsFromPath(
       
   192                                                         DEF_SITE_HOME_PATH)
       
   193 
       
   194     if site_settings:
       
   195       # populate form with the existing SiteSettings entity
       
   196       settings_form = SiteSettingsForm(instance=site_settings)
       
   197     else:
       
   198       # no SiteSettings entity exists for this key_name, so show a blank form
       
   199       settings_form = SiteSettingsForm()
       
   200     
       
   201   context.update({'document_form': document_form,
       
   202                   'settings_form': settings_form })
       
   203   
       
   204   return response_helpers.respond(request, template, context)