app/soc/views/site/settings.py
changeset 377 d94ec6f104cc
parent 376 ce8b3a9fa0de
child 378 51c41e8bd45f
equal deleted inserted replaced
376:ce8b3a9fa0de 377:d94ec6f104cc
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Site-wide Melange home page views.
       
    18 
       
    19 public: how the general public sees the site home page of a Melange
       
    20   site
       
    21   
       
    22 edit: site settings view for logged-in Developers
       
    23 """
       
    24 
       
    25 __authors__ = [
       
    26   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    27   ]
       
    28 
       
    29 
       
    30 from google.appengine.api import users
       
    31 from google.appengine.ext import db
       
    32 
       
    33 from django import forms
       
    34 
       
    35 from soc.logic import models
       
    36 from soc.logic import validate
       
    37 from soc.logic.models import document
       
    38 from soc.views import helper
       
    39 from soc.views.helper import access
       
    40 from soc.views.helper import decorators
       
    41 
       
    42 import soc.logic.models.site_settings
       
    43 import soc.models.document
       
    44 import soc.models.site_settings
       
    45 import soc.views.helper.forms
       
    46 import soc.views.helper.responses
       
    47 import soc.views.helper.templates
       
    48 import soc.views.helper.widgets
       
    49 import soc.views.out_of_band
       
    50 
       
    51 
       
    52 class DocumentForm(helper.forms.DbModelForm):
       
    53   content = forms.fields.CharField(widget=helper.widgets.TinyMCE(
       
    54       attrs={'rows':10, 'cols':40}))
       
    55 
       
    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 = ['partial_path', 'link_name',
       
    64                'author', 'modified', 'created', 'inheritance_line']
       
    65 
       
    66 
       
    67 class SiteSettingsForm(helper.forms.DbModelForm):
       
    68   """Django form displayed when creating or editing Site Settings.
       
    69   """
       
    70   class Meta:
       
    71     """Inner Meta class that defines some behavior for the form.
       
    72     """
       
    73     #: db.Model subclass for which the form will gather information
       
    74     model = soc.models.site_settings.SiteSettings
       
    75 
       
    76     #: list of model fields which will *not* be gathered by the form
       
    77     exclude = ['inheritance_line', 'home']
       
    78 
       
    79   def clean_feed_url(self):
       
    80     feed_url = self.cleaned_data.get('feed_url')
       
    81 
       
    82     if feed_url == '':
       
    83       # feed url not supplied (which is OK), so do not try to validate it
       
    84       return None
       
    85     
       
    86     if not validate.isFeedURLValid(feed_url):
       
    87       raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
       
    88 
       
    89     return feed_url
       
    90 
       
    91 
       
    92 DEF_SITE_HOME_EDIT_TMPL = 'soc/site/settings/edit.html'
       
    93 
       
    94 @decorators.view
       
    95 def edit(request, page=None, template=DEF_SITE_HOME_EDIT_TMPL):
       
    96   """View for Developer to edit content of Melange site home page.
       
    97 
       
    98   Args:
       
    99     request: the standard django request object.
       
   100     page: a soc.logic.site.page.Page object which is abstraction that combines 
       
   101       a Django view with sidebar menu info
       
   102     template: the template path to use for rendering the template.
       
   103 
       
   104   Returns:
       
   105     A subclass of django.http.HttpResponse with generated template.
       
   106   """
       
   107 
       
   108   try:
       
   109     access.checkIsDeveloper(request)
       
   110   except  soc.views.out_of_band.AccessViolationResponse, alt_response:
       
   111     return alt_response.response()
       
   112 
       
   113   # create default template context for use with any templates
       
   114   context = helper.responses.getUniversalContext(request)
       
   115 
       
   116   settings_form = None
       
   117   document_form = None
       
   118 
       
   119   if request.method == 'POST':
       
   120     document_form = DocumentForm(request.POST)
       
   121     settings_form = SiteSettingsForm(request.POST)
       
   122 
       
   123     if document_form.is_valid() and settings_form.is_valid():
       
   124       link_name = models.site_settings.logic.DEF_SITE_HOME_DOC_LINK_NAME
       
   125       partial_path = models.site_settings.logic.DEF_SITE_SETTINGS_PATH
       
   126       logged_in_id = users.get_current_user()
       
   127       author = models.user.logic.getFromFields(email=logged_in_id.email())
       
   128 
       
   129       properties = {
       
   130         'title': document_form.cleaned_data.get('title'),
       
   131         'short_name': document_form.cleaned_data.get('short_name'),
       
   132         'content': document_form.cleaned_data.get('content'),
       
   133         'link_name': link_name,
       
   134         'partial_path': partial_path,
       
   135         'id': logged_in_id,
       
   136         'author': author,
       
   137       }
       
   138 
       
   139       site_doc = document.logic.updateOrCreateFromFields(
       
   140           properties, partial_path=partial_path, link_name=link_name)
       
   141       
       
   142       feed_url = settings_form.cleaned_data.get('feed_url')
       
   143 
       
   144       site_settings = models.site_settings.logic.updateOrCreateFromFields(
       
   145           {'feed_url': feed_url, 'home': site_doc},
       
   146           path=models.site_settings.logic.DEF_SITE_SETTINGS_PATH)
       
   147       
       
   148       context['notice'] = 'Site Settings saved.'
       
   149   else: # request.method == 'GET'
       
   150     # try to fetch SiteSettings entity by unique key_name
       
   151     site_settings = models.site_settings.logic.getFromFields(
       
   152         path=models.site_settings.logic.DEF_SITE_SETTINGS_PATH)
       
   153 
       
   154     if site_settings:
       
   155       # populate form with the existing SiteSettings entity
       
   156       settings_form = SiteSettingsForm(instance=site_settings)
       
   157       
       
   158       # check if ReferenceProperty to home Document is valid
       
   159       try:
       
   160         site_doc = site_settings.home
       
   161       except db.Error:
       
   162         site_doc = None
       
   163     
       
   164     else:
       
   165       # no SiteSettings entity exists for this key_name, so show a blank form
       
   166       settings_form = SiteSettingsForm()
       
   167       site_doc = None
       
   168 
       
   169     if site_doc:
       
   170       # populate form with the existing Document entity
       
   171       document_form = DocumentForm(instance=site_doc)
       
   172     else:
       
   173       # no Document entity exists for this key_name, so show a blank form
       
   174       document_form = DocumentForm()
       
   175       
       
   176   context.update({'document_form': document_form,
       
   177                   'settings_form': settings_form })
       
   178   
       
   179   return helper.responses.respond(request, template, context)