app/soc/views/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Mon, 03 Nov 2008 19:47:02 +0000
changeset 439 0658c3c9a9dc
parent 438 af082c92ddda
child 442 92c17629af0e
permissions -rw-r--r--
Minor fixes needed for generic key name We no longer try to retreive an entity when there are unset fields. This sort of makes 'getIfFields' obsolete, since we check if fields now anyway. This is needed because getKeyFieldsFromDict expects the fields to be set. Also a minor fix in a Django template so that the generic 'edit' page has a working delete button again.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
# 
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
# 
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Helpers functions for displaying views.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 428
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from django import http
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
from django.utils.translation import ugettext_lazy
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
import soc.logic
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
import soc.logic.out_of_band
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
import soc.views.helper.lists
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
import soc.views.helper.responses
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
import soc.views.out_of_band
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    34
from soc.logic import dicts
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
from soc.logic import models
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
from soc.views import simple
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
from soc.views import helper
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
from soc.views.helper import access
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
class View:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
  """Views for entity classes.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
  The View class functions specific to Entity classes by relying
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
  on the the child-classes to define the following fields:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
  self._logic: the logic singleton for this entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
  """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    50
  DEF_SUBMIT_MSG_PARAM_NAME = 's'
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    51
  DEF_SUBMIT_MSG_PROFILE_SAVED = 0
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    52
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    53
  DEF_CREATE_NEW_ENTITY_MSG = ugettext_lazy(
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    54
      ' You can create a new %(entity_type_lower)s by visiting'
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    55
      ' <a href="%(create)s">Create '
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    56
      'a New %(entity_type)s</a> page.')
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    57
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
  def __init__(self, params=None, rights=None):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
    """
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    60
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    61
    Args:
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    62
      rights: This dictionary should be filled with the access check
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    63
        functions that should be called, it will be modified in-place.
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    64
      params: This dictionary should be filled with the parameters
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    65
        specific to this entity, required fields are:
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    66
        name: the name of the entity (names should have sentence-style caps) 
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    67
        name_short: the short form name of the name ('org' vs 'organization')
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    68
        name_plural: the plural form of the name
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    69
        edit_form: the class of the Django form to be used when editing
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    70
        create_form: the class of the Django form to be used when creating
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    71
        edit_template: the Django template to be used for editing
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    72
        public_template: the Django template to be used as public page 
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    73
        list_template: the Django template to be used as list page
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    74
        lists_template: the Django templates to search for the list page
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    75
        delete_redirect: the Django template to redirect to on delete
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    76
        create_redirect: the Django template to redirect to after creation
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    77
        save_message: the message to display when the entity is saved
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    78
        edit_params: the params to use when editing
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
    new_rights = {}
418
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
    82
    new_rights['any_access'] = [access.checkIsUser]
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    84
    self._rights = dicts.merge(rights, new_rights)
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    85
    self._params = params
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    87
  def public(self, request, page=None, **kwargs):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    """Displays the public page for the entity specified by **kwargs
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
    Args:
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    91
      request: the standard Django HTTP request object
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    92
      page: a soc.logic.site.page.Page object which is abstraction
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    93
        that combines a Django view with sidebar menu info
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
      kwargs: the Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
    try:
