app/soc/views/models/base.py
changeset 858 e79e7a22326f
parent 755 1ed041c0cdc6
child 874 30e9629bf590
equal deleted inserted replaced
857:9767c1afe494 858:e79e7a22326f
    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 access
    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 import sitemap
    36 from soc.views import sitemap
    36 
    37 
    37 import soc.logic
    38 import soc.logic
    38 import soc.logic.lists
    39 import soc.logic.lists
    39 import soc.views.helper.lists
    40 import soc.views.helper.lists
   123     context['entity'] = entity
   124     context['entity'] = entity
   124     context['entity_type'] = params['name']
   125     context['entity_type'] = params['name']
   125 
   126 
   126     template = params['public_template']
   127     template = params['public_template']
   127 
   128 
   128     return helper.responses.respond(request, template, context)
   129     return helper.responses.respond(request, template, context=context)
       
   130 
       
   131   def export(self, request, access_type,
       
   132              page_name=None, params=None, **kwargs):
       
   133     """Displays the export page for the entity specified by **kwargs.
       
   134 
       
   135     Params usage:
       
   136       rights: The rights dictionary is used to check if the user has
       
   137         the required rights to view the export page for this entity.
       
   138         See checkAccess for more details on how the rights dictionary
       
   139         is used to check access rights.
       
   140       error_export: The error_export value is used as template when
       
   141         the key values (as defined by the page's url) do not
       
   142         correspond to an existing entity.
       
   143       name: The name value is used to set the entity_type in the
       
   144         context so that the template can refer to it.
       
   145       export_template: The export_template value is used as template
       
   146         to display the export page of the found entity.
       
   147       export_content_type: The export_content_type value is used to set
       
   148         the Content-Type header of the HTTP response.  If empty (or None),
       
   149         public() is called instead.
       
   150 
       
   151     Args:
       
   152       request: the standard Django HTTP request object
       
   153       page_name: the page name displayed in templates as page and header title
       
   154       params: a dict with params for this View
       
   155       kwargs: the Key Fields for the specified entity
       
   156     """
       
   157     params = dicts.merge(params, self._params)
       
   158 
       
   159     if not params.get('export_content_type'):
       
   160       return self.public(request, access_type, page_name=page_name,
       
   161                          params=params, kwargs=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 
       
   168     # create default template context for use with any templates
       
   169     context = helper.responses.getUniversalContext(request)
       
   170     context['page_name'] = page_name
       
   171     entity = None
       
   172 
       
   173     if not all(kwargs.values()):
       
   174       #TODO: Change this into a proper redirect
       
   175       return http.HttpResponseRedirect('/')
       
   176 
       
   177     try:
       
   178       key_fields = self._logic.getKeyFieldsFromDict(kwargs)
       
   179       entity = self._logic.getIfFields(key_fields)
       
   180     except out_of_band.Error, error:
       
   181       return helper.responses.errorResponse(
       
   182           error, request, template=params['error_export'], context=context)
       
   183 
       
   184     self._export(request, entity, context)
       
   185 
       
   186     context['entity'] = entity
       
   187     context['entity_type'] = params['name']
       
   188 
       
   189     template = params['export_template']
       
   190 
       
   191     response_args = {'mimetype': params['export_content_type']}
       
   192 
       
   193     return helper.responses.respond(request, template, context=context,
       
   194                                     response_args=response_args)
   129 
   195 
   130   def create(self, request, access_type,
   196   def create(self, request, access_type,
   131              page_name=None, params=None, **kwargs):
   197              page_name=None, params=None, **kwargs):
   132     """Displays the create page for this entity type.
   198     """Displays the create page for this entity type.
   133 
   199 
   519     scope = self._params['scope_logic'].logic.getFromKeyName(
   585     scope = self._params['scope_logic'].logic.getFromKeyName(
   520         fields['scope_path'])
   586         fields['scope_path'])
   521     fields['scope'] = scope
   587     fields['scope'] = scope
   522 
   588 
   523   def _public(self, request, entity, context):
   589   def _public(self, request, entity, context):
   524     """Performs any required processing to get an entities public page.
   590     """Performs any required processing to get an entity' public page.
   525 
   591 
   526     Args:
   592     Args:
   527       request: the django request object
   593       request: the django request object
   528       entity: the entity to make public
   594       entity: the entity to make public
   529       context: the context object
   595       context: the context object
   530     """
   596     """
   531 
   597     pass
       
   598 
       
   599   def _export(self, request, entity, context):
       
   600     """Performs any required processing to get an entity's export page.
       
   601 
       
   602     Args:
       
   603       request: the django request object
       
   604       entity: the entity to export
       
   605       context: the context object
       
   606     """
   532     pass
   607     pass
   533 
   608 
   534   def _editGet(self, request, entity, form):
   609   def _editGet(self, request, entity, form):
   535     """Performs any required processing on the form to get its edit page.
   610     """Performs any required processing on the form to get its edit page.
   536 
   611 
   585     context['entity_type'] = params['name']
   660     context['entity_type'] = params['name']
   586     context['entity_type_plural'] = params['name_plural']
   661     context['entity_type_plural'] = params['name_plural']
   587     context['entity_type_short'] = params['name_short']
   662     context['entity_type_short'] = params['name_short']
   588     context['entity_type_url'] = params['url_name']
   663     context['entity_type_url'] = params['url_name']
   589 
   664 
       
   665     if params.get('export_content_type'):
       
   666       context['export_link'] = redirects.getExportRedirect(entity, params)
       
   667 
   590     if entity:
   668     if entity:
   591       template = params['edit_template']
   669       template = params['edit_template']
   592     else:
   670     else:
   593       template = params['create_template']
   671       template = params['create_template']
   594 
   672 
   628       params: a dict with params for this View
   706       params: a dict with params for this View
   629     """
   707     """
   630 
   708 
   631     params = dicts.merge(params, self._params)
   709     params = dicts.merge(params, self._params)
   632     return sitemap.sitemap.getDjangoURLPatterns(params)
   710     return sitemap.sitemap.getDjangoURLPatterns(params)
       
   711