app/soc/views/models/base.py
changeset 876 0c1329d4b514
parent 874 30e9629bf590
child 880 d67557c612de
equal deleted inserted replaced
875:03c674f510d8 876:0c1329d4b514
    28 from django.utils.translation import ugettext_lazy
    28 from django.utils.translation import ugettext_lazy
    29 
    29 
    30 from soc.logic import dicts
    30 from soc.logic import dicts
    31 from soc.views import helper
    31 from soc.views import helper
    32 from soc.views import out_of_band
    32 from soc.views import out_of_band
    33 from soc.views.helper import access
    33 from soc.views.helper import decorators
    34 from soc.views.helper import forms
    34 from soc.views.helper import forms
    35 from soc.views.helper import redirects
    35 from soc.views.helper import redirects
    36 from soc.views import sitemap
    36 from soc.views import sitemap
    37 
    37 
    38 import soc.logic
    38 import soc.logic
    69     """
    69     """
    70 
    70 
    71     self._params = helper.params.constructParams(params)
    71     self._params = helper.params.constructParams(params)
    72     self._logic = params['logic']
    72     self._logic = params['logic']
    73 
    73 
    74 
    74   @decorators.merge_params
       
    75   @decorators.check_access
    75   def public(self, request, access_type,
    76   def public(self, request, access_type,
    76              page_name=None, params=None, **kwargs):
    77              page_name=None, params=None, **kwargs):
    77     """Displays the public page for the entity specified by **kwargs.
    78     """Displays the public page for the entity specified by **kwargs.
    78 
    79 
    79     Params usage:
    80     Params usage:
    94       page_name: the page name displayed in templates as page and header title
    95       page_name: the page name displayed in templates as page and header title
    95       params: a dict with params for this View
    96       params: a dict with params for this View
    96       kwargs: the Key Fields for the specified entity
    97       kwargs: the Key Fields for the specified entity
    97     """
    98     """
    98 
    99 
    99     params = dicts.merge(params, self._params)
       
   100 
       
   101     try:
       
   102       access.checkAccess(access_type, request, rights=params['rights'])
       
   103     except out_of_band.Error, error:
       
   104       return helper.responses.errorResponse(error, request)
       
   105 
       
   106     # create default template context for use with any templates
   100     # create default template context for use with any templates
   107     context = helper.responses.getUniversalContext(request)
   101     context = helper.responses.getUniversalContext(request)
   108     context['page_name'] = page_name
   102     context['page_name'] = page_name
   109     entity = None
   103     entity = None
   110 
   104 
   126 
   120 
   127     template = params['public_template']
   121     template = params['public_template']
   128 
   122 
   129     return helper.responses.respond(request, template, context=context)
   123     return helper.responses.respond(request, template, context=context)
   130 
   124 
       
   125   @decorators.merge_params
       
   126   @decorators.check_access
   131   def export(self, request, access_type,
   127   def export(self, request, access_type,
   132              page_name=None, params=None, **kwargs):
   128              page_name=None, params=None, **kwargs):
   133     """Displays the export page for the entity specified by **kwargs.
   129     """Displays the export page for the entity specified by **kwargs.
   134 
   130 
   135     Params usage:
   131     Params usage:
   152       request: the standard Django HTTP request object
   148       request: the standard Django HTTP request object
   153       page_name: the page name displayed in templates as page and header title
   149       page_name: the page name displayed in templates as page and header title
   154       params: a dict with params for this View
   150       params: a dict with params for this View
   155       kwargs: the Key Fields for the specified entity
   151       kwargs: the Key Fields for the specified entity
   156     """
   152     """
   157     params = dicts.merge(params, self._params)
       
   158 
   153 
   159     if not params.get('export_content_type'):
   154     if not params.get('export_content_type'):
   160       return self.public(request, access_type, page_name=page_name,
   155       return self.public(request, access_type, page_name=page_name,
   161                          params=params, **kwargs)
   156                          params=params, **kwargs)
   162 
       
   163     try:
       
   164       access.checkAccess(access_type, request, rights=params['rights'])
       
   165     except out_of_band.Error, error:
       
   166       return helper.responses.errorResponse(error, request)
       
   167 
   157 
   168     # create default template context for use with any templates
   158     # create default template context for use with any templates
   169     context = helper.responses.getUniversalContext(request)
   159     context = helper.responses.getUniversalContext(request)
   170     context['page_name'] = page_name
   160     context['page_name'] = page_name
   171     entity = None
   161     entity = None
   191     response_args = {'mimetype': params['export_content_type']}
   181     response_args = {'mimetype': params['export_content_type']}
   192 
   182 
   193     return helper.responses.respond(request, template, context=context,
   183     return helper.responses.respond(request, template, context=context,
   194                                     response_args=response_args)
   184                                     response_args=response_args)
   195 
   185 
       
   186   @decorators.check_access
   196   def create(self, request, access_type,
   187   def create(self, request, access_type,
   197              page_name=None, params=None, **kwargs):
   188              page_name=None, params=None, **kwargs):
   198     """Displays the create page for this entity type.
   189     """Displays the create page for this entity type.
   199 
   190 
   200     Params usage:
   191     Params usage:
   225       empty_kwargs[field] = None
   216       empty_kwargs[field] = None
   226 
   217 
   227     return self.edit(request, access_type, page_name=page_name,
   218     return self.edit(request, access_type, page_name=page_name,
   228                      params=params, seed=kwargs, **empty_kwargs)
   219                      params=params, seed=kwargs, **empty_kwargs)
   229 
   220 
       
   221   @decorators.merge_params
       
   222   @decorators.check_access
   230   def edit(self, request, access_type,
   223   def edit(self, request, access_type,
   231            page_name=None, params=None, seed=None, **kwargs):
   224            page_name=None, params=None, seed=None, **kwargs):
   232     """Displays the edit page for the entity specified by **kwargs.
   225     """Displays the edit page for the entity specified by **kwargs.
   233 
   226 
   234     Params usage:
   227     Params usage:
   253       request: the standard Django HTTP request object
   246       request: the standard Django HTTP request object
   254       page_name: the page name displayed in templates as page and header title
   247       page_name: the page name displayed in templates as page and header title
   255       params: a dict with params for this View
   248       params: a dict with params for this View
   256       kwargs: The Key Fields for the specified entity
   249       kwargs: The Key Fields for the specified entity
   257     """
   250     """
   258 
       
   259     params = dicts.merge(params, self._params)
       
   260 
       
   261     try:
       
   262       access.checkAccess(access_type, request, rights=params['rights'])
       
   263     except out_of_band.Error, error:
       
   264       return helper.responses.errorResponse(error, request)
       
   265 
   251 
   266     context = helper.responses.getUniversalContext(request)
   252     context = helper.responses.getUniversalContext(request)
   267     context['page_name'] = page_name
   253     context['page_name'] = page_name
   268     entity = None
   254     entity = None
   269 
   255 
   280             'create' : params['missing_redirect']})
   266             'create' : params['missing_redirect']})
   281         return helper.responses.errorResponse(
   267         return helper.responses.errorResponse(
   282             error, request, template=params['error_public'], context=context)
   268             error, request, template=params['error_public'], context=context)
   283 
   269 
   284     if request.method == 'POST':
   270     if request.method == 'POST':
   285       return self.editPost(request, entity, context, params)
   271       return self.editPost(request, entity, context, params=params)
   286     else:
   272     else:
   287       return self.editGet(request, entity, context, seed, params)
   273       return self.editGet(request, entity, context, seed, params=params)
   288 
   274 
   289   def editPost(self, request, entity, context, params):
   275   @decorators.merge_params
       
   276   def editPost(self, request, entity, context, params=None):
   290     """Processes POST requests for the specified entity.
   277     """Processes POST requests for the specified entity.
   291 
   278 
   292     Params usage:
   279     Params usage:
   293       The params dictionary is passed to _constructResponse when the
   280       The params dictionary is passed to _constructResponse when the
   294       form is not valid (see edit_form and create_form below). See
   281       form is not valid (see edit_form and create_form below). See
   310 
   297 
   311     Args:
   298     Args:
   312       request: a django request object
   299       request: a django request object
   313       entity: the entity that will be modified or created, may be None
   300       entity: the entity that will be modified or created, may be None
   314       context: the context dictionary that will be provided to Django
   301       context: the context dictionary that will be provided to Django
   315       params: a dict with params for this View
   302       params: required, a dict with params for this View
   316     """
   303     """
   317 
       
   318     params = dicts.merge(params, self._params)
       
   319 
   304 
   320     if entity:
   305     if entity:
   321       form = params['edit_form'](request.POST)
   306       form = params['edit_form'](request.POST)
   322     else:
   307     else:
   323       form = params['create_form'](request.POST)
   308       form = params['create_form'](request.POST)
   346     # redirect to (possibly new) location of the entity
   331     # redirect to (possibly new) location of the entity
   347     # (causes 'Profile saved' message to be displayed)
   332     # (causes 'Profile saved' message to be displayed)
   348     return helper.responses.redirectToChangedSuffix(
   333     return helper.responses.redirectToChangedSuffix(
   349         request, None, params=page_params)
   334         request, None, params=page_params)
   350 
   335 
   351   def editGet(self, request, entity, context, seed, params):
   336   @decorators.merge_params
       
   337   def editGet(self, request, entity, context, seed, params=None):
   352     """Processes GET requests for the specified entity.
   338     """Processes GET requests for the specified entity.
   353 
   339 
   354     Params usage:
   340     Params usage:
   355       The params dictionary is passed to _constructResponse, see the
   341       The params dictionary is passed to _constructResponse, see the
   356       docstring  of _constructResponse on how it uses it.
   342       docstring  of _constructResponse on how it uses it.
   374     Args:
   360     Args:
   375       request: the django request object
   361       request: the django request object
   376       entity: the entity that will be edited, may be None
   362       entity: the entity that will be edited, may be None
   377       context: the context dictionary that will be provided to django
   363       context: the context dictionary that will be provided to django
   378       seed: if no entity is provided, the initial values for the new entity
   364       seed: if no entity is provided, the initial values for the new entity
   379       params: a dict with paras for this View
   365       params: required, a dict with params for this View
   380     """
   366     """
   381 
   367 
   382     params = dicts.merge(params, self._params)
       
   383     suffix = self._logic.getKeySuffix(entity)
   368     suffix = self._logic.getKeySuffix(entity)
   384 
   369 
   385     # Remove the params from the request, this is relevant only if
   370     # Remove the params from the request, this is relevant only if
   386     # someone bookmarked a POST page.
   371     # someone bookmarked a POST page.
   387     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   372     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   409       else:
   394       else:
   410         form = params['create_form']()
   395         form = params['create_form']()
   411 
   396 
   412     return self._constructResponse(request, entity, context, form, params)
   397     return self._constructResponse(request, entity, context, form, params)
   413 
   398 
       
   399   @decorators.merge_params
       
   400   @decorators.check_access
   414   def list(self, request, access_type,
   401   def list(self, request, access_type,
   415            page_name=None, params=None, filter=None):
   402            page_name=None, params=None, filter=None):
   416     """Displays the list page for the entity type.
   403     """Displays the list page for the entity type.
   417     
   404     
   418     Args:
   405     Args:
   424     Params usage:
   411     Params usage:
   425       The params dictionary is passed as argument to getListContent in
   412       The params dictionary is passed as argument to getListContent in
   426       the soc.views.helper.list module. See the docstring for getListContent 
   413       the soc.views.helper.list module. See the docstring for getListContent 
   427       on how it uses it. The params dictionary is also passed as argument to 
   414       on how it uses it. The params dictionary is also passed as argument to 
   428       the _list method. See the docstring for _list on how it uses it.
   415       the _list method. See the docstring for _list on how it uses it.
   429 
   416     """
   430       rights: The rights dictionary is used to check if the user has
       
   431         the required rights to list all entities of this View's type.
       
   432         See checkAccess for more details on how the rights dictionary
       
   433         is used to check access rights.
       
   434     """
       
   435 
       
   436     params = dicts.merge(params, self._params)
       
   437 
       
   438     try:
       
   439       access.checkAccess(access_type, request, rights=params['rights'])
       
   440     except out_of_band.Error, error:
       
   441       return helper.responses.errorResponse(error, request)
       
   442 
   417 
   443     content = helper.lists.getListContent(request, params, filter)
   418     content = helper.lists.getListContent(request, params, filter)
   444     contents = [content]
   419     contents = [content]
   445 
   420 
   446     return self._list(request, params, contents, page_name)
   421     return self._list(request, params, contents, page_name)
   473 
   448 
   474     template = params['list_template']
   449     template = params['list_template']
   475 
   450 
   476     return helper.responses.respond(request, template, context)
   451     return helper.responses.respond(request, template, context)
   477 
   452 
       
   453   @decorators.merge_params
       
   454   @decorators.check_access
   478   def delete(self, request, access_type,
   455   def delete(self, request, access_type,
   479              page_name=None, params=None, **kwargs):
   456              page_name=None, params=None, **kwargs):
   480     """Shows the delete page for the entity specified by **kwargs.
   457     """Shows the delete page for the entity specified by **kwargs.
   481 
   458 
   482     Args:
   459     Args:
   495       missing_redirect: see name
   472       missing_redirect: see name
   496       error_edit: see name
   473       error_edit: see name
   497       delete_redirect: The delete_redirect value is used as the url to
   474       delete_redirect: The delete_redirect value is used as the url to
   498         redirect to after having successfully deleted the entity.
   475         redirect to after having successfully deleted the entity.
   499     """
   476     """
   500 
       
   501     params = dicts.merge(params, self._params)
       
   502 
       
   503     try:
       
   504       access.checkAccess(access_type, request, rights=params['rights'])
       
   505     except out_of_band.Error, error:
       
   506       return helper.responses.errorResponse(error, request)
       
   507 
   477 
   508     # create default template context for use with any templates
   478     # create default template context for use with any templates
   509     context = helper.responses.getUniversalContext(request)
   479     context = helper.responses.getUniversalContext(request)
   510     context['page_name'] = page_name
   480     context['page_name'] = page_name
   511     entity = None
   481     entity = None
   537     return http.HttpResponseRedirect(redirect)
   507     return http.HttpResponseRedirect(redirect)
   538 
   508 
   539   def select(self, request, view, redirect, page_name=None, params=None):
   509   def select(self, request, view, redirect, page_name=None, params=None):
   540     """Displays a list page allowing the user to select an entity.
   510     """Displays a list page allowing the user to select an entity.
   541 
   511 
   542     After having selected the Sponsor, the user is redirected to the
   512     After having selected the Scope, the user is redirected to the
   543     'create a new program' page with the scope_path set appropriately.
   513     'create a new entity' page with the scope_path set appropriately.
   544 
   514 
   545     Params usage:
   515     Params usage:
   546       The params dictionary is also passed to getListContent from
   516       The params dictionary is also passed to getListContent from
   547         the helper.list module, please refer to its docstring also.
   517         the helper.list module, please refer to its docstring also.
   548       The params dictionary is passed to self._list as well, refer
   518       The params dictionary is passed to self._list as well, refer
   676     """Returns this view's params attribute.
   646     """Returns this view's params attribute.
   677     """
   647     """
   678 
   648 
   679     return self._params
   649     return self._params
   680 
   650 
       
   651   @decorators.merge_params
   681   def getSidebarMenus(self, request, params=None):
   652   def getSidebarMenus(self, request, params=None):
   682     """Returns an dictionary with one sidebar entry.
   653     """Returns an dictionary with one sidebar entry.
   683 
   654 
   684     Args:
   655     Args:
   685       request: the django request object
   656       request: the django request object
   689       The params dictionary is passed as argument to getSidebarItems
   660       The params dictionary is passed as argument to getSidebarItems
   690       from the soc.views.sitemap.sidebar module, see the docstring
   661       from the soc.views.sitemap.sidebar module, see the docstring
   691       of _getSidebarItems on how it uses it.
   662       of _getSidebarItems on how it uses it.
   692     """
   663     """
   693 
   664 
   694     params = dicts.merge(params, self._params)
       
   695     return sitemap.sidebar.getSidebarMenus(request, params)
   665     return sitemap.sidebar.getSidebarMenus(request, params)
   696 
   666 
       
   667   @decorators.merge_params
   697   def getDjangoURLPatterns(self, params=None):
   668   def getDjangoURLPatterns(self, params=None):
   698     """Retrieves a list of sidebar entries for this view
   669     """Retrieves a list of sidebar entries for this view
   699 
   670 
   700     Params usage:
   671     Params usage:
   701       The params dictionary is passed to the getDjangoURLPatterns
   672       The params dictionary is passed to the getDjangoURLPatterns
   704 
   675 
   705     Args:
   676     Args:
   706       params: a dict with params for this View
   677       params: a dict with params for this View
   707     """
   678     """
   708 
   679 
   709     params = dicts.merge(params, self._params)
       
   710     return sitemap.sitemap.getDjangoURLPatterns(params)
   680     return sitemap.sitemap.getDjangoURLPatterns(params)
   711 
   681