app/soc/views/models/program.py
changeset 643 b57d538a9dd3
child 647 355ac73823a1
equal deleted inserted replaced
642:671dc971c3d1 643:b57d538a9dd3
       
     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 Programs.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.api import users
       
    26 
       
    27 from django import forms
       
    28 from django.utils.translation import ugettext_lazy
       
    29 
       
    30 from soc.logic import dicts
       
    31 from soc.logic import cleaning
       
    32 from soc.logic.models import sponsor as sponsor_logic
       
    33 from soc.views.helper import redirects
       
    34 from soc.views.models import base
       
    35 from soc.views.models import sponsor as sponsor_view
       
    36 from soc.views import helper
       
    37 
       
    38 import soc.logic.models.program
       
    39 
       
    40 
       
    41 class View(base.View):
       
    42   """View methods for the Sponsor model.
       
    43   """
       
    44 
       
    45   DEF_CREATE_INSTRUCTION_MSG_FMT = ugettext_lazy(
       
    46       'Please use this form to select a Sponsor for the new Program')
       
    47 
       
    48   def __init__(self, original_params=None):
       
    49     """Defines the fields and methods required for the base View class
       
    50     to provide the user with list, public, create, edit and delete views.
       
    51 
       
    52     Params:
       
    53       original_params: a dict with params for this View 
       
    54     """    
       
    55 
       
    56     params = {}
       
    57     params['logic'] = soc.logic.models.program.logic
       
    58 
       
    59     params['name'] = "Program"
       
    60     params['name_short'] = "Program"
       
    61     params['name_plural'] = "Programs"
       
    62     params['url_name'] = "program"
       
    63     params['module_name'] = "program"
       
    64 
       
    65     params['extra_dynaexclude'] = ['home']
       
    66     params['create_extra_dynafields'] = {
       
    67         'scope_path': forms.CharField(widget=forms.HiddenInput,
       
    68                                    required=False),
       
    69          'clean_link_id': cleaning.clean_link_id,
       
    70         }
       
    71 
       
    72     params = dicts.merge(original_params, params)
       
    73 
       
    74     base.View.__init__(self, params=params)
       
    75 
       
    76   def create(self, request, page_name=None, params=None, **kwargs):
       
    77     """Specialized create view to enforce needing a scope_path
       
    78 
       
    79     This view simply gives control to the base.View.create if the
       
    80     scope_path is specified in kwargs. If it is not present, it
       
    81     instead displays the result of self.selectSponsor. Refer to the
       
    82     respective docstrings on what they do.
       
    83 
       
    84     Args: 
       
    85       see base.View.create
       
    86     """
       
    87 
       
    88     if 'scope_path' in kwargs:
       
    89       return super(View, self).create(request, page_name=page_name,
       
    90           params=params, **kwargs)
       
    91 
       
    92     params = dicts.merge(params, self._params)
       
    93     return self.selectSponsor(request, page_name, params)
       
    94 
       
    95   def selectSponsor(self, request, page_name, params):
       
    96     """Displays a list page allowing the user to select a Sponsor
       
    97 
       
    98     After having selected the Sponsor, the user is redirected to the
       
    99     'create a new program' page with the scope_path set appropriately.
       
   100 
       
   101     Params usage:
       
   102       The params dictionary is passed to getCreateProgramRedirect from
       
   103         the redirects module, please see the docstring for
       
   104         getCreateProgramRedirect on how it uses it.
       
   105       The params dictionary is also passed to getListContent from
       
   106         the helper.list module, please refer to its docstring also.
       
   107       The params dictionary is passed to self._list as well, refer
       
   108         to its docstring for details on how it uses it.
       
   109 
       
   110     Args:
       
   111       request: the standard Django HTTP request object
       
   112       page_name: the page name displayed in templates as page and header title
       
   113       params: a dict with params for this View
       
   114     """
       
   115 
       
   116     new_params = {}
       
   117     new_params['list_action'] = (redirects.getCreateProgramRedirect, params)
       
   118     new_params['instruction_text'] = \
       
   119         self.DEF_CREATE_INSTRUCTION_MSG_FMT % self._params
       
   120 
       
   121     params = dicts.merge(new_params, params)
       
   122     params = dicts.merge(params, sponsor_view.view._params)
       
   123 
       
   124     content = helper.lists.getListContent(request, params, sponsor_logic.logic)
       
   125     contents = [content]
       
   126 
       
   127     return self._list(request, params, contents, page_name)
       
   128 
       
   129   def _editGet(self, request, entity, form):
       
   130     """See base.View._editGet().
       
   131     """
       
   132 
       
   133     # fill in the email field with the data from the entity
       
   134     form.fields['scope_path'].initial = entity.scope_path
       
   135 
       
   136   def getDjangoURLPatterns(self, params=None):
       
   137     """See base.View.getDjangoURLPatterns().
       
   138     """
       
   139 
       
   140     default_patterns = self._params['django_patterns_defaults']
       
   141     default_patterns += [
       
   142         (r'^%(url_name)s/create/(?P<scope_path>%(ulnp)s)$',
       
   143             'soc.views.models.%s.create', 'Create %(name_short)s')]
       
   144 
       
   145     params = {}
       
   146     params['django_patterns_defaults'] = default_patterns
       
   147 
       
   148     params = dicts.merge(params, self._params)
       
   149     patterns = super(View, self).getDjangoURLPatterns(params)
       
   150 
       
   151     return patterns
       
   152 
       
   153 
       
   154 view = View()
       
   155 
       
   156 create = view.create
       
   157 delete = view.delete
       
   158 edit = view.edit
       
   159 list = view.list
       
   160 public = view.public