app/soc/views/settings.py
changeset 517 661ab830e921
parent 516 ec1dcd70b97e
child 518 d9d31d316a74
equal deleted inserted replaced
516:ec1dcd70b97e 517:661ab830e921
     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 """Home page settings views.
       
    18 
       
    19 edit: settings view for authorized Developers, Administrators, etc.
       
    20 """
       
    21 
       
    22 __authors__ = [
       
    23   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    24   '"Todd Larsen" <tlarsen@google.com>',
       
    25   ]
       
    26 
       
    27 
       
    28 from google.appengine.ext import db
       
    29 
       
    30 from django import forms
       
    31 from django.utils.translation import ugettext_lazy
       
    32 
       
    33 from soc.logic import models
       
    34 from soc.logic import validate
       
    35 from soc.logic.models import document
       
    36 from soc.views import helper
       
    37 from soc.views.helper import access
       
    38 from soc.views.helper import decorators
       
    39 
       
    40 import soc.logic.models.home_settings
       
    41 import soc.models.document
       
    42 import soc.models.home_settings
       
    43 import soc.models.work
       
    44 import soc.views.helper.forms
       
    45 import soc.views.helper.responses
       
    46 import soc.views.helper.templates
       
    47 import soc.views.helper.widgets
       
    48 import soc.views.out_of_band
       
    49 
       
    50 
       
    51 class SettingsValidationForm(helper.forms.BaseForm):
       
    52   """Django form displayed when creating or editing Settings.
       
    53   
       
    54   This form includes validation functions for Settings fields.
       
    55   """
       
    56 
       
    57   def clean_feed_url(self):
       
    58     feed_url = self.cleaned_data.get('feed_url')
       
    59 
       
    60     if feed_url == '':
       
    61       # feed url not supplied (which is OK), so do not try to validate it
       
    62       return None
       
    63     
       
    64     if not validate.isFeedURLValid(feed_url):
       
    65       raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
       
    66 
       
    67     return feed_url
       
    68 
       
    69 
       
    70 class SettingsForm(SettingsValidationForm):
       
    71   """Django form displayed when creating or editing Settings.
       
    72   """
       
    73 
       
    74   class Meta:
       
    75     """Inner Meta class that defines some behavior for the form.
       
    76     """
       
    77     #: db.Model subclass for which the form will gather information
       
    78     model = soc.models.home_settings.HomeSettings
       
    79 
       
    80     #: list of model fields which will *not* be gathered by the form
       
    81     exclude = ['inheritance_line', 'home']
       
    82 
       
    83 
       
    84 class DocSelectForm(helper.forms.BaseForm):
       
    85   """Django form displayed to select a Document.
       
    86   """
       
    87 
       
    88   # TODO(tlarsen): scope_path will be a hard-coded read-only
       
    89   #   field for some (most?) User Roles
       
    90   doc_scope_path = forms.CharField(required=False,
       
    91       label=soc.models.work.Work.scope_path.verbose_name,
       
    92       help_text=soc.models.work.Work.scope_path.help_text)
       
    93 
       
    94   # TODO(tlarsen): actually, using these two text fields to specify
       
    95   #   the Document is pretty cheesy; this needs to be some much better
       
    96   #   Role-scoped Document selector that we don't have yet
       
    97   doc_link_id = forms.CharField(required=False,
       
    98       label=soc.models.work.Work.link_id.verbose_name,
       
    99       help_text=soc.models.work.Work.link_id.help_text)
       
   100 
       
   101   class Meta:
       
   102     model = None
       
   103 
       
   104 
       
   105 DEF_HOME_EDIT_TMPL = 'soc/site_settings/edit.html'
       
   106 
       
   107 @decorators.view
       
   108 def edit(request, page_name=None, scope_path=None, link_id=None, 
       
   109          logic=models.home_settings.logic,
       
   110          settings_form_class=SettingsForm,
       
   111          template=DEF_HOME_EDIT_TMPL):
       
   112   """View for authorized User to edit contents of a home page.
       
   113 
       
   114   Args:
       
   115     request: the standard django request object.
       
   116     page_name: the page name displayed in templates as page and header title
       
   117     path: path that is used to uniquely identify settings
       
   118     logic: settings logic object
       
   119     settings_form_class: 
       
   120     template: the template path to use for rendering the template
       
   121 
       
   122   Returns:
       
   123     A subclass of django.http.HttpResponse with generated template.
       
   124   """
       
   125 
       
   126   try:
       
   127     access.checkIsDeveloper(request)
       
   128   except  soc.views.out_of_band.AccessViolationResponse, alt_response:
       
   129     # TODO(tlarsen): change this to just limit Settings paths that can be
       
   130     #   viewed or modified by the User in their current Role
       
   131     return alt_response.response()
       
   132 
       
   133   # create default template context for use with any templates
       
   134   context = helper.responses.getUniversalContext(request)
       
   135   context['page_name'] = page_name
       
   136 
       
   137   settings_form = None
       
   138   doc_select_form = None
       
   139   home_doc = None
       
   140 
       
   141   if request.method == 'POST':
       
   142     settings_form = settings_form_class(request.POST)
       
   143     doc_select_form = DocSelectForm(request.POST)
       
   144     
       
   145     if doc_select_form.is_valid() and settings_form.is_valid():
       
   146       fields = {}
       
   147       
       
   148       # Ask for all the fields and pull them out 
       
   149       for field in settings_form.cleaned_data:
       
   150         value = settings_form.cleaned_data.get(field)
       
   151         fields[field] = value
       
   152 
       
   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')
       
   155 
       
   156       home_doc = document.logic.getFromFields(
       
   157           scope_path=doc_scope_path, link_id=doc_link_id)
       
   158 
       
   159       if home_doc:
       
   160         fields['home'] = home_doc
       
   161         context['notice'] = ugettext_lazy('Settings saved.')
       
   162       else:
       
   163         context['notice'] = ugettext_lazy(
       
   164             'Document not specified or could not be found; ' \
       
   165             'other Settings saved.')
       
   166 
       
   167       key_fields = logic.getKeyFieldsFromDict(fields)
       
   168       settings = logic.updateOrCreateFromFields(fields, key_fields)
       
   169       
       
   170       if settings.home:
       
   171         home_doc = settings.home
       
   172   else: # request.method == 'GET'
       
   173     # try to fetch HomeSettings entity by unique key_name
       
   174     settings = logic.getFromFields(scope_path=scope_path, 
       
   175                                    link_id=link_id)
       
   176 
       
   177     if settings:
       
   178       # populate form with the existing HomeSettings entity
       
   179       settings_form = settings_form_class(instance=settings)
       
   180 
       
   181       # check if ReferenceProperty to home Document is valid
       
   182       try:
       
   183         home_doc = settings.home
       
   184       except db.Error:
       
   185         pass
       
   186     
       
   187       if home_doc:
       
   188         doc_select_form = DocSelectForm(initial={
       
   189             'doc_scope_path': home_doc.scope_path,
       
   190             'doc_link_id': home_doc.link_id})
       
   191       else:
       
   192         doc_select_form = DocSelectForm()
       
   193     else:
       
   194       # no SiteSettings entity exists for this key_name, so show a blank form
       
   195       settings_form = settings_form_class()
       
   196       doc_select_form = DocSelectForm()
       
   197 
       
   198   context.update({'settings_form': settings_form,
       
   199                   'doc_select_form': doc_select_form,
       
   200                   'home_doc': home_doc})
       
   201   
       
   202   return helper.responses.respond(request, template, context)