app/soc/views/models/base.py
changeset 611 2ec30182e5f1
parent 610 e0bd276ffd82
child 612 3cca81b1e5a1
equal deleted inserted replaced
610:e0bd276ffd82 611:2ec30182e5f1
    23   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    23   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    24   ]
    24   ]
    25 
    25 
    26 
    26 
    27 from django import http
    27 from django import http
    28 from django import forms
       
    29 from django.conf.urls import defaults
       
    30 from django.utils.translation import ugettext_lazy
    28 from django.utils.translation import ugettext_lazy
    31 
    29 
    32 import soc.logic
    30 import soc.logic
    33 import soc.logic.lists
    31 import soc.logic.lists
    34 import soc.views.helper.lists
    32 import soc.views.helper.lists
    35 import soc.views.helper.responses
    33 import soc.views.helper.responses
    36 
    34 import soc.views.helper.params
    37 from soc.logic import cleaning
    35 
    38 from soc.logic import dicts
    36 from soc.logic import dicts
    39 from soc.logic import models
    37 from soc.logic import models
    40 from soc.models import linkable
    38 from soc.models import linkable
    41 from soc.views import helper
    39 from soc.views import helper
    42 from soc.views import out_of_band
    40 from soc.views import out_of_band
    43 from soc.views.helper import access
       
    44 from soc.views.helper import dynaform
       
    45 
    41 
    46 
    42 
    47 class View(object):
    43 class View(object):
    48   """Views for entity classes.
    44   """Views for entity classes.
    49 
    45 
    50   The View class functions specific to Entity classes by relying
    46   The View class functions specific to Entity classes by relying
    51   on the the child-classes to define the following fields:
    47   on the the child-classes to define the following fields:
    52 
    48 
    53   self._logic: the logic singleton for this entity
    49   self._logic: the logic singleton for this entity
    54   """
    50   """
    55 
       
    56   DEF_SUBMIT_MSG_PARAM_NAME = 's'
       
    57   DEF_SUBMIT_MSG_PROFILE_SAVED = 0
       
    58 
    51 
    59   DEF_CREATE_NEW_ENTITY_MSG_FMT = ugettext_lazy(
    52   DEF_CREATE_NEW_ENTITY_MSG_FMT = ugettext_lazy(
    60       ' You can create a new %(entity_type)s by visiting'
    53       ' You can create a new %(entity_type)s by visiting'
    61       ' <a href="%(create)s">Create '
    54       ' <a href="%(create)s">Create '
    62       'a New %(entity_type)s</a> page.')
    55       'a New %(entity_type)s</a> page.')
    68       params: This dictionary should be filled with the parameters
    61       params: This dictionary should be filled with the parameters
    69         specific to this entity. See the methods in this class on
    62         specific to this entity. See the methods in this class on
    70         the fields it should contain, and how they are used.
    63         the fields it should contain, and how they are used.
    71     """
    64     """
    72 
    65 
    73     rights = {}
    66     self._params = helper.params.constructParams(params)
    74     rights['unspecified'] = []
    67     self._logic = params['logic']
    75     rights['any_access'] = [access.checkIsLoggedIn]
    68 
    76     rights['public'] = [access.checkIsUser]
       
    77     rights['create'] = [access.checkIsDeveloper]
       
    78     rights['edit'] = [access.checkIsDeveloper]
       
    79     rights['delete'] = [access.checkIsDeveloper]
       
    80     rights['list'] = [access.checkIsDeveloper]
       
    81 
       
    82     if 'rights' in params:
       
    83       rights = dicts.merge(params['rights'], rights)
       
    84 
       
    85     new_params = {}
       
    86     new_params['rights'] = rights
       
    87     new_params['create_redirect'] = '/%(url_name)s' % params
       
    88     new_params['edit_redirect'] = '/%(url_name)s/edit' % params
       
    89     new_params['missing_redirect'] = '/%(url_name)s/create' % params
       
    90     new_params['delete_redirect'] = '/%(url_name)s/list' % params
       
    91     new_params['invite_redirect'] = '/request/list'
       
    92     
       
    93     new_params['sidebar'] = None
       
    94     new_params['sidebar_defaults'] = [
       
    95      ('/%s/create', 'New %(name)s', 'create'),
       
    96      ('/%s/list', 'List %(name_plural)s', 'list'),
       
    97     ]
       
    98     new_params['sidebar_additional'] = []
       
    99 
       
   100     new_params['key_fields_prefix'] = []
       
   101 
       
   102     new_params['django_patterns'] = None
       
   103     new_params['django_patterns_defaults'] = [
       
   104         (r'^%(url_name)s/show/%(key_fields)s$', 
       
   105             'soc.views.models.%s.public', 'Show %(name_short)s'),
       
   106         (r'^%(url_name)s/create$',
       
   107             'soc.views.models.%s.create', 'Create %(name_short)s'),
       
   108         (r'^%(url_name)s/create/%(key_fields)s$',
       
   109             'soc.views.models.%s.create', 'Create %(name_short)s'),
       
   110         (r'^%(url_name)s/delete/%(key_fields)s$',
       
   111             'soc.views.models.%s.delete', 'Delete %(name_short)s'),
       
   112         (r'^%(url_name)s/edit/%(key_fields)s$',
       
   113             'soc.views.models.%s.edit', 'Edit %(name_short)s'),
       
   114         (r'^%(url_name)s/list$',
       
   115             'soc.views.models.%s.list', 'List %(name_plural)s'),
       
   116         ]
       
   117 
       
   118     new_params['public_template'] = 'soc/%(module_name)s/public.html' % params
       
   119     new_params['create_template'] = 'soc/models/edit.html'
       
   120     new_params['edit_template'] = 'soc/models/edit.html'
       
   121     new_params['list_template'] = 'soc/models/list.html'
       
   122     new_params['invite_template'] = 'soc/models/invite.html'
       
   123 
       
   124     new_params['error_public'] = 'soc/%(module_name)s/error.html' % params
       
   125     new_params['error_edit'] = 'soc/%(module_name)s/error.html'  % params
       
   126 
       
   127     new_params['list_main'] = 'soc/list/main.html'
       
   128     new_params['list_pagination'] = 'soc/list/pagination.html'
       
   129     new_params['list_row'] = 'soc/%(module_name)s/list/row.html' % params
       
   130     new_params['list_heading'] = 'soc/%(module_name)s/list/heading.html' % params
       
   131 
       
   132     new_params['list_action'] = (self.getEditRedirect, None)
       
   133     new_params['list_params'] = {
       
   134         'list_action': 'action',
       
   135         'list_description': 'description',
       
   136         'list_main': 'main',
       
   137         'list_pagination': 'pagination',
       
   138         'list_row': 'row',
       
   139         'list_heading': 'heading',
       
   140         }
       
   141 
       
   142     description = ugettext_lazy('List of %(name_plural)s in Google Open Source Programs.')
       
   143     new_params['list_description'] = description % params
       
   144     new_params['save_message'] = [ugettext_lazy('Profile saved.')]
       
   145     new_params['edit_params'] = {
       
   146         self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED,
       
   147         }
       
   148 
       
   149     new_params['dynabase'] = helper.forms.BaseForm
       
   150 
       
   151     new_params['create_dynainclude'] = [] + params.get('extra_dynainclude', [])
       
   152     new_params['create_dynaexclude'] = ['scope', 'scope_path'] + \
       
   153         params.get('extra_dynaexclude', [])
       
   154     new_params['create_dynafields'] = {
       
   155         'clean_link_id': cleaning.clean_new_link_id(params['logic']),
       
   156         'clean_feed_url': cleaning.clean_feed_url,
       
   157         }
       
   158 
       
   159     dynafields = {
       
   160         'clean_link_id': cleaning.clean_link_id,
       
   161         'link_id': forms.CharField(widget=helper.widgets.ReadOnlyInput()),
       
   162         }
       
   163     dynafields.update(params.get('extra_dynafields', {}))
       
   164 
       
   165     new_params['edit_dynainclude'] = None
       
   166     new_params['edit_dynaexclude'] = None
       
   167     new_params['edit_dynafields'] = dynafields
       
   168 
       
   169     self._params = dicts.merge(params, new_params)
       
   170     self._logic = self._params['logic']
       
   171 
       
   172     # These need to be constructed seperately, because they require
       
   173     # parameters that can be defined either in params, or new_params.
       
   174     if 'create_form' not in self._params:
       
   175       self._params['create_form'] = dynaform.newDynaForm(
       
   176         dynabase = self._params['dynabase'],
       
   177         dynamodel = self._logic.getModel(),
       
   178         dynainclude = self._params['create_dynainclude'],
       
   179         dynaexclude = self._params['create_dynaexclude'],
       
   180         dynafields = self._params['create_dynafields'],
       
   181         )
       
   182 
       
   183     if 'edit_form' not in self._params:
       
   184       self._params['edit_form'] = dynaform.extendDynaForm(
       
   185         dynaform = self._params['create_form'],
       
   186         dynainclude = self._params['edit_dynainclude'],
       
   187         dynaexclude = self._params['edit_dynaexclude'],
       
   188         dynafields = self._params['edit_dynafields'],
       
   189         )
       
   190 
    69 
   191   def public(self, request, page_name=None, params=None, **kwargs):
    70   def public(self, request, page_name=None, params=None, **kwargs):
   192     """Displays the public page for the entity specified by **kwargs.
    71     """Displays the public page for the entity specified by **kwargs.
   193 
    72 
   194     Params usage:
    73     Params usage:
   409         _editGet on how it uses it.
   288         _editGet on how it uses it.
   410       create_form: The create_form is used as form if there was no
   289       create_form: The create_form is used as form if there was no
   411         existing entity. If the seed argument is present, it is passed
   290         existing entity. If the seed argument is present, it is passed
   412         as the 'initial' argument on construction. Otherwise, it is
   291         as the 'initial' argument on construction. Otherwise, it is
   413         called with no arguments.
   292         called with no arguments.
       
   293       submit_msg_param_name: The submit_msg_param_name value is used
       
   294         as the key part in the ?key=value construct for the submit
       
   295         message parameter (see also save_message).
   414 
   296 
   415     Args:
   297     Args:
   416       request: the django request object
   298       request: the django request object
   417       entity: the entity that will be edited, may be None
   299       entity: the entity that will be edited, may be None
   418       context: the context dictionary that will be provided to django
   300       context: the context dictionary that will be provided to django
   424     suffix = self._logic.getKeySuffix(entity)
   306     suffix = self._logic.getKeySuffix(entity)
   425 
   307 
   426     # Remove the params from the request, this is relevant only if
   308     # Remove the params from the request, this is relevant only if
   427     # someone bookmarked a POST page.
   309     # someone bookmarked a POST page.
   428     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   310     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   429     if request.GET.get(self.DEF_SUBMIT_MSG_PARAM_NAME):
   311     if request.GET.get(params['submit_msg_param_name']):
   430       if (not entity) or (not is_self_referrer):
   312       if (not entity) or (not is_self_referrer):
   431         return http.HttpResponseRedirect(request.path)
   313         return http.HttpResponseRedirect(request.path)
   432 
   314 
   433     if entity:
   315     if entity:
   434       # Note: no message will be displayed if parameter is not present
   316       # Note: no message will be displayed if parameter is not present
   435       context['notice'] = helper.requests.getSingleIndexedParamValue(
   317       context['notice'] = helper.requests.getSingleIndexedParamValue(
   436           request, self.DEF_SUBMIT_MSG_PARAM_NAME,
   318           request, params['submit_msg_param_name'],
   437           values=params['save_message'])
   319           values=params['save_message'])
   438 
   320 
   439       # populate form with the existing entity
   321       # populate form with the existing entity
   440       form = params['edit_form'](instance=entity)
   322       form = params['edit_form'](instance=entity)
   441 
   323 
   573 
   455 
   574     self._logic.delete(entity)
   456     self._logic.delete(entity)
   575     redirect = params['delete_redirect']
   457     redirect = params['delete_redirect']
   576 
   458 
   577     return http.HttpResponseRedirect(redirect)
   459     return http.HttpResponseRedirect(redirect)
   578 
       
   579   def getEditRedirect(self, entity, _):
       
   580     """Returns the edit redirect for the specified entity
       
   581     """
       
   582 
       
   583     suffix = self._logic.getKeySuffix(entity)
       
   584     url_name = self._params['url_name']
       
   585     return '/%s/edit/%s' % (url_name, suffix)
       
   586 
   460 
   587   def _editPost(self, request, entity, fields):
   461   def _editPost(self, request, entity, fields):
   588     """Performs any required processing on the entity to post its edit page.
   462     """Performs any required processing on the entity to post its edit page.
   589 
   463 
   590     Args:
   464     Args: