app/soc/views/helper/params.py
changeset 611 2ec30182e5f1
child 613 4880ffa9f3ba
equal deleted inserted replaced
610:e0bd276ffd82 611:2ec30182e5f1
       
     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 """Params related methods
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from django import forms
       
    26 from django.utils.translation import ugettext_lazy
       
    27 
       
    28 from soc.logic import cleaning
       
    29 from soc.logic import dicts
       
    30 from soc.views import helper
       
    31 from soc.views.helper import access
       
    32 from soc.views.helper import dynaform
       
    33 from soc.views.helper import redirects
       
    34 
       
    35 
       
    36 LIST_DESCRIPTION_FMT = ugettext_lazy(
       
    37     'List of %(name_plural)s in Google Open Source Programs.')
       
    38 
       
    39 DEF_SUBMIT_MSG_PARAM_NAME = 's'
       
    40 DEF_SUBMIT_MSG_PROFILE_SAVED = 0
       
    41 
       
    42 
       
    43 def constructParams(params):
       
    44   """Constructs a new params dictionary based on params
       
    45 
       
    46   Params usage:
       
    47     The params dictionary is passed to getCreateForm and getEditForm,
       
    48       see their docstring on how they use it.
       
    49 
       
    50     rights: The rights value is merged with a default rights
       
    51       dictionary and then used as rights value.
       
    52     url_name: The url_name value is used in constructing several
       
    53       redirects as the first part of the url.
       
    54     module_name: The module_name value is used in constructing the
       
    55       location of several templates. It is expected that it matches
       
    56       the part after "/templates/soc/" for this View.
       
    57     name_plural: The name_plural argument is provided to the
       
    58       LIST_DESCRIPTION when constructing the list_description field.
       
    59     extra_dynainclude: The extra_dynainclude value is used when
       
    60       constructing the create_dynainclude value.
       
    61     extra_dynaexclude: The extra_dynaexclude value is used when
       
    62       constructing the create_dynaexclude value.
       
    63     logic: The logic value is used as argument to clean_new_link_id
       
    64       from the cleaning module.
       
    65   """
       
    66 
       
    67   rights = {}
       
    68   rights['unspecified'] = []
       
    69   rights['any_access'] = [access.checkIsLoggedIn]
       
    70   rights['public'] = [access.checkIsUser]
       
    71   rights['create'] = [access.checkIsDeveloper]
       
    72   rights['edit'] = [access.checkIsDeveloper]
       
    73   rights['delete'] = [access.checkIsDeveloper]
       
    74   rights['list'] = [access.checkIsDeveloper]
       
    75 
       
    76   if 'rights' in params:
       
    77     rights = dicts.merge(params['rights'], rights)
       
    78 
       
    79   new_params = {}
       
    80   new_params['rights'] = rights
       
    81   new_params['create_redirect'] = '/%(url_name)s' % params
       
    82   new_params['edit_redirect'] = '/%(url_name)s/edit' % params
       
    83   new_params['missing_redirect'] = '/%(url_name)s/create' % params
       
    84   new_params['delete_redirect'] = '/%(url_name)s/list' % params
       
    85   new_params['invite_redirect'] = '/request/list'
       
    86 
       
    87   new_params['sidebar'] = None
       
    88   new_params['sidebar_defaults'] = [
       
    89    ('/%s/create', 'New %(name)s', 'create'),
       
    90    ('/%s/list', 'List %(name_plural)s', 'list'),
       
    91   ]
       
    92   new_params['sidebar_additional'] = []
       
    93 
       
    94   new_params['key_fields_prefix'] = []
       
    95 
       
    96   new_params['django_patterns'] = None
       
    97   new_params['django_patterns_defaults'] = [
       
    98       (r'^%(url_name)s/show/%(key_fields)s$',
       
    99           'soc.views.models.%s.public', 'Show %(name_short)s'),
       
   100       (r'^%(url_name)s/create$',
       
   101           'soc.views.models.%s.create', 'Create %(name_short)s'),
       
   102       (r'^%(url_name)s/create/%(key_fields)s$',
       
   103           'soc.views.models.%s.create', 'Create %(name_short)s'),
       
   104       (r'^%(url_name)s/delete/%(key_fields)s$',
       
   105           'soc.views.models.%s.delete', 'Delete %(name_short)s'),
       
   106       (r'^%(url_name)s/edit/%(key_fields)s$',
       
   107           'soc.views.models.%s.edit', 'Edit %(name_short)s'),
       
   108       (r'^%(url_name)s/list$',
       
   109           'soc.views.models.%s.list', 'List %(name_plural)s'),
       
   110       ]
       
   111 
       
   112   new_params['public_template'] = 'soc/%(module_name)s/public.html' % params
       
   113   new_params['create_template'] = 'soc/models/edit.html'
       
   114   new_params['edit_template'] = 'soc/models/edit.html'
       
   115   new_params['list_template'] = 'soc/models/list.html'
       
   116   new_params['invite_template'] = 'soc/models/invite.html'
       
   117 
       
   118   new_params['error_public'] = 'soc/%(module_name)s/error.html' % params
       
   119   new_params['error_edit'] = 'soc/%(module_name)s/error.html'  % params
       
   120 
       
   121   new_params['list_main'] = 'soc/list/main.html'
       
   122   new_params['list_pagination'] = 'soc/list/pagination.html'
       
   123   new_params['list_row'] = 'soc/%(module_name)s/list/row.html' % params
       
   124   new_params['list_heading'] = 'soc/%(module_name)s/list/heading.html' % params
       
   125 
       
   126   new_params['list_action'] = (redirects.getEditRedirect, params)
       
   127   new_params['list_params'] = {
       
   128       'list_action': 'action',
       
   129       'list_description': 'description',
       
   130       'list_main': 'main',
       
   131       'list_pagination': 'pagination',
       
   132       'list_row': 'row',
       
   133       'list_heading': 'heading',
       
   134       }
       
   135 
       
   136   new_params['list_description'] = LIST_DESCRIPTION_FMT % params
       
   137   new_params['save_message'] = [ugettext_lazy('Profile saved.')]
       
   138   new_params['submit_msg_param_name'] = DEF_SUBMIT_MSG_PARAM_NAME
       
   139   new_params['edit_params'] = {
       
   140       DEF_SUBMIT_MSG_PARAM_NAME: DEF_SUBMIT_MSG_PROFILE_SAVED,
       
   141       }
       
   142 
       
   143   new_params['dynabase'] = helper.forms.BaseForm
       
   144 
       
   145   new_params['create_dynainclude'] = [] + params.get('extra_dynainclude', [])
       
   146   new_params['create_dynaexclude'] = ['scope', 'scope_path'] + \
       
   147       params.get('extra_dynaexclude', [])
       
   148   new_params['create_dynafields'] = {
       
   149       'clean_link_id': cleaning.clean_new_link_id(params['logic']),
       
   150       'clean_feed_url': cleaning.clean_feed_url,
       
   151       }
       
   152 
       
   153   dynafields = {
       
   154       'clean_link_id': cleaning.clean_link_id,
       
   155       'link_id': forms.CharField(widget=helper.widgets.ReadOnlyInput()),
       
   156       }
       
   157   dynafields.update(params.get('extra_dynafields', {}))
       
   158 
       
   159   new_params['edit_dynainclude'] = None
       
   160   new_params['edit_dynaexclude'] = None
       
   161   new_params['edit_dynafields'] = dynafields
       
   162 
       
   163   params = dicts.merge(params, new_params)
       
   164 
       
   165   # These need to be constructed separately, because they require
       
   166   # parameters that can be defined either in params, or new_params.
       
   167   if not 'create_form' in params:
       
   168     params['create_form'] = getCreateForm(params)
       
   169 
       
   170   if not 'edit_form' in params:
       
   171     params['edit_form'] = getEditForm(params)
       
   172 
       
   173   return params
       
   174 
       
   175 def getCreateForm(params):
       
   176   """Constructs a new CreateForm using params
       
   177 
       
   178   Params usage:
       
   179     dynabase: The dynabase value is used as the base argument to
       
   180       dynaform.newDynaForm.
       
   181     logic: The logic value is used to get the model argument to newDynaForm.
       
   182     create_dynainclude: same as dynabase, but as dynainclude argument
       
   183     create_dynaexclude: same as dynabase, but as dynaexclude argument
       
   184     create_dynafields: same as dynabase, but as dynafields argument
       
   185   """
       
   186 
       
   187   create_form = dynaform.newDynaForm(
       
   188     dynabase = params['dynabase'],
       
   189     dynamodel = params['logic'].getModel(),
       
   190     dynainclude = params['create_dynainclude'],
       
   191     dynaexclude = params['create_dynaexclude'],
       
   192     dynafields = params['create_dynafields'],
       
   193     )
       
   194 
       
   195   return create_form
       
   196 
       
   197 def getEditForm(params):
       
   198   """Constructs a new EditForm using params
       
   199 
       
   200   Params usage:
       
   201     create_form: The dynabase value is used as the dynaform argument
       
   202       to dyanform.extendDynaForm.
       
   203     edit_dynainclude: same as create_form, but as dynainclude argument
       
   204     edit_dynaexclude: same as create_form, but as dynaexclude argument
       
   205     edit_dynafields: same as create_form, but as dynafields argument
       
   206   """
       
   207 
       
   208   edit_form = dynaform.extendDynaForm(
       
   209     dynaform = params['create_form'],
       
   210     dynainclude = params['edit_dynainclude'],
       
   211     dynaexclude = params['edit_dynaexclude'],
       
   212     dynafields = params['edit_dynafields'],
       
   213     )
       
   214 
       
   215   return edit_form