app/soc/views/settings.py
changeset 377 d94ec6f104cc
parent 370 36d367e741e2
child 390 d12c95ade374
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 """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.api import users
       
    29 from google.appengine.ext import db
       
    30 
       
    31 from django import forms
       
    32 from django.utils.translation import ugettext_lazy
       
    33 
       
    34 from soc.logic import models
       
    35 from soc.logic import validate
       
    36 from soc.logic.models import document
       
    37 from soc.views import helper
       
    38 from soc.views.helper import access
       
    39 from soc.views.helper import decorators
       
    40 
       
    41 import soc.logic.models.home_settings
       
    42 import soc.models.document
       
    43 import soc.models.home_settings
       
    44 import soc.models.work
       
    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 SettingsForm(helper.forms.DbModelForm):
       
    53   """Django form displayed when creating or editing Settings.
       
    54   """
       
    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.home_settings.HomeSettings
       
    61 
       
    62     #: list of model fields which will *not* be gathered by the form
       
    63     exclude = ['inheritance_line', 'home']
       
    64 
       
    65   def clean_feed_url(self):
       
    66     feed_url = self.cleaned_data.get('feed_url')
       
    67 
       
    68     if feed_url == '':
       
    69       # feed url not supplied (which is OK), so do not try to validate it
       
    70       return None
       
    71     
       
    72     if not validate.isFeedURLValid(feed_url):
       
    73       raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
       
    74 
       
    75     return feed_url
       
    76 
       
    77 
       
    78 class DocSelectForm(helper.forms.DbModelForm):
       
    79   """Django form displayed to select a Document.
       
    80   """
       
    81 
       
    82   # TODO(tlarsen): partial_path will be a hard-coded read-only
       
    83   #   field for some (most?) User Roles
       
    84   partial_path = forms.CharField(required=False,
       
    85       label=soc.models.work.Work.partial_path.verbose_name,
       
    86       help_text=soc.models.work.Work.partial_path.help_text)
       
    87 
       
    88   # TODO(tlarsen): actually, using these two text fields to specify
       
    89   #   the Document is pretty cheesy; this needs to be some much better
       
    90   #   Role-scoped Document selector that we don't have yet
       
    91   link_name = forms.CharField(required=False,
       
    92       label=soc.models.work.Work.link_name.verbose_name,
       
    93       help_text=soc.models.work.Work.link_name.help_text)
       
    94 
       
    95   class Meta:
       
    96     model = None
       
    97 
       
    98 
       
    99 DEF_HOME_EDIT_TMPL = 'soc/settings/edit.html'
       
   100 
       
   101 @decorators.view
       
   102 def edit(request, page=None, path=None, logic=models.home_settings.logic,
       
   103          template=DEF_HOME_EDIT_TMPL):
       
   104   """View for authorized User to edit contents of a home page.
       
   105 
       
   106   Args:
       
   107     request: the standard django request object.
       
   108     page: a soc.logic.site.page.Page object which is abstraction that
       
   109       combines a Django view with sidebar menu info
       
   110     template: the template path to use for rendering the template.
       
   111 
       
   112   Returns:
       
   113     A subclass of django.http.HttpResponse with generated template.
       
   114   """
       
   115 
       
   116   try:
       
   117     access.checkIsDeveloper(request)
       
   118   except  soc.views.out_of_band.AccessViolationResponse, alt_response:
       
   119     # TODO(tlarsen): change this to just limit Settings paths that can be
       
   120     #   viewed or modified by the User in their current Role
       
   121     return alt_response.response()
       
   122 
       
   123   # create default template context for use with any templates
       
   124   context = helper.responses.getUniversalContext(request)
       
   125   context['page'] = page
       
   126 
       
   127   settings_form = None
       
   128   doc_select_form = None
       
   129   home_doc = None
       
   130 
       
   131   if request.method == 'POST':
       
   132     settings_form = SettingsForm(request.POST)
       
   133     doc_select_form = DocSelectForm(request.POST)
       
   134     
       
   135     if doc_select_form.is_valid() and settings_form.is_valid():
       
   136       fields = {'feed_url': settings_form.cleaned_data.get('feed_url')}
       
   137       partial_path = doc_select_form.cleaned_data.get('partial_path')
       
   138       link_name = doc_select_form.cleaned_data.get('link_name')
       
   139 
       
   140       home_doc = document.logic.getFromFields(
       
   141           partial_path=partial_path, link_name=link_name)
       
   142 
       
   143       if home_doc:
       
   144         fields['home'] = home_doc
       
   145         context['notice'] = ugettext_lazy('Settings saved.')
       
   146       else:
       
   147         context['notice'] = ugettext_lazy('Document not specified or could not be found; other Settings saved.')
       
   148 
       
   149       settings = logic.updateOrCreateFromFields(fields, path=path)
       
   150       
       
   151       if settings.home:
       
   152         home_doc = settings.home
       
   153   else: # request.method == 'GET'
       
   154     # try to fetch HomeSettings entity by unique key_name
       
   155     settings = logic.getFromFields(path=path)
       
   156 
       
   157     if settings:
       
   158       # populate form with the existing HomeSettings entity
       
   159       settings_form = SettingsForm(instance=settings)
       
   160 
       
   161       # check if ReferenceProperty to home Document is valid
       
   162       try:
       
   163         home_doc = settings.home
       
   164       except db.Error:
       
   165         pass
       
   166     
       
   167       if home_doc:
       
   168         doc_select_form = DocSelectForm(initial={
       
   169             'partial_path': home_doc.partial_path,
       
   170             'link_name': home_doc.link_name})
       
   171       else:
       
   172         doc_select_form = DocSelectForm()
       
   173     else:
       
   174       # no SiteSettings entity exists for this key_name, so show a blank form
       
   175       settings_form = SettingsForm()
       
   176       doc_select_form = DocSelectForm()
       
   177 
       
   178   context.update({'settings_form': settings_form,
       
   179                   'doc_select_form': doc_select_form,
       
   180                   'home_doc': home_doc})
       
   181   
       
   182   return helper.responses.respond(request, template, context)