app/soc/views/models/home_settings.py
changeset 446 0b479d573a4c
child 452 160c748988a2
equal deleted inserted replaced
445:31927f21970d 446:0b479d573a4c
       
     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 """Views for Home Settings.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.ext import db
       
    26 from google.appengine.api import users
       
    27 
       
    28 from django import forms
       
    29 from django.utils.translation import ugettext_lazy
       
    30 
       
    31 from soc.logic import dicts
       
    32 from soc.logic import validate
       
    33 from soc.views import helper
       
    34 from soc.views.helper import widgets
       
    35 from soc.views.models import base
       
    36 
       
    37 import soc.models.home_settings
       
    38 import soc.logic.models.home_settings
       
    39 import soc.logic.dicts
       
    40 import soc.views.helper
       
    41 import soc.views.helper.widgets
       
    42 
       
    43 
       
    44 class SettingsValidationForm(helper.forms.BaseForm):
       
    45   """Django form displayed when creating or editing Settings.
       
    46   
       
    47   This form includes validation functions for Settings fields.
       
    48   """
       
    49 
       
    50     # TODO(tlarsen): partial_path will be a hard-coded read-only
       
    51     #   field for some (most?) User Roles
       
    52   doc_partial_path = forms.CharField(required=False,
       
    53       label=soc.models.work.Work.partial_path.verbose_name,
       
    54       help_text=soc.models.work.Work.partial_path.help_text)
       
    55 
       
    56   # TODO(tlarsen): actually, using these two text fields to specify
       
    57   #   the Document is pretty cheesy; this needs to be some much better
       
    58   #   Role-scoped Document selector that we don't have yet
       
    59   doc_link_name = forms.CharField(required=False,
       
    60       label=soc.models.work.Work.link_name.verbose_name,
       
    61       help_text=soc.models.work.Work.link_name.help_text)
       
    62 
       
    63   def clean_feed_url(self):
       
    64     feed_url = self.cleaned_data.get('feed_url')
       
    65 
       
    66     if feed_url == '':
       
    67       # feed url not supplied (which is OK), so do not try to validate it
       
    68       return None
       
    69     
       
    70     if not validate.isFeedURLValid(feed_url):
       
    71       raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')
       
    72 
       
    73     return feed_url
       
    74 
       
    75 
       
    76 class CreateForm(SettingsValidationForm):
       
    77   """Django form displayed when creating or editing Settings.
       
    78   """
       
    79 
       
    80   class Meta:
       
    81     """Inner Meta class that defines some behavior for the form.
       
    82     """
       
    83     #: db.Model subclass for which the form will gather information
       
    84     model = soc.models.home_settings.HomeSettings
       
    85 
       
    86     #: list of model fields which will *not* be gathered by the form
       
    87     exclude = ['inheritance_line', 'home']
       
    88 
       
    89 
       
    90 class EditForm(CreateForm):
       
    91   """Django form displayed a Document is edited.
       
    92   """
       
    93 
       
    94   pass
       
    95 
       
    96 
       
    97 class View(base.View):
       
    98   """View methods for the Docs model
       
    99   """
       
   100 
       
   101   def __init__(self, original_params=None, original_rights=None, stop=False):
       
   102     """Defines the fields and methods required for the base View class
       
   103     to provide the user with list, public, create, edit and delete views.
       
   104 
       
   105     Params:
       
   106       original_params: a dict with params for this View
       
   107       original_rights: a dict with right definitions for this View
       
   108     """
       
   109 
       
   110     params = {}
       
   111     rights = {}
       
   112 
       
   113     params['name'] = "HomeSetting"
       
   114     params['name_short'] = "Home"
       
   115     params['name_plural'] = "HomeSettings"
       
   116 
       
   117     params['edit_form'] = EditForm
       
   118     params['create_form'] = CreateForm
       
   119 
       
   120     # TODO(tlarsen) Add support for Django style template lookup
       
   121     params['edit_template'] = 'soc/models/edit.html'
       
   122     params['public_template'] = 'soc/home_settings/public.html'
       
   123     params['list_template'] = 'soc/models/list.html'
       
   124 
       
   125     params['lists_template'] = {
       
   126       'list_main': 'soc/list/list_main.html',
       
   127       'list_pagination': 'soc/list/list_pagination.html',
       
   128       'list_row': 'soc/home_settings/list/home_row.html',
       
   129       'list_heading': 'soc/home_settings/list/home_heading.html',
       
   130     }
       
   131 
       
   132     params['delete_redirect'] = 'home/list'
       
   133     params['create_redirect'] = 'home/edit'
       
   134 
       
   135     params['save_message'] = [ugettext_lazy('Profile saved.')]
       
   136 
       
   137     params['edit_params'] = {
       
   138         self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED,
       
   139         }
       
   140 
       
   141     rights['list'] = [helper.access.checkIsDeveloper]
       
   142     rights['delete'] = [helper.access.checkIsDeveloper]
       
   143 
       
   144     params = dicts.merge(original_params, params)
       
   145     rights = dicts.merge(original_rights, rights)
       
   146 
       
   147     base.View.__init__(self, rights=rights, params=params, stop=stop)
       
   148 
       
   149     self._logic = soc.logic.models.home_settings.logic
       
   150 
       
   151   def _public(self, request, entity, context):
       
   152     """
       
   153     """
       
   154 
       
   155     if not entity:
       
   156       return
       
   157 
       
   158     try:
       
   159       home_doc = entity.home
       
   160     except db.Error:
       
   161       home_doc = None
       
   162 
       
   163     if home_doc:
       
   164       home_doc.content = helper.templates.unescape(home_doc.content)
       
   165       context['home_document'] = home_doc
       
   166 
       
   167   def _editGet(self, request, entity, form):
       
   168     """See base.View._editGet().
       
   169     """
       
   170 
       
   171     try:
       
   172       form.fields['doc_partial_path'].initial = entity.home.partial_path
       
   173       form.fields['doc_link_name'].initial = entity.home.link_name
       
   174     except db.Error:
       
   175       pass
       
   176 
       
   177 
       
   178 view = View()
       
   179 
       
   180 create = view.create
       
   181 edit = view.edit
       
   182 delete = view.delete
       
   183 list = view.list
       
   184 public = view.public