app/soc/views/models/base.py
changeset 373 dcd7013ae0d5
parent 363 d35ffa6ca643
child 387 c55195361cb6
equal deleted inserted replaced
372:8595c1129c74 373:dcd7013ae0d5
    45 
    45 
    46   The View class functions specific to Entity classes by relying
    46   The View class functions specific to Entity classes by relying
    47   on the the child-classes to define the following fields:
    47   on the the child-classes to define the following fields:
    48 
    48 
    49   self._logic: the logic singleton for this entity
    49   self._logic: the logic singleton for this entity
    50 
       
    51   Args:
       
    52     rights: This dictionary should be filled with the access check
       
    53             functions that should be called
       
    54 
       
    55     params: This dictionary should be filled with the parameters
       
    56             specific to this entity.
       
    57   """
    50   """
    58 
    51 
    59   def __init__(self, params=None, rights=None):
    52   def __init__(self, params=None, rights=None):
    60     """
    53     """
       
    54 
       
    55     Args:
       
    56       rights: This dictionary should be filled with the access check
       
    57         functions that should be called
       
    58       params: This dictionary should be filled with the parameters
       
    59         specific to this entity.
    61     """
    60     """
    62 
    61 
    63     new_rights = {}
    62     new_rights = {}
    64     new_rights['base'] = [access.checkIsLoggedIn]
    63     new_rights['base'] = [access.checkIsLoggedIn]
    65 
    64 
    71     self.DEF_CREATE_NEW_ENTITY_MSG = ugettext_lazy(
    70     self.DEF_CREATE_NEW_ENTITY_MSG = ugettext_lazy(
    72         ' You can create a new %(model_type)s by visiting'
    71         ' You can create a new %(model_type)s by visiting'
    73         ' <a href="%(create)s">Create '
    72         ' <a href="%(create)s">Create '
    74         'a New %(Type)s</a> page.')
    73         'a New %(Type)s</a> page.')
    75 
    74 
    76   def public(self, request, **kwargs):
    75   def public(self, request, page=None, **kwargs):
    77     """Displays the public page for the entity specified by **kwargs
    76     """Displays the public page for the entity specified by **kwargs
    78 
    77 
    79     Args:
    78     Args:
    80       request: the Django request object
    79       request: the standard Django HTTP request object
       
    80       page: a soc.logic.site.page.Page object which is abstraction
       
    81         that combines a Django view with sidebar menu info
    81       kwargs: the Key Fields for the specified entity
    82       kwargs: the Key Fields for the specified entity
    82     """
    83     """
    83 
    84 
    84     try:
    85     try:
    85       self.checkAccess('edit', request)
    86       self.checkAccess('edit', request)
    86     except soc.views.out_of_band.AccessViolationResponse, alt_response:
    87     except soc.views.out_of_band.AccessViolationResponse, alt_response:
    87       return alt_response.response()
    88       return alt_response.response()
    88 
    89 
    89     # create default template context for use with any templates
    90     # create default template context for use with any templates
    90     context = helper.responses.getUniversalContext(request)
    91     context = helper.responses.getUniversalContext(request)
       
    92     context['page'] = page
    91     entity = None
    93     entity = None
    92 
    94 
    93     try:
    95     try:
    94       entity = self._logic.getIfFields(**kwargs)
    96       entity = self._logic.getIfFields(**kwargs)
    95     except soc.logic.out_of_band.ErrorResponse, error:
    97     except soc.logic.out_of_band.ErrorResponse, error:
   107 
   109 
   108     template = self._params['public_template']
   110     template = self._params['public_template']
   109 
   111 
   110     return helper.responses.respond(request, template, context)
   112     return helper.responses.respond(request, template, context)
   111 
   113 
   112   def create(self, request, **kwargs):
   114   def create(self, request, page=None, **kwargs):
   113     """Displays the create page for this entity type
   115     """Displays the create page for this entity type
   114 
   116 
   115     request: the django request object
   117     Args:
   116     kwargs: not used
   118       request: the standard Django HTTP request object
       
   119       page: a soc.logic.site.page.Page object which is abstraction
       
   120         that combines a Django view with sidebar menu info
       
   121       kwargs: not used for create()
   117     """
   122     """
   118 
   123 
   119     # Create page is an edit page with no key fields
   124     # Create page is an edit page with no key fields
   120     kwargs = {}
   125     kwargs = {}
   121     return self.edit(request, **kwargs)
   126     return self.edit(request, page=page, **kwargs)
   122 
   127 
   123   def edit(self, request, **kwargs):
   128   def edit(self, request, page=None, **kwargs):
   124     """Displays the public page for the entity specified by **kwargs
   129     """Displays the public page for the entity specified by **kwargs
   125 
   130 
   126     Args:
   131     Args:
   127       request: The Django request object
   132       request: the standard Django HTTP request object
       
   133       page: a soc.logic.site.page.Page object which is abstraction
       
   134         that combines a Django view with sidebar menu info
   128       kwargs: The Key Fields for the specified entity
   135       kwargs: The Key Fields for the specified entity
   129     """
   136     """
   130 
   137 
   131     try:
   138     try:
   132       self.checkAccess('edit', request)
   139       self.checkAccess('edit', request)
   133     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   140     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   134       return alt_response.response()
   141       return alt_response.response()
   135 
   142 
       
   143     context = helper.responses.getUniversalContext(request)
       
   144     context['page'] = page
   136     entity = None
   145     entity = None
   137 
   146 
   138     try:
   147     try:
   139       entity = self._logic.getIfFields(**kwargs)
   148       entity = self._logic.getIfFields(**kwargs)
   140     except soc.logic.out_of_band.ErrorResponse, error:
   149     except soc.logic.out_of_band.ErrorResponse, error:
   145           'create' : self._redirects['create']
   154           'create' : self._redirects['create']
   146           }
   155           }
   147       return simple.errorResponse(request, error, template, context)
   156       return simple.errorResponse(request, error, template, context)
   148 
   157 
   149     if request.method == 'POST':
   158     if request.method == 'POST':
   150       return self.editPost(request, entity)
   159       return self.editPost(request, entity, context)
   151     else:
   160     else:
   152       return self.editGet(request, entity)
   161       return self.editGet(request, entity, context)
   153 
   162 
   154   def editPost(self, request, entity):
   163   def editPost(self, request, entity, context):
   155     """Same as edit, but on POST
   164     """Same as edit, but on POST
   156     """
   165     """
   157 
       
   158     context = helper.responses.getUniversalContext(request)
       
   159 
       
   160     if entity:
   166     if entity:
   161       form = self._params['edit_form'](request.POST)
   167       form = self._params['edit_form'](request.POST)
   162     else:
   168     else:
   163       form = self._params['create_form'](request.POST)
   169       form = self._params['create_form'](request.POST)
   164 
   170 
   183     # (causes 'Profile saved' message to be displayed)
   189     # (causes 'Profile saved' message to be displayed)
   184     return helper.responses.redirectToChangedSuffix(
   190     return helper.responses.redirectToChangedSuffix(
   185         request, None, suffix,
   191         request, None, suffix,
   186         params=params)
   192         params=params)
   187 
   193 
   188   def editGet(self, request, entity):
   194   def editGet(self, request, entity, context):
   189     """Same as edit, but on GET
   195     """Same as edit, but on GET
   190     """
   196     """
   191 
       
   192     context = helper.responses.getUniversalContext(request)
       
   193     #TODO(SRabbelier) Construct a suffix
   197     #TODO(SRabbelier) Construct a suffix
   194     suffix = None
   198     suffix = None
   195     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   199     is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
   196 
   200 
   197     # Remove the params from the request, this is relevant only if
   201     # Remove the params from the request, this is relevant only if
   219 
   223 
   220     template = self._params['create_template']
   224     template = self._params['create_template']
   221 
   225 
   222     return helper.responses.respond(request, template, context)
   226     return helper.responses.respond(request, template, context)
   223 
   227 
   224   def list(self, request):
   228   def list(self, request, page=None):
   225     """Displays the list page for the entity type
   229     """Displays the list page for the entity type
       
   230     
       
   231     Args:
       
   232       request: the standard Django HTTP request object
       
   233       page: a soc.logic.site.page.Page object which is abstraction
       
   234         that combines a Django view with sidebar menu info
   226     """
   235     """
   227 
   236 
   228     try:
   237     try:
   229       self.checkAccess('list', request)
   238       self.checkAccess('list', request)
   230     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   239     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   231       return alt_response.response()
   240       return alt_response.response()
   232 
   241 
   233     context = helper.responses.getUniversalContext(request)
   242     context = helper.responses.getUniversalContext(request)
       
   243     context['page'] = page
   234 
   244 
   235     offset, limit = helper.lists.cleanListParameters(
   245     offset, limit = helper.lists.cleanListParameters(
   236       offset=request.GET.get('offset'), limit=request.GET.get('limit'))
   246       offset=request.GET.get('offset'), limit=request.GET.get('limit'))
   237 
   247 
   238     # Fetch one more to see if there should be a 'next' link
   248     # Fetch one more to see if there should be a 'next' link
   250 
   260 
   251     template = self._params['list_template']
   261     template = self._params['list_template']
   252 
   262 
   253     return helper.responses.respond(request, template, context)
   263     return helper.responses.respond(request, template, context)
   254 
   264 
   255   def delete(self, request, **kwargs):
   265   def delete(self, request, page=None, **kwargs):
   256     """Shows the delete page for the entity specified by kwargs
   266     """Shows the delete page for the entity specified by kwargs
   257 
   267 
   258     Args:
   268     Args:
   259       request: The Django request object
   269       request: the standard Django HTTP request object
       
   270       page: a soc.logic.site.page.Page object which is abstraction
       
   271         that combines a Django view with sidebar menu info
   260       kwargs: The Key Fields for the specified entity
   272       kwargs: The Key Fields for the specified entity
   261     """
   273     """
   262 
   274 
   263     try:
   275     try:
   264       self.checkAccess('delete', request)
   276       self.checkAccess('delete', request)
   265     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   277     except soc.views.out_of_band.AccessViolationResponse, alt_response:
   266       return alt_response.response()
   278       return alt_response.response()
   267 
   279 
   268     # create default template context for use with any templates
   280     # create default template context for use with any templates
   269     context = helper.responses.getUniversalContext(request)
   281     context = helper.responses.getUniversalContext(request)
       
   282     context['page'] = page
   270     entity = None
   283     entity = None
   271 
   284 
   272     try:
   285     try:
   273       entity = models.sponsor.logic.getIfFields(**kwargs)
   286       entity = models.sponsor.logic.getIfFields(**kwargs)
   274     except soc.logic.out_of_band.ErrorResponse, error:
   287     except soc.logic.out_of_band.ErrorResponse, error: