app/soc/views/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 29 Nov 2008 19:45:19 +0000
changeset 614 53a3e46fc512
parent 613 4880ffa9f3ba
child 615 1d09147de51f
permissions -rw-r--r--
Moved getSidebarLinks to sitemap.sidebar This is the fourth, and final patch, that attempts to improve the cohesion in the base.View class. Patch by: Sverre Rabbelier
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>',
516
ec1dcd70b97e The list method in models/base.py can now use a filter to make a selection of entities to show.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 514
diff changeset
    22
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    23
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
363
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
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
from django import http
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
from django.utils.translation import ugettext_lazy
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
import soc.logic
539
e30462354e26 Allow for multiple list objects
Sverre Rabbelier <srabbelier@gmail.com>
parents: 537
diff changeset
    31
import soc.logic.lists
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
import soc.views.helper.lists
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
import soc.views.helper.responses
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
    34
import soc.views.helper.params
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    36
from soc.logic import dicts
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
from soc.logic import models
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
from soc.views import helper
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
    39
from soc.views import out_of_band
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
    40
from soc.views.helper import access
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
    41
from soc.views.helper import forms
613
4880ffa9f3ba Moved getDjangoURLPatterns to sitemap.sitemap
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
    42
from soc.views import sitemap
363
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
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    45
class View(object):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
  """Views for entity classes.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
  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
    49
  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
    50
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  self._logic: the logic singleton for this entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
  """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
542
7cc99461b64d Remove redundant dicts for URL patterns and sidebar menu text, and use the
Todd Larsen <tlarsen@google.com>
parents: 539
diff changeset
    54
  DEF_CREATE_NEW_ENTITY_MSG_FMT = ugettext_lazy(
502
e1e24c0a4e82 Add new parameters to views: url_name which is name used in urls (instead of using lower entity names) and module_name which is used for constructing django urls. Append changes to all affected files. Fix some title names. Change urls that included underscores like "site_settings" into urls with slash "site/settings". Plus some other minor fixes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 500
diff changeset
    55
      ' You can create a new %(entity_type)s by visiting'
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    56
      ' <a href="%(create)s">Create '
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    57
      'a New %(entity_type)s</a> page.')
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    58
507
3603fdafabf7 Fold the _rights dictionary into params
Sverre Rabbelier <srabbelier@gmail.com>
parents: 502
diff changeset
    59
  def __init__(self, params=None):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
    """
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
    61
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
    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
    63
      params: This dictionary should be filled with the parameters
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    64
        specific to this entity. See the methods in this class on
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    65
        the fields it should contain, and how they are used.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
    68
    self._params = helper.params.constructParams(params)
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
    69
    self._logic = params['logic']
554
68c7a1dd3c52 Renamed templates/soc/entity/list/entity_*.html to *.html
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    70
606
65d35584ee31 Make forms generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 605
diff changeset
    71
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
    72
  def public(self, request, page_name=None, params=None, **kwargs):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
    73
    """Displays the public page for the entity specified by **kwargs.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    75
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    76
      rights: The rights dictionary is used to check if the user has
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    77
        the required rights to view the public page for this entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    78
        See checkAccess for more details on how the rights dictionary
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    79
        is used to check access rights.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    80
      error_public: The error_public value is used as template when
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    81
        the key values (as defined by the page's url) do not
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    82
        correspond to an existing entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    83
      name: The name value is used to set the entity_type in the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    84
        context so that the template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    85
      public_template: The public_template value is used as template
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    86
        to display the public page of the found entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    87
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    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
    89
      request: the standard Django HTTP request object
500
44ea4620c5c0 Replace old page parameter doc string description with new one for page_name.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 499
diff changeset
    90
      page_name: the page name displayed in templates as page and header title
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    91
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
      kwargs: the Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
    95
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
    96
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
    try:
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
    98
      access.checkAccess('public', request, rights=params['rights'])
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
    99
    except out_of_band.Error, error:
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   100
      return error.response(request)
363
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)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   104
    context['page_name'] = page_name
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
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   107
    if not all(kwargs.values()):
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   108
      #TODO: Change this into a proper redirect
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   109
      return http.HttpResponseRedirect('/')
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   110
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
    try:
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   112
      key_fields = self._logic.getKeyFieldsFromDict(kwargs)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   113
      entity = self._logic.getIfFields(key_fields)
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   114
    except out_of_band.Error, error:
573
1b37588c4922 Moved the custom error.html page for Site to it's proper place
Sverre Rabbelier <srabbelier@gmail.com>
parents: 572
diff changeset
   115
      return error.response(request, template=params['error_public'],
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   116
                            context=context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   118
    self._public(request, entity, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
    context['entity'] = entity
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   121
    context['entity_type'] = params['name']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   123
    template = params['public_template']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
    return helper.responses.respond(request, template, context)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   127
  def create(self, request, page_name=None, params=None, **kwargs):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   128
    """Displays the create page for this entity type.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   130
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   131
      The params dictionary is passed on to edit, see the docstring
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   132
      for edit on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   133
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
   134
    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
   135
      request: the standard Django HTTP request object
500
44ea4620c5c0 Replace old page parameter doc string description with new one for page_name.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 499
diff changeset
   136
      page_name: the page name displayed in templates as page and header title
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   137
      params: a dict with params for this View
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
   138
      kwargs: not used for create()
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   141
    params = dicts.merge(params, self._params)
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   142
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
    # Create page is an edit page with no key fields
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   144
    empty_kwargs = {}
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   145
    fields = self._logic.getKeyFieldNames()
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   146
    for field in fields:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   147
      empty_kwargs[field] = None
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   148
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   149
    if not kwargs:
537
f2bd9c465a6d Fix getKeyValues() method footprint mismatches in soc.logic.models.site.Logic
Todd Larsen <tlarsen@google.com>
parents: 533
diff changeset
   150
      return self.edit(request, page_name=page_name, params=params,
f2bd9c465a6d Fix getKeyValues() method footprint mismatches in soc.logic.models.site.Logic
Todd Larsen <tlarsen@google.com>
parents: 533
diff changeset
   151
                       **empty_kwargs)
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   152
    else:
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   153
      return self.edit(request, page_name=page_name, params=params,
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   154
                       seed=kwargs, **empty_kwargs)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   156
  def edit(self, request, page_name=None, params=None, seed=None, **kwargs):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   157
    """Displays the edit page for the entity specified by **kwargs.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   159
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   160
      The params dictionary is passed on to either editGet or editPost
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   161
      depending on the method type of the request. See the docstring
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   162
      for editGet and editPost on how they use it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   163
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   164
      rights: The rights dictionary is used to check if the user has
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   165
        the required rights to edit (or create) a new entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   166
        See checkAccess for more details on how the rights dictionary
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   167
        is used to check access rights.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   168
      name: The name value is used to construct the message_fmt of the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   169
        raised error when there key_values do not define an existing
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   170
        entity. See DEF_CREATE_NEW_ENTITY_MSG_FMT on how the name
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   171
        (and the lower() version of it) is used.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   172
      missing_redirect: The missing_redirect value is also used to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   173
        construct the message_fmt mentioned above.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   174
      error_public: The error_public value is used as the template for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   175
        the error response mentioned above.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   176
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
    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
   178
      request: the standard Django HTTP request object
500
44ea4620c5c0 Replace old page parameter doc string description with new one for page_name.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 499
diff changeset
   179
      page_name: the page name displayed in templates as page and header title
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   180
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
      kwargs: The Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   184
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   185
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
    try:
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
   187
      access.checkAccess('edit', request, rights=params['rights'])
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   188
    except out_of_band.Error, error:
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   189
      return error.response(request)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
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
   191
    context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   192
    context['page_name'] = page_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
    try:
439
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   196
      if all(kwargs.values()):
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   197
        key_fields = self._logic.getKeyFieldsFromDict(kwargs)
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   198
        entity = self._logic.getIfFields(key_fields)
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   199
    except out_of_band.Error, error:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   200
      if not seed:
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   201
        error.message_fmt = (
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   202
          error.message_fmt + self.DEF_CREATE_NEW_ENTITY_MSG_FMT % {
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   203
            'entity_type_lower' : params['name'].lower(),
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   204
            'entity_type' : params['name'],
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   205
            'create' : params['missing_redirect']})
573
1b37588c4922 Moved the custom error.html page for Site to it's proper place
Sverre Rabbelier <srabbelier@gmail.com>
parents: 572
diff changeset
   206
        return error.response(request, template=params['error_public'],
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   207
                              context=context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
    if request.method == 'POST':
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   210
      return self.editPost(request, entity, context, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
    else:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   212
      return self.editGet(request, entity, context, seed, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   214
  def editPost(self, request, entity, context, params):
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   215
    """Processes POST requests for the specified entity
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   216
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   217
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   218
      The params dictionary is passed to _constructResponse when the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   219
      form is not valid (see edit_form and create_form below). See
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   220
      the docstring of _constructResponse on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   221
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   222
      edit_form: The edit_form value is used as form when there is an
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   223
        existing entity. It is provided with with the request.POST
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   224
        dictionary on construction. The collectCleanedFields method
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   225
        is called with the newly constructed form. If the form is
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   226
        not valid, it is passed as argument to _constructResponse.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   227
      create_form: The create_form value is used in a similar way to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   228
        edit_form, only it is used when there is no existing entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   229
      edit_redirect: The edit_redirect value is used as the first part
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   230
        of the url if the form was valid. The last part of the url is
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   231
        created using the getKeySuffix method of the _logic object.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   232
      edit_params: The edit_params dictionary is used as argument to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   233
        redirectToChangedSuffix, it will be appended to the url in the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   234
        standard ?key=value format.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   235
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   236
    Args:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   237
      request: a django request object
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   238
      entity: the entity that will be modified or created, may be None
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   239
      context: the context dictionary that will be provided to Django
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   240
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   241
    """
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   242
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   243
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   244
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
    if entity:
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   246
      form = params['edit_form'](request.POST)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   247
    else:
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   248
      form = params['create_form'](request.POST)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   249
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   250
    if not form.is_valid():
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   251
      return self._constructResponse(request, entity, context, form, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   252
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
   253
    key_name, fields = forms.collectCleanedFields(form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   254
563
4a8565ce48c6 Fixed redirects to work consistently
Sverre Rabbelier <srabbelier@gmail.com>
parents: 560
diff changeset
   255
    request.path = params['edit_redirect']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   256
    self._editPost(request, entity, fields)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   257
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   258
    if not key_name:
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   259
      key_fields =  self._logic.getKeyFieldsFromDict(fields)
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   260
      key_name = self._logic.getKeyNameForFields(key_fields)
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   261
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   262
    entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   263
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   264
    if not entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   265
      return http.HttpResponseRedirect('/')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   266
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   267
    page_params = params['edit_params']
563
4a8565ce48c6 Fixed redirects to work consistently
Sverre Rabbelier <srabbelier@gmail.com>
parents: 560
diff changeset
   268
    new_suffix = self._logic.getKeySuffix(entity)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   269
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   270
    # redirect to (possibly new) location of the entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
    # (causes 'Profile saved' message to be displayed)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   272
    return helper.responses.redirectToChangedSuffix(
563
4a8565ce48c6 Fixed redirects to work consistently
Sverre Rabbelier <srabbelier@gmail.com>
parents: 560
diff changeset
   273
        request, None, new_suffix,
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   274
        params=page_params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   275
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   276
  def editGet(self, request, entity, context, seed, params):
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   277
    """Processes GET requests for the specified entity
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   278
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   279
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   280
      The params dictionary is passed to _constructResponse, see the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   281
        docstring  of _constructResponse on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   282
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   283
      save_message: The save_message list is used as argument to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   284
        getSingleIndexedParamValue when an existing entity was saved.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   285
      edit_form: The edit_form is used as form if there is an existing
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   286
        entity. The existing entity is passed as instance to it on
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   287
        construction. If key_name is part of it's fields it will be
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   288
        set to the entity's key().name() value. It is also passed as
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   289
        argument to the _editGet method. See the docstring for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   290
        _editGet on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   291
      create_form: The create_form is used as form if there was no
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   292
        existing entity. If the seed argument is present, it is passed
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   293
        as the 'initial' argument on construction. Otherwise, it is
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   294
        called with no arguments.
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   295
      submit_msg_param_name: The submit_msg_param_name value is used
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   296
        as the key part in the ?key=value construct for the submit
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   297
        message parameter (see also save_message).
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   298
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   299
    Args:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   300
      request: the django request object
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   301
      entity: the entity that will be edited, may be None
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   302
      context: the context dictionary that will be provided to django
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   303
      seed: if no entity is provided, the initial values for the new entity
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   304
      params: a dict with paras for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   305
    """
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   306
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   307
    params = dicts.merge(params, self._params)
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   308
    suffix = self._logic.getKeySuffix(entity)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   309
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   310
    # 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
   311
    # someone bookmarked a POST page.
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   312
    is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   313
    if request.GET.get(params['submit_msg_param_name']):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   314
      if (not entity) or (not is_self_referrer):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   315
        return http.HttpResponseRedirect(request.path)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   316
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   317
    if entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   318
      # 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
   319
      context['notice'] = helper.requests.getSingleIndexedParamValue(
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   320
          request, params['submit_msg_param_name'],
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   321
          values=params['save_message'])
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   322
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   323
      # populate form with the existing entity
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   324
      form = params['edit_form'](instance=entity)
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   325
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   326
      if 'key_name' in form.fields:
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   327
        form.fields['key_name'].initial = entity.key().name()
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   328
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   329
      self._editGet(request, entity, form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   330
    else:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   331
      if seed:
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 494
diff changeset
   332
        self._editSeed(request, seed)
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   333
        form = params['create_form'](initial=seed)
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   334
      else:
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   335
        form = params['create_form']()
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   336
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   337
    return self._constructResponse(request, entity, context, form, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   338
516
ec1dcd70b97e The list method in models/base.py can now use a filter to make a selection of entities to show.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 514
diff changeset
   339
  def list(self, request, page_name=None, params=None, filter=None):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   340
    """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
   341
    
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
   342
    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
   343
      request: the standard Django HTTP request object
500
44ea4620c5c0 Replace old page parameter doc string description with new one for page_name.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 499
diff changeset
   344
      page_name: the page name displayed in templates as page and header title
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   345
      params: a dict with params for this View
516
ec1dcd70b97e The list method in models/base.py can now use a filter to make a selection of entities to show.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 514
diff changeset
   346
      filter: a dict for the properties that the entities should have
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   347
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   348
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   349
      The params dictionary is passed as argument to getListContent in
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   350
        the soc.views.helper.list module. See the docstring for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   351
        getListContent on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   352
      The params dictionary is also passed as argument to the _list
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   353
        method. See the docstring for _list on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   354
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   355
      rights: The rights dictionary is used to check if the user has
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   356
        the required rights to list all entities of this View's type.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   357
        See checkAccess for more details on how the rights dictionary
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   358
        is used to check access rights.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   359
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   360
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   361
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   362
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   363
    try:
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
   364
      access.checkAccess('list', request, rights=params['rights'])
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   365
    except out_of_band.Error, error:
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   366
      return error.response(request)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   367
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   368
    content = helper.lists.getListContent(request, params, self._logic, filter)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   369
    contents = [content]
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   370
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   371
    return self._list(request, params, contents, page_name)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   372
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   373
  def _list(self, request, params, contents, page_name):
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   374
    """Returns the list page for the specified contents
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   375
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   376
    Args:
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   377
      request: the standard Django HTTP request object
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   378
      params: a dict with params for this View
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   379
      contents: a list of content dicts
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   380
      page_name: the page name displayed in templates as page and header title
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   381
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   382
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   383
      name: The name value is used to set the entity_type in the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   384
        context so that the template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   385
      name_plural: The name_plural value is used to set
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   386
        the entity_type_plural value in the context so that the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   387
        template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   388
      list_template: The list_template value is used as template for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   389
        to display the list of all entities for this View.
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   390
    """
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   391
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   392
    context = helper.responses.getUniversalContext(request)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   393
    context['page_name'] = page_name
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   394
    context['list'] = soc.logic.lists.Lists(contents)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   395
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   396
    context['entity_type'] = params['name']
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   397
    context['entity_type_plural'] = params['name_plural']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   398
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   399
    template = params['list_template']
363
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
    return helper.responses.respond(request, template, context)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   402
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   403
  def delete(self, request, page_name=None, params=None, **kwargs):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   404
    """Shows the delete page for the entity specified by **kwargs.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   405
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   406
    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
   407
      request: the standard Django HTTP request object
500
44ea4620c5c0 Replace old page parameter doc string description with new one for page_name.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 499
diff changeset
   408
      page_name: the page name displayed in templates as page and header title
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   409
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   410
      kwargs: The Key Fields for the specified entity
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   411
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   412
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   413
      rights: The rights dictionary is used to check if the user has
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   414
        the required rights to delete the specified entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   415
        See checkAccess for more details on how the rights dictionary
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   416
        is used to check access rights.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   417
      name: used in the same way as in edit(), see it's docstring for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   418
        a more detailed explanation on how it is used.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   419
      missing_redirect: see name
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   420
      error_edit: see name
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   421
      delete_redirect: The delete_redirect value is used as the url to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   422
        redirect to after having successfully deleted the entity.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   423
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   424
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   425
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   426
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   427
    try:
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
   428
      access.checkAccess('delete', request, rights=params['rights'])
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   429
    except out_of_band.Error, error:
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   430
      return error.response(request)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   431
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   432
    # create default template context for use with any templates
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   433
    context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   434
    context['page_name'] = page_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   435
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   436
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   437
    try:
438
af082c92ddda Quick fix to fix user profile creation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 435
diff changeset
   438
      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
   439
      entity = self._logic.getIfFields(key_fields)
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   440
    except out_of_band.Error, error:
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   441
      error.message_fmt = (
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   442
        error.message_fmt + self.DEF_CREATE_NEW_ENTITY_MSG_FMT % {
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   443
          'entity_type_lower' : params['name'].lower(),
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   444
          'entity_type' : params['name'],
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   445
          'create' : params['missing_redirect']})
573
1b37588c4922 Moved the custom error.html page for Site to it's proper place
Sverre Rabbelier <srabbelier@gmail.com>
parents: 572
diff changeset
   446
      return error.response(request, template=params['error_edit'],
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   447
                            context=context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   448
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   449
    if not entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   450
      #TODO: Create a proper error page for this
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   451
      return http.HttpResponseRedirect('/')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   452
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   453
    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
   454
      # 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
   455
      # can't delete the entity
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   456
      pass
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   457
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   458
    self._logic.delete(entity)
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   459
    redirect = params['delete_redirect']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   460
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   461
    return http.HttpResponseRedirect(redirect)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   462
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   463
  def _editPost(self, request, entity, fields):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   464
    """Performs any required processing on the entity to post its edit page.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   465
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   466
    Args:
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   467
      request: the django request object
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   468
      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
   469
      fields: the new field values
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   470
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   471
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   472
    pass
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   473
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   474
  def _public(self, request, entity, context):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   475
    """Performs any required processing to get an entities public page.
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   476
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   477
    Args:
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   478
      request: the django request object
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   479
      entity: the entity to make public
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   480
      context: the context object
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   481
    """
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   482
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   483
    pass
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   484
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   485
  def _editGet(self, request, entity, form):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   486
    """Performs any required processing on the form to get its edit page.
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   487
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   488
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   489
      request: the django request object
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   490
      entity: the entity to get
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   491
      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
   492
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   493
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   494
    pass
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   495
497
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   496
  def _editSeed(self, request, seed):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   497
    """Performs any required processing on the form to get its edit page.
497
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   498
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   499
    Args:
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   500
      request: the django request object
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   501
      seed: the fields to seed the create page with
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   502
    """
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   503
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   504
    pass
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   505
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   506
  def _constructResponse(self, request, entity, context, form, params):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   507
    """Updates the context and returns a response for the specified arguments.
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   508
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   509
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   510
      request: the django request object
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   511
      entity: the entity that is used and set in the context
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   512
      context: the context to be used
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   513
      form: the form that will be used and set in the context
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   514
      params: a dict with params for this View
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   515
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   516
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   517
      name: The name_plural value is used to set the entity_type
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   518
       value in the context so that the template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   519
      name_plural: same as name, but used to set entity_type_plural
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   520
      name_short: same as name, but used to set entity_type_short
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   521
      url_name: same as name, but used to set entity_type_url
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   522
      edit_template: The edit_template value is used as template when
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   523
        there is an existing entity to display the edit page for the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   524
        specified entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   525
      create_template: similar to edit_template, but is used when
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   526
        there is no existing entity.
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   527
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   528
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   529
    suffix = self._logic.getKeySuffix(entity)
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   530
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   531
    context['form'] = form
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   532
    context['entity'] = entity
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   533
    context['entity_suffix'] = suffix
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   534
    context['entity_type'] = params['name']
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   535
    context['entity_type_plural'] = params['name_plural']
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   536
    context['entity_type_short'] = params['name_short']
502
e1e24c0a4e82 Add new parameters to views: url_name which is name used in urls (instead of using lower entity names) and module_name which is used for constructing django urls. Append changes to all affected files. Fix some title names. Change urls that included underscores like "site_settings" into urls with slash "site/settings". Plus some other minor fixes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 500
diff changeset
   537
    context['entity_type_url'] = params['url_name']
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   538
576
6614d3f545b7 Fixed spurious 'Invite Host' button in 'create host'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 573
diff changeset
   539
    if entity:
6614d3f545b7 Fixed spurious 'Invite Host' button in 'create host'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 573
diff changeset
   540
      template = params['edit_template']
6614d3f545b7 Fixed spurious 'Invite Host' button in 'create host'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 573
diff changeset
   541
    else:
6614d3f545b7 Fixed spurious 'Invite Host' button in 'create host'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 573
diff changeset
   542
      template = params['create_template']
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   543
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   544
    return helper.responses.respond(request, template, context)
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   545
586
a4a36b06a870 Make the sidebar dynamic depending on the user's rights
Sverre Rabbelier <srabbelier@gmail.com>
parents: 576
diff changeset
   546
  def getSidebarLinks(self, request, params=None):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   547
    """Returns an dictionary with one sidebar entry.
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   548
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   549
    Args:
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   550
      request: the django request object
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   551
      params: a dict with params for this View
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   552
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   553
    Params usage:
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   554
      The params dictionary is passed as argument to getSidebarItems
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   555
        from the soc.views.sitemap.sidebar module, see the docstring
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   556
        of _getSidebarItems on how it uses it.
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   557
    """
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   558
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   559
    params = dicts.merge(params, self._params)
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   560
    return sitemap.sidebar.getSidebarLinks(params)
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   561
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   562
  def getDjangoURLPatterns(self, params=None):
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   563
    """Retrieves a list of sidebar entries for this view
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   564
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   565
    Params usage:
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   566
      The params dictionary is passed to the getDjangoURLPatterns
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   567
        function in the soc.views.sitemap.sitemap module, see the
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   568
        docstring of getDjangoURLPatterns on how it uses it.
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   569
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   570
    Args:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   571
      params: a dict with params for this View
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   572
    """
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   573
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   574
    params = dicts.merge(params, self._params)
613
4880ffa9f3ba Moved getDjangoURLPatterns to sitemap.sitemap
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   575
    return sitemap.sitemap.getDjangoURLPatterns(params)