app/soc/views/models/base.py
changeset 455 4d98832b43ba
parent 446 0b479d573a4c
child 456 7781c2836d3c
equal deleted inserted replaced
454:b5d66c883af6 455:4d98832b43ba
    82     new_rights['any_access'] = [access.checkIsUser]
    82     new_rights['any_access'] = [access.checkIsUser]
    83 
    83 
    84     self._rights = dicts.merge(rights, new_rights)
    84     self._rights = dicts.merge(rights, new_rights)
    85     self._params = params
    85     self._params = params
    86 
    86 
    87   def public(self, request, page=None, **kwargs):
    87   def public(self, request, page=None, params=None, **kwargs):
    88     """Displays the public page for the entity specified by **kwargs
    88     """Displays the public page for the entity specified by **kwargs
    89 
    89 
    90     Args:
    90     Args:
    91       request: the standard Django HTTP request object
    91       request: the standard Django HTTP request object
    92       page: a soc.logic.site.page.Page object which is abstraction
    92       page: a soc.logic.site.page.Page object which is abstraction
    93         that combines a Django view with sidebar menu info
    93         that combines a Django view with sidebar menu info
    94       kwargs: the Key Fields for the specified entity
    94       kwargs: the Key Fields for the specified entity
    95     """
    95     """
    96 
    96 
       
    97     params = dicts.merge(params, self._params)
       
    98 
    97     try:
    99     try:
    98       self.checkAccess('public', request)
   100       self.checkAccess('public', request)
    99     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   101     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   100       return alt_response.response()
   102       return alt_response.response()
   101 
   103 
   110 
   112 
   111     try:
   113     try:
   112       key_fields = self._logic.getKeyFieldsFromDict(kwargs)
   114       key_fields = self._logic.getKeyFieldsFromDict(kwargs)
   113       entity = self._logic.getIfFields(key_fields)
   115       entity = self._logic.getIfFields(key_fields)
   114     except soc.logic.out_of_band.ErrorResponse, error:
   116     except soc.logic.out_of_band.ErrorResponse, error:
   115       template = self._params['public_template']
   117       template = params['public_template']
   116       return simple.errorResponse(request, page, error, template, context)
   118       return simple.errorResponse(request, page, error, template, context)
   117 
   119 
   118     self._public(request, entity, context)
   120     self._public(request, entity, context)
   119 
   121 
   120     context['entity'] = entity
   122     context['entity'] = entity
   121     context['entity_type'] = self._params['name']
   123     context['entity_type'] = params['name']
   122 
   124 
   123     template = self._params['public_template']
   125     template = params['public_template']
   124 
   126 
   125     return helper.responses.respond(request, template, context)
   127     return helper.responses.respond(request, template, context)
   126 
   128 
   127   def create(self, request, page=None, **kwargs):
   129   def create(self, request, page=None, params=None, **kwargs):
   128     """Displays the create page for this entity type
   130     """Displays the create page for this entity type
   129 
   131 
   130     Args:
   132     Args:
   131       request: the standard Django HTTP request object
   133       request: the standard Django HTTP request object
   132       page: a soc.logic.site.page.Page object which is abstraction
   134       page: a soc.logic.site.page.Page object which is abstraction
   139     fields = self._logic.getKeyFieldNames()
   141     fields = self._logic.getKeyFieldNames()
   140     for field in fields:
   142     for field in fields:
   141       kwargs[field] = None
   143       kwargs[field] = None
   142 
   144 
   143     # TODO(SRabbelier): make edit strip off 'create' if present and replace with 'edit'
   145     # TODO(SRabbelier): make edit strip off 'create' if present and replace with 'edit'
   144     return self.edit(request, page=page, **kwargs)
   146     return self.edit(request, page=page, params=params, **kwargs)
   145 
   147 
   146   def edit(self, request, page=None, **kwargs):
   148   def edit(self, request, page=None, params=None, **kwargs):
   147     """Displays the public page for the entity specified by **kwargs
   149     """Displays the public page for the entity specified by **kwargs
   148 
   150 
   149     Args:
   151     Args:
   150       request: the standard Django HTTP request object
   152       request: the standard Django HTTP request object
   151       page: a soc.logic.site.page.Page object which is abstraction
   153       page: a soc.logic.site.page.Page object which is abstraction
   152         that combines a Django view with sidebar menu info
   154         that combines a Django view with sidebar menu info
   153       kwargs: The Key Fields for the specified entity
   155       kwargs: The Key Fields for the specified entity
   154     """
   156     """
   155 
   157 
       
   158     params = dicts.merge(params, self._params)
       
   159 
   156     try:
   160     try:
   157       self.checkAccess('edit', request)
   161       self.checkAccess('edit', request)
   158     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   162     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   159       return alt_response.response()
   163       return alt_response.response()
   160 
   164 
   165     try:
   169     try:
   166       if all(kwargs.values()):
   170       if all(kwargs.values()):
   167         key_fields = self._logic.getKeyFieldsFromDict(kwargs)
   171         key_fields = self._logic.getKeyFieldsFromDict(kwargs)
   168         entity = self._logic.getIfFields(key_fields)
   172         entity = self._logic.getIfFields(key_fields)
   169     except soc.logic.out_of_band.ErrorResponse, error:
   173     except soc.logic.out_of_band.ErrorResponse, error:
   170       template = self._params['public_template']
   174       template = params['public_template']
   171       error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
   175       error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
   172           'entity_type_lower' : self._params['name'].lower(),
   176           'entity_type_lower' : params['name'].lower(),
   173           'entity_type' : self._params['name'],
   177           'entity_type' : params['name'],
   174           'create' : self._params['create_redirect']
   178           'create' : params['create_redirect']
   175           }
   179           }
   176       return simple.errorResponse(request, page, error, template, context)
   180       return simple.errorResponse(request, page, error, template, context)
   177 
   181 
   178     if request.method == 'POST':
   182     if request.method == 'POST':
   179       return self.editPost(request, entity, context)
   183       return self.editPost(request, entity, context, params=params)
   180     else:
   184     else:
   181       return self.editGet(request, entity, context)
   185       return self.editGet(request, entity, context, params=params)
   182 
   186 
   183   def editPost(self, request, entity, context):
   187   def editPost(self, request, entity, context, params=None):
   184     """Same as edit, but on POST
   188     """Same as edit, but on POST
   185     """
   189     """
   186 
   190 
       
   191     params = dicts.merge(params, self._params)
       
   192 
   187     if entity:
   193     if entity:
   188       form = self._params['edit_form'](request.POST)
   194       form = params['edit_form'](request.POST)
   189     else:
   195     else:
   190       form = self._params['create_form'](request.POST)
   196       form = params['create_form'](request.POST)
   191 
   197 
   192     if not form.is_valid():
   198     if not form.is_valid():
   193       return self._constructResponse(request, entity, context, form)
   199       return self._constructResponse(request, entity, context, form, params)
   194 
   200 
   195     fields = self.collectCleanedFields(form)
   201     fields = self.collectCleanedFields(form)
   196 
   202 
   197     # get the old_suffix before editing
   203     # get the old_suffix before editing
   198     old_suffix = self._logic.getKeySuffix(entity)
   204     old_suffix = self._logic.getKeySuffix(entity)
   203     entity = self._logic.updateOrCreateFromFields(fields, key_fields)
   209     entity = self._logic.updateOrCreateFromFields(fields, key_fields)
   204 
   210 
   205     if not entity:
   211     if not entity:
   206       return http.HttpResponseRedirect('/')
   212       return http.HttpResponseRedirect('/')
   207 
   213 
   208     params = self._params['edit_params']
   214     page_params = params['edit_params']
   209     suffix = self._logic.getKeySuffix(entity)
   215     suffix = self._logic.getKeySuffix(entity)
   210 
   216 
   211     # redirect to (possibly new) location of the entity
   217     # redirect to (possibly new) location of the entity
   212     # (causes 'Profile saved' message to be displayed)
   218     # (causes 'Profile saved' message to be displayed)
   213     return helper.responses.redirectToChangedSuffix(
   219     return helper.responses.redirectToChangedSuffix(
   214         request, old_suffix, suffix,
   220         request, old_suffix, suffix,
   215         params=params)
   221         params=page_params)
   216 
   222 
   217   def editGet(self, request, entity, context):
   223   def editGet(self, request, entity, context, params=None):
   218     """Same as edit, but on GET
   224     """Same as edit, but on GET
   219     """
   225     """
   220 
   226 
       
   227     params = dicts.merge(params, self._params)
   221     suffix = self._logic.getKeySuffix(entity)
   228     suffix = self._logic.getKeySuffix(entity)
   222 
   229 
   223     # Remove the params from the request, this is relevant only if
   230     # Remove the params from the request, this is relevant only if
   224     # someone bookmarked a POST page.
   231     # someone bookmarked a POST page.
   225     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   232     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   229 
   236 
   230     if entity:
   237     if entity:
   231       # Note: no message will be displayed if parameter is not present
   238       # Note: no message will be displayed if parameter is not present
   232       context['notice'] = helper.requests.getSingleIndexedParamValue(
   239       context['notice'] = helper.requests.getSingleIndexedParamValue(
   233           request, self.DEF_SUBMIT_MSG_PARAM_NAME,
   240           request, self.DEF_SUBMIT_MSG_PARAM_NAME,
   234           values=self._params['save_message'])
   241           values=params['save_message'])
   235 
   242 
   236       # populate form with the existing entity
   243       # populate form with the existing entity
   237       form = self._params['edit_form'](instance=entity)
   244       form = params['edit_form'](instance=entity)
   238       self._editGet(request, entity, form)
   245       self._editGet(request, entity, form)
   239     else:
   246     else:
   240       form = self._params['create_form']()
   247       form = params['create_form']()
   241 
   248 
   242     return self._constructResponse(request, entity, context, form)
   249     return self._constructResponse(request, entity, context, form, params)
   243 
   250 
   244   def list(self, request, page=None):
   251   def list(self, request, page=None, params=None):
   245     """Displays the list page for the entity type
   252     """Displays the list page for the entity type
   246     
   253     
   247     Args:
   254     Args:
   248       request: the standard Django HTTP request object
   255       request: the standard Django HTTP request object
   249       page: a soc.logic.site.page.Page object which is abstraction
   256       page: a soc.logic.site.page.Page object which is abstraction
   250         that combines a Django view with sidebar menu info
   257         that combines a Django view with sidebar menu info
   251     """
   258     """
   252 
   259 
       
   260     params = dicts.merge(params, self._params)
       
   261 
   253     try:
   262     try:
   254       self.checkAccess('list', request)
   263       self.checkAccess('list', request)
   255     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   264     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   256       return alt_response.response()
   265       return alt_response.response()
   257 
   266 
   264     # Fetch one more to see if there should be a 'next' link
   273     # Fetch one more to see if there should be a 'next' link
   265     entities = self._logic.getForLimitAndOffset(limit + 1, offset=offset)
   274     entities = self._logic.getForLimitAndOffset(limit + 1, offset=offset)
   266 
   275 
   267     context['pagination_form'] = helper.lists.makePaginationForm(request, limit)
   276     context['pagination_form'] = helper.lists.makePaginationForm(request, limit)
   268 
   277 
   269     templates = self._params['lists_template']
   278     templates = params['lists_template']
   270 
   279 
   271     context = helper.lists.setList(request, context, entities, 
   280     context = helper.lists.setList(request, context, entities, 
   272                                  offset, limit, templates)
   281                                  offset, limit, templates)
   273 
   282 
   274     context['entity_type'] = self._params['name']
   283     context['entity_type'] = params['name']
   275     context['entity_type_plural'] = self._params['name_plural']
   284     context['entity_type_plural'] = params['name_plural']
   276 
   285 
   277     template = self._params['list_template']
   286     template = params['list_template']
   278 
   287 
   279     return helper.responses.respond(request, template, context)
   288     return helper.responses.respond(request, template, context)
   280 
   289 
   281   def delete(self, request, page=None, **kwargs):
   290   def delete(self, request, page=None, params=None, **kwargs):
   282     """Shows the delete page for the entity specified by kwargs
   291     """Shows the delete page for the entity specified by kwargs
   283 
   292 
   284     Args:
   293     Args:
   285       request: the standard Django HTTP request object
   294       request: the standard Django HTTP request object
   286       page: a soc.logic.site.page.Page object which is abstraction
   295       page: a soc.logic.site.page.Page object which is abstraction
   287         that combines a Django view with sidebar menu info
   296         that combines a Django view with sidebar menu info
   288       kwargs: The Key Fields for the specified entity
   297       kwargs: The Key Fields for the specified entity
   289     """
   298     """
   290 
   299 
       
   300     params = dicts.merge(params, self._params)
       
   301 
   291     try:
   302     try:
   292       self.checkAccess('delete', request)
   303       self.checkAccess('delete', request)
   293     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   304     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   294       return alt_response.response()
   305       return alt_response.response()
   295 
   306 
   300 
   311 
   301     try:
   312     try:
   302       key_fields = self._logic.getKeyFieldsFromDict(kwargs)
   313       key_fields = self._logic.getKeyFieldsFromDict(kwargs)
   303       entity = self._logic.getIfFields(key_fields)
   314       entity = self._logic.getIfFields(key_fields)
   304     except soc.logic.out_of_band.ErrorResponse, error:
   315     except soc.logic.out_of_band.ErrorResponse, error:
   305       template = self._params['edit_template']
   316       template = params['edit_template']
   306       error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
   317       error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
   307           'entity_type_lower' : self._params['name'].lower(),
   318           'entity_type_lower' : params['name'].lower(),
   308           'entity_type' : self._params['name'],
   319           'entity_type' : params['name'],
   309           'create' : self._params['create_redirect']
   320           'create' : params['create_redirect']
   310           }
   321           }
   311       return simple.errorResponse(request, page, error, template, context)
   322       return simple.errorResponse(request, page, error, template, context)
   312 
   323 
   313     if not entity:
   324     if not entity:
   314       #TODO: Create a proper error page for this
   325       #TODO: Create a proper error page for this
   318       # TODO: Update the notice area telling the user that they
   329       # TODO: Update the notice area telling the user that they
   319       # can't delete the entity
   330       # can't delete the entity
   320       pass
   331       pass
   321 
   332 
   322     self._logic.delete(entity)
   333     self._logic.delete(entity)
   323     redirect = self._params['delete_redirect']
   334     redirect = params['delete_redirect']
   324 
   335 
   325     return http.HttpResponseRedirect(redirect)
   336     return http.HttpResponseRedirect(redirect)
   326 
   337 
   327   def _editPost(self, request, entity, fields):
   338   def _editPost(self, request, entity, fields):
   328     """Performs any required processing on the entity to post its edit page
   339     """Performs any required processing on the entity to post its edit page
   365                    not present in the _rights dictionary when checking.
   376                    not present in the _rights dictionary when checking.
   366     """
   377     """
   367 
   378 
   368     pass
   379     pass
   369 
   380 
   370   def _constructResponse(self, request, entity, context, form):
   381   def _constructResponse(self, request, entity, context, form, params):
   371     """Updates the context and returns a response for the specified arguments
   382     """Updates the context and returns a response for the specified arguments
   372 
   383 
   373     Args:
   384     Args:
   374       request: the django request object
   385       request: the django request object
   375       entity: the entity that is used
   386       entity: the entity that is used
   380     suffix = self._logic.getKeySuffix(entity)
   391     suffix = self._logic.getKeySuffix(entity)
   381 
   392 
   382     context['form'] = form
   393     context['form'] = form
   383     context['entity'] = entity
   394     context['entity'] = entity
   384     context['entity_suffix'] = suffix
   395     context['entity_suffix'] = suffix
   385     context['entity_type'] = self._params['name']
   396     context['entity_type'] = params['name']
   386     context['entity_type_plural'] = self._params['name_plural']
   397     context['entity_type_plural'] = params['name_plural']
   387     context['entity_type_short'] = self._params['name_short']
   398     context['entity_type_short'] = params['name_short']
   388 
   399 
   389     template = self._params['edit_template']
   400     template = params['edit_template']
   390 
   401 
   391     return helper.responses.respond(request, template, context)
   402     return helper.responses.respond(request, template, context)
   392 
   403 
   393   def checkAccess(self, access_type, request):
   404   def checkAccess(self, access_type, request):
   394     """Runs all the defined checks for the specified type
   405     """Runs all the defined checks for the specified type