428
e0d5de282004 Fixed typo in access check
Sverre Rabbelier <srabbelier@gmail.com>
parents: 418
diff changeset
    98
      self.checkAccess('public', request)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
      return alt_response.response()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
    # create default template context for use with any templates
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
    context = helper.responses.getUniversalContext(request)
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   104
    context['page'] = page
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
    try:
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   108
      key_fields = self._logic.getKeyFieldsFromDict(kwargs)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   109
      entity = self._logic.getIfFields(key_fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
    except soc.logic.out_of_band.ErrorResponse, error:
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   111
      template = self._params['public_template']
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   112
      return simple.errorResponse(request, page, error, template, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
    if not entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
      #TODO: Change this into a proper redirect
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
      return http.HttpResponseRedirect('/')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
    context['entity'] = entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
    context['entity_type'] = self._params['name']
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
    template = self._params['public_template']
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
    return helper.responses.respond(request, template, context)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   125
  def create(self, request, page=None, **kwargs):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
    """Displays the create page for this entity type
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   128
    Args:
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   129
      request: the standard Django HTTP request object
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   130
      page: a soc.logic.site.page.Page object which is abstraction
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   131
        that combines a Django view with sidebar menu info
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   132
      kwargs: not used for create()
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
    # Create page is an edit page with no key fields
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   136
    kwargs = {}
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   137
    fields = self._logic.getKeyFieldNames()
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   138
    for field in fields:
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   139
      kwargs[field] = None
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   140
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   141
    return self.edit(request, page=page, **kwargs)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   143
  def edit(self, request, page=None, **kwargs):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
    """Displays the public page for the entity specified by **kwargs
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
    Args:
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   147
      request: the standard Django HTTP request object
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   148
      page: a soc.logic.site.page.Page object which is abstraction
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   149
        that combines a Django view with sidebar menu info
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
      kwargs: The Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
    try:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
      self.checkAccess('edit', request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
      return alt_response.response()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   158
    context = helper.responses.getUniversalContext(request)
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   159
    context['page'] = page
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
    try:
439
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   163
      if all(kwargs.values()):
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   164
        key_fields = self._logic.getKeyFieldsFromDict(kwargs)
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   165
        entity = self._logic.getIfFields(key_fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
    except soc.logic.out_of_band.ErrorResponse, error:
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   167
      template = self._params['public_template']
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   168
      error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   169
          'entity_type_lower' : self._params['name'].lower(),
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   170
          'entity_type' : self._params['name'],
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   171
          'create' : self._params['create_redirect']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
          }
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   173
      return simple.errorResponse(request, page, error, template, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
    if request.method == 'POST':
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   176
      return self.editPost(request, entity, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
    else:
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   178
      return self.editGet(request, entity, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   180
  def editPost(self, request, entity, context):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
    """Same as edit, but on POST
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
    """
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   183
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
    if entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
      form = self._params['edit_form'](request.POST)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
    else:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
      form = self._params['create_form'](request.POST)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
    if not form.is_valid():
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   190
      return self._constructResponse(request, entity, context, form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
    fields = self.collectCleanedFields(form)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
    self._editPost(request, entity, fields)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   196
    key_fields = self._logic.getKeyFieldsFromDict(fields)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   197
    entity = self._logic.updateOrCreateFromFields(fields, key_fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
    if not entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
      return http.HttpResponseRedirect('/')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 387
diff changeset
   202
    params = self._params['edit_params']
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   203
    suffix = self._logic.getKeySuffix(entity)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
    # redirect to (possibly new) location of the entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
    # (causes 'Profile saved' message to be displayed)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
    return helper.responses.redirectToChangedSuffix(
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   208
        request, suffix, suffix,
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
        params=params)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   211
  def editGet(self, request, entity, context):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
    """Same as edit, but on GET
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
    """
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   214
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   215
    suffix = self._logic.getKeySuffix(entity)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   216
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   217
    # Remove the params from the request, this is relevant only if
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   218
    # someone bookmarked a POST page.
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   219
    is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   220
    if request.GET.get(self.DEF_SUBMIT_MSG_PARAM_NAME):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   221
      if (not entity) or (not is_self_referrer):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   222
        return http.HttpResponseRedirect(request.path)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   223
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   224
    if entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   225
      # Note: no message will be displayed if parameter is not present
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   226
      context['notice'] = helper.requests.getSingleIndexedParamValue(
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   227
          request, self.DEF_SUBMIT_MSG_PARAM_NAME,
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   228
          values=self._params['save_message'])
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   229
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   230
      # populate form with the existing entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   231
      form = self._params['edit_form'](instance=entity)
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   232
      self._editGet(request, entity, form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   233
    else:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   234
      form = self._params['create_form']()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   235
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   236
    return self._constructResponse(request, entity, context, form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   237
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   238
  def list(self, request, page=None):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   239
    """Displays the list page for the entity type
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   240
    
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   241
    Args:
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   242
      request: the standard Django HTTP request object
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   243
      page: a soc.logic.site.page.Page object which is abstraction
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   244
        that combines a Django view with sidebar menu info
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   246
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   247
    try:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   248
      self.checkAccess('list', request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   249
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   250
      return alt_response.response()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   251
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   252
    context = helper.responses.getUniversalContext(request)
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   253
    context['page'] = page
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   254
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   255
    offset, limit = helper.lists.cleanListParameters(
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   256
      offset=request.GET.get('offset'), limit=request.GET.get('limit'))
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   257
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   258
    # Fetch one more to see if there should be a 'next' link
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   259
    entities = self._logic.getForLimitAndOffset(limit + 1, offset=offset)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   260
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   261
    context['pagination_form'] = helper.lists.makePaginationForm(request, limit)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   263
    templates = self._params['lists_template']
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   264
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   265
    context = helper.lists.setList(request, context, entities, 
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   266
                                 offset, limit, templates)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   267
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   268
    context['entity_type'] = self._params['name']
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   269
    context['entity_type_plural'] = self._params['name_plural']
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   270
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
    template = self._params['list_template']
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   272
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   273
    return helper.responses.respond(request, template, context)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   274
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   275
  def delete(self, request, page=None, **kwargs):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   276
    """Shows the delete page for the entity specified by kwargs
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   277
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   278
    Args:
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   279
      request: the standard Django HTTP request object
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   280
      page: a soc.logic.site.page.Page object which is abstraction
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   281
        that combines a Django view with sidebar menu info
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   282
      kwargs: The Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   283
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   284
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   285
    try:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   286
      self.checkAccess('delete', request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   287
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   288
      return alt_response.response()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   289
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   290
    # create default template context for use with any templates
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   291
    context = helper.responses.getUniversalContext(request)
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   292
    context['page'] = page
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   293
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   294
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   295
    try:
438
af082c92ddda Quick fix to fix user profile creation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 435
diff changeset
   296
      key_fields = self._logic.getKeyFieldsFromDict(kwargs)
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   297
      entity = self._logic.getIfFields(key_fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   298
    except soc.logic.out_of_band.ErrorResponse, error:
409
9d24850db88f Addressed comments by Pawel and Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 407
diff changeset
   299
      template = self._params['edit_template']
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   300
      error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   301
          'entity_type_lower' : self._params['name'].lower(),
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   302
          'entity_type' : self._params['name'],
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   303
          'create' : self._params['create_redirect']
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   304
          }
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   305
      return simple.errorResponse(request, page, error, template, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   306
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   307
    if not entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   308
      #TODO: Create a proper error page for this
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   309
      return http.HttpResponseRedirect('/')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   310
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   311
    if not self._logic.isDeletable(entity):
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 387
diff changeset
   312
      # TODO: Update the notice area telling the user that they
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 387
diff changeset
   313
      # can't delete the entity
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   314
      pass
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   315
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   316
    self._logic.delete(entity)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   317
    redirect = self._params['delete_redirect']
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   318
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   319
    return http.HttpResponseRedirect(redirect)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   320
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   321
  def _editPost(self, request, entity, fields):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   322
    """Performs any required processing on the entity to post its edit page
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   323
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   324
    Args:
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   325
      request: the django request object
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   326
      entity:  the entity to create or update from POST contents
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   327
      fields: the new field values
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   328
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   329
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   330
    raise NotImplementedError
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   331
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   332
  def _editGet(self, request, entity, form):
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   333
    """Performs any required processing on the form to get its edit page
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   334
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   335
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   336
      request: the django request object
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   337
      entity: the entity to get
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   338
      form: the django form that will be used for the page
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   339
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   340
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   341
    pass
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   342
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   343
  def checkUnspecified(self, access_type, request):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   344
    """Checks whether an unspecified access_type should be allowed
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   345
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   346
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   347
      access_type: the access type (such as 'list' or 'edit') that was
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   348
                   not present in the _rights dictionary when checking.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   349
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   350
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   351
    pass
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   352
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   353
  def _constructResponse(self, request, entity, context, form):
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   354
    """Updates the context and returns a response for the specified arguments
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   355
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   356
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   357
      request: the django request object
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   358
      entity: the entity that is used
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   359
      context: the context to be used
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   360
      form: the form that will be used
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   361
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   362
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   363
    suffix = self._logic.getKeySuffix(entity)
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   364
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   365
    context['form'] = form
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   366
    context['entity'] = entity
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   367
    context['entity_suffix'] = suffix
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   368
    context['entity_type'] = self._params['name']
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   369
    context['entity_type_plural'] = self._params['name_plural']
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   370
    context['entity_type_short'] = self._params['name_short']
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   371
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   372
    template = self._params['edit_template']
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   373
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   374
    return helper.responses.respond(request, template, context)
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   375
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   376
  def checkAccess(self, access_type, request):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   377
    """Runs all the defined checks for the specified type
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   378
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   379
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   380
      access_type: the type of request (such as 'list' or 'edit')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   381
      request: the Django request object
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   382
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   383
    Returns:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   384
      True: If all the required access checks have been made successfully
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   385
      False: If a check failed, in this case self._response will contain
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   386
             the response provided by the failed access check.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   387
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   388
418
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   389
    # Call each access checker
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   390
    for check in self._rights['any_access']:
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   391
      check(request)
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   392
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   393
    if access_type not in self._rights:
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   394
       # No checks defined, so do the 'generic check' and bail out
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   395
      self.checkUnspecified(access_type, request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   396
      return
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   397
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   398
    for check in self._rights[access_type]:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   399
      check(request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   400
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   401
  def collectCleanedFields(self, form):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   402
    """Collects all cleaned fields from form and returns them 
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   403
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   404
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   405
      form: The form from which the cleaned fields should be collected
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   406
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   407
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   408
    fields = {}
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   409
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   410
    for field, value in form.cleaned_data.iteritems():
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   411
      fields[field] = value
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   412
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   413
    return fields