app/soc/views/models/base.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Mon, 17 Nov 2008 22:21:02 +0000
changeset 502 e1e24c0a4e82
parent 500 44ea4620c5c0
child 507 3603fdafabf7
permissions -rw-r--r--
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. Patch by: Pawel Solyga
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>',
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
    22
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
  ]
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
from django import http
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    27
from django.conf.urls import defaults
363
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
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
import soc.logic.out_of_band
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
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
import soc.views.out_of_band
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
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
    38
from soc.logic import path_link_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
from soc.views import simple
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
from soc.views import helper
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
from soc.views.helper import access
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    44
class View(object):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
  """Views for entity classes.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
  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
    48
  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
    49
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  self._logic: the logic singleton for this entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    53
  DEF_SUBMIT_MSG_PARAM_NAME = 's'
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    54
  DEF_SUBMIT_MSG_PROFILE_SAVED = 0
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    55
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    56
  DEF_CREATE_NEW_ENTITY_MSG = 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
    57
      ' 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
    58
      ' <a href="%(create)s">Create '
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    59
      'a New %(entity_type)s</a> page.')
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    60
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
    61
  def __init__(self, params=None, rights=None):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
    """
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
    63
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    64
    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
    65
      rights: This dictionary should be filled with the access check
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    66
        functions that should be called, it will be modified in-place.
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
    67
      params: This dictionary should be filled with the parameters
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    68
        specific to this entity, required fields are:
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    69
        name: the name of the entity (names should have sentence-style caps) 
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    70
        name_short: the short form name of the name ('org' vs 'organization')
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    71
        name_plural: the plural form of the name
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
    72
        url_name: the name of the entity used in urls
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    73
        edit_form: the class of the Django form to be used when editing
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    74
        create_form: the class of the Django form to be used when creating
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    75
        edit_template: the Django template to be used for editing
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    76
        public_template: the Django template to be used as public page 
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    77
        list_template: the Django template to be used as list page
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    78
        lists_template: the Django templates to search for the list page
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    79
        delete_redirect: the Django template to redirect to on delete
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    80
        create_redirect: the Django template to redirect to after creation
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    81
        save_message: the message to display when the entity is saved
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    82
        edit_params: the params to use when editing
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    83
        sidebar: the sidebar menu items for this view
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    84
        sidebar_defaults: a dictionary with defaults for the sidebar 
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
    new_rights = {}
418
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
    88
    new_rights['any_access'] = [access.checkIsUser]
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
    90
    new_params = {}
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
    91
    new_params['create_redirect'] = '/%s' % params['url_name']
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
    92
    new_params['missing_redirect'] = '/%s/create' % params['url_name']
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
    93
    
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    94
    new_params['sidebar'] = None
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    95
    new_params['sidebar_defaults'] = [
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    96
     ('/%s/create', 'New %(name)s'),
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    97
     ('/%s/list', 'List %(plural)s'),
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    98
    ]
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    99
    new_params['sidebar_additional'] = []
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   100
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   101
    new_params['key_fields_prefix'] = []
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   102
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   103
    new_params['django_patterns'] = None
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   104
    new_params['django_patterns_defaults'] = [
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
   105
        (r'^%(url_name)s/show/%(key_fields)s$', 
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   106
            'soc.views.models.%s.public', 'Show %(name)s'),
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
   107
        (r'^%(url_name)s/create$',
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   108
            'soc.views.models.%s.create', 'Create %(name)s'),
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
   109
        (r'^%(url_name)s/create/%(key_fields)s$',
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 494
diff changeset
   110
            'soc.views.models.%s.create', 'Create %(name)s'),
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
   111
        (r'^%(url_name)s/delete/%(key_fields)s$',
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   112
            'soc.views.models.%s.delete', 'Delete %(name)s'),
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
   113
        (r'^%(url_name)s/edit/%(key_fields)s$',
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   114
            'soc.views.models.%s.edit', 'Edit %(name)s'),
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
   115
        (r'^%(url_name)s/list$',
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   116
            'soc.views.models.%s.list', 'List %(name_plural)s'),
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   117
        ]
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   118
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
   119
    new_params['list_redirect_action'] = '/' + params['url_name'] + '/edit'
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 494
diff changeset
   120
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   121
    self._rights = dicts.merge(rights, new_rights)
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   122
    self._params = dicts.merge(params, new_params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   124
  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
   125
    """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
   126
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
    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
   128
      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
   129
      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
   130
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
      kwargs: the Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   134
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   135
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
    try:
428
e0d5de282004 Fixed typo in access check
Sverre Rabbelier <srabbelier@gmail.com>
parents: 418
diff changeset
   137
      self.checkAccess('public', request)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
      return alt_response.response()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
    # create default template context for use with any templates
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
    context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   143
    context['page_name'] = page_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   146
    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
   147
      #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
   148
      return http.HttpResponseRedirect('/')
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   149
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
    try:
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   151
      key_fields = self._logic.getKeyFieldsFromDict(kwargs)
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 429
diff changeset
   152
      entity = self._logic.getIfFields(key_fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
    except soc.logic.out_of_band.ErrorResponse, error:
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   154
      template = params['public_template']
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   155
      return simple.errorResponse(request, page_name, error, template, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   157
    self._public(request, entity, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
    context['entity'] = entity
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   160
    context['entity_type'] = params['name']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   162
    template = params['public_template']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
    return helper.responses.respond(request, template, context)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   166
  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
   167
    """Displays the create page for this entity type.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
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
   169
    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
   170
      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
   171
      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
   172
      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
   173
      kwargs: not used for create()
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   176
    params = dicts.merge(params, self._params)
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   177
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
    # 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
   179
    empty_kwargs = {}
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   180
    fields = self._logic.getKeyFieldNames()
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   181
    for field in fields:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   182
      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
   183
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   184
    request.path = params['create_redirect']
456
7781c2836d3c Fix create redirect
Sverre Rabbelier <srabbelier@gmail.com>
parents: 455
diff changeset
   185
    request.path = helper.requests.replaceSuffix(request.path,
7781c2836d3c Fix create redirect
Sverre Rabbelier <srabbelier@gmail.com>
parents: 455
diff changeset
   186
                                                 old_suffix='edit',
7781c2836d3c Fix create redirect
Sverre Rabbelier <srabbelier@gmail.com>
parents: 455
diff changeset
   187
                                                 new_suffix='edit')
7781c2836d3c Fix create redirect
Sverre Rabbelier <srabbelier@gmail.com>
parents: 455
diff changeset
   188
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   189
    if not kwargs:
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   190
      return self.edit(request, page_name=page_name, params=params, **empty_kwargs)
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   191
    else:
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 494
diff changeset
   192
      return self.edit(request, page_name=page_name, params=params, seed=kwargs, **empty_kwargs)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   194
  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
   195
    """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
   196
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
    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
   198
      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
   199
      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
   200
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
      kwargs: The Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   204
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   205
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
    try:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
      self.checkAccess('edit', request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
      return alt_response.response()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
373
dcd7013ae0d5 Add the Page object to the context of each view method. Also, add some missing
Todd Larsen <tlarsen@google.com>
parents: 363
diff changeset
   211
    context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   212
    context['page_name'] = page_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
    try:
439
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   216
      if all(kwargs.values()):
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   217
        key_fields = self._logic.getKeyFieldsFromDict(kwargs)
0658c3c9a9dc Minor fixes needed for generic key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   218
        entity = self._logic.getIfFields(key_fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   219
    except soc.logic.out_of_band.ErrorResponse, error:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   220
      if not seed:
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   221
        template = params['public_template']
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   222
        error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   223
            'entity_type_lower' : params['name'].lower(),
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   224
            'entity_type' : params['name'],
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   225
            'create' : params['missing_redirect']
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   226
            }
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   227
        return simple.errorResponse(request, page_name, error, template, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   228
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   229
    if request.method == 'POST':
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   230
      return self.editPost(request, entity, context, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   231
    else:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   232
      return self.editGet(request, entity, context, seed, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   233
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   234
  def editPost(self, request, entity, context, 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
   235
    """Same as edit, but on POST.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   236
    """
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   237
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   238
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   239
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   240
    if entity:
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   241
      form = params['edit_form'](request.POST)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   242
    else:
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   243
      form = params['create_form'](request.POST)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   244
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
    if not form.is_valid():
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   246
      return self._constructResponse(request, entity, context, form, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   247
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   248
    key_name, fields = self.collectCleanedFields(form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   249
442
92c17629af0e Fix wrong redirect when changing partial path or link name. Remove an unnecessary assignment in logic/models/base.py that would generate an error when an entity did not exist.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 439
diff changeset
   250
    # get the old_suffix before editing
92c17629af0e Fix wrong redirect when changing partial path or link name. Remove an unnecessary assignment in logic/models/base.py that would generate an error when an entity did not exist.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 439
diff changeset
   251
    old_suffix = self._logic.getKeySuffix(entity)
92c17629af0e Fix wrong redirect when changing partial path or link name. Remove an unnecessary assignment in logic/models/base.py that would generate an error when an entity did not exist.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 439
diff changeset
   252
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   253
    self._editPost(request, entity, fields)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   254
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   255
    if not key_name:
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   256
      key_fields =  self._logic.getKeyFieldsFromDict(fields)
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   257
      key_name = self._logic.getKeyNameForFields(key_fields)
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   258
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   259
    entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   260
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   261
    if not entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
      return http.HttpResponseRedirect('/')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   263
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   264
    page_params = params['edit_params']
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   265
    suffix = self._logic.getKeySuffix(entity)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   266
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   267
    # redirect to (possibly new) location of the entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   268
    # (causes 'Profile saved' message to be displayed)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   269
    return helper.responses.redirectToChangedSuffix(
442
92c17629af0e Fix wrong redirect when changing partial path or link name. Remove an unnecessary assignment in logic/models/base.py that would generate an error when an entity did not exist.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 439
diff changeset
   270
        request, old_suffix, suffix,
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   271
        params=page_params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   272
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   273
  def editGet(self, request, entity, context, seed, 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
   274
    """Same as edit, but on GET.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   275
    """
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   276
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   277
    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
   278
    suffix = self._logic.getKeySuffix(entity)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   279
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   280
    # 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
   281
    # someone bookmarked a POST page.
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   282
    is_self_referrer = helper.requests.isReferrerSelf(request, suffix=suffix)
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   283
    if request.GET.get(self.DEF_SUBMIT_MSG_PARAM_NAME):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   284
      if (not entity) or (not is_self_referrer):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   285
        return http.HttpResponseRedirect(request.path)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   286
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   287
    if entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   288
      # 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
   289
      context['notice'] = helper.requests.getSingleIndexedParamValue(
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   290
          request, self.DEF_SUBMIT_MSG_PARAM_NAME,
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   291
          values=params['save_message'])
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   292
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   293
      # 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
   294
      form = params['edit_form'](instance=entity)
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   295
      if 'key_name' in form.fields:
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   296
        form.fields['key_name'].initial = entity.key().name()
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   297
      self._editGet(request, entity, form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   298
    else:
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   299
      if seed:
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 494
diff changeset
   300
        self._editSeed(request, seed)
490
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   301
        form = params['create_form'](initial=seed)
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   302
      else:
7cfa2ad62944 Allow pre-seeding a create form
Sverre Rabbelier <srabbelier@gmail.com>
parents: 482
diff changeset
   303
        form = params['create_form']()
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   304
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   305
    return self._constructResponse(request, entity, context, form, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   306
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   307
  def list(self, request, page_name=None, 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
   308
    """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
   309
    
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
   310
    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
   311
      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
   312
      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
   313
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   314
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   315
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   316
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   317
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   318
    try:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   319
      self.checkAccess('list', request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   320
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   321
      return alt_response.response()
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
    context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   324
    context['page_name'] = page_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   325
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   326
    offset, limit = helper.lists.cleanListParameters(
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   327
      offset=request.GET.get('offset'), limit=request.GET.get('limit'))
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   328
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   329
    # Fetch one more to see if there should be a 'next' link
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   330
    entities = self._logic.getForLimitAndOffset(limit + 1, offset=offset)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   331
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   332
    context['pagination_form'] = helper.lists.makePaginationForm(request, limit)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   333
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   334
    templates = params['lists_template']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   335
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   336
    context = helper.lists.setList(request, context, entities, 
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   337
                                 offset, limit, templates)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   338
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   339
    context['entity_type'] = params['name']
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   340
    context['entity_type_plural'] = params['name_plural']
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents: 494
diff changeset
   341
    context['redirect_action'] = params['list_redirect_action']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   342
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   343
    template = params['list_template']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   344
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   345
    return helper.responses.respond(request, template, context)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   346
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   347
  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
   348
    """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
   349
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   350
    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
   351
      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
   352
      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
   353
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   354
      kwargs: The Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   355
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   356
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   357
    params = dicts.merge(params, self._params)
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   358
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   359
    try:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   360
      self.checkAccess('delete', request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   361
    except soc.views.out_of_band.AccessViolationResponse, alt_response:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   362
      return alt_response.response()
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   364
    # create default template context for use with any templates
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   365
    context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   366
    context['page_name'] = page_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   367
    entity = None
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   368
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   369
    try:
438
af082c92ddda Quick fix to fix user profile creation
Sverre Rabbelier <srabbelier@gmail.com>
parents: 435
diff changeset
   370
      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
   371
      entity = self._logic.getIfFields(key_fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   372
    except soc.logic.out_of_band.ErrorResponse, error:
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   373
      template = params['edit_template']
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   374
      error.message = error.message + self.DEF_CREATE_NEW_ENTITY_MSG % {
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   375
          'entity_type_lower' : params['name'].lower(),
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   376
          'entity_type' : params['name'],
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   377
          'create' : params['missing_redirect']
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   378
          }
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   379
      return simple.errorResponse(request, page_name, error, template, context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   380
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   381
    if not entity:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   382
      #TODO: Create a proper error page for this
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   383
      return http.HttpResponseRedirect('/')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   384
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   385
    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
   386
      # 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
   387
      # can't delete the entity
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   388
      pass
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   389
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   390
    self._logic.delete(entity)
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   391
    redirect = params['delete_redirect']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   392
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   393
    return http.HttpResponseRedirect(redirect)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   394
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   395
  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
   396
    """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
   397
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   398
    Args:
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   399
      request: the django request object
402
021e86368600 Incorperated suggestions by Todd
Sverre Rabbelier <srabbelier@gmail.com>
parents: 399
diff changeset
   400
      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
   401
      fields: the new field values
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   402
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   403
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   404
    pass
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   405
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   406
  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
   407
    """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
   408
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   409
    Args:
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   410
      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
   411
      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
   412
      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
   413
    """
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   414
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   415
    pass
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   416
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   417
  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
   418
    """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
   419
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   420
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   421
      request: the django request object
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   422
      entity: the entity to get
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   423
      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
   424
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   425
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   426
    pass
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   427
497
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   428
  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
   429
    """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
   430
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   431
    Args:
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   432
      request: the django request object
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   433
      seed: the fields to seed the create page with
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   434
    """
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   435
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   436
    pass
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   437
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   438
  def checkUnspecified(self, access_type, request):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   439
    """Checks whether an unspecified access_type should be allowed.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   440
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   441
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   442
      access_type: the access type (such as 'list' or 'edit') that was
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   443
                   not present in the _rights dictionary when checking.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   444
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   445
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   446
    pass
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   447
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   448
  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
   449
    """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
   450
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   451
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   452
      request: the django request object
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   453
      entity: the entity that is used
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   454
      context: the context to be used
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   455
      form: the form that will be used
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
   456
      params: a dict with params for this View
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   457
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   458
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   459
    suffix = self._logic.getKeySuffix(entity)
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   460
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   461
    context['form'] = form
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   462
    context['entity'] = entity
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   463
    context['entity_suffix'] = suffix
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   464
    context['entity_type'] = params['name']
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   465
    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
   466
    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
   467
    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
   468
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   469
    template = params['edit_template']
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   470
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   471
    return helper.responses.respond(request, template, context)
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   472
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   473
  def checkAccess(self, access_type, request):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   474
    """Runs all the defined checks for the specified type
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   475
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   476
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   477
      access_type: the type of request (such as 'list' or 'edit')
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   478
      request: the Django request object
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   479
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   480
    Returns:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   481
      True: If all the required access checks have been made successfully
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   482
      False: If a check failed, in this case self._response will contain
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   483
             the response provided by the failed access check.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   484
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   485
418
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   486
    # Call each access checker
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   487
    for check in self._rights['any_access']:
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   488
      check(request)
e035f31d131e Always check if a user filled in a user profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
   489
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   490
    if access_type not in self._rights:
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
   491
       # No checks defined, so do the 'generic check' and bail out
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   492
      self.checkUnspecified(access_type, request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   493
      return
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   494
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   495
    for check in self._rights[access_type]:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   496
      check(request)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   497
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   498
  def collectCleanedFields(self, 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
   499
    """Collects all cleaned fields and returns them with the key_name.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   500
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   501
    Args:
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   502
      form: The form from which the cleaned fields should be collected
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   503
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   504
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   505
    fields = {}
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   506
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   507
    key_name = None
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   508
    if 'key_name' in form.cleaned_data:
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   509
      key_name = form.cleaned_data.pop('key_name')
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   510
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   511
    for field, value in form.cleaned_data.iteritems():
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   512
      fields[field] = value
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   513
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 458
diff changeset
   514
    return key_name, fields
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   515
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   516
  def getKeyFieldsPattern(self, params):
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   517
    """
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   518
    """
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   519
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   520
    names = self._logic.getKeyFieldNames()
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   521
    patterns = params['key_fields_prefix']
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   522
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   523
    for name in names:
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   524
      pattern = r'(?P<%s>%s)' % (name, path_link_name.LINKNAME_PATTERN_CORE)
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   525
      patterns.append(pattern)
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   526
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   527
    result = '/'.join(patterns)
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   528
    return result
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   529
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   530
  def _getSidebarItems(self, 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
   531
    """Retrieves a list of sidebar entries for this view from self._params.
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   532
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   533
    If params['sidebar'] is None default entries will be constructed 
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   534
    """
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   535
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   536
    # Return the found result
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   537
    if params['sidebar']:
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   538
      return params['sidebar']
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   539
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   540
    # Construct defaults manualy
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   541
    defaults = params['sidebar_defaults']
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   542
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   543
    result = []
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   544
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   545
    for url, title in defaults:
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
   546
      url = url % params['url_name'].lower()
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   547
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   548
      title = title % {
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   549
          'name': params['name'],
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   550
          'plural': params['name_plural']
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   551
          }
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   552
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   553
      item = (url, title)
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   554
      result.append(item)
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   555
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   556
    for item in params['sidebar_additional']:
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   557
      result.append(item)
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
    return result
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   560
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   561
  def getSidebarLinks(self, 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
   562
    """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
   563
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   564
    Args:
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   565
      params: see __init__
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   566
    """
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   567
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   568
    params = dicts.merge(params, self._params)
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   569
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   570
    items = []
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   571
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   572
    for url, title in self._getSidebarItems(params):
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   573
      items.append({'url': url, 'title': title})
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   574
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   575
    res = {}
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   576
    res['heading'] = params['name']
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   577
    res['items'] = items
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   578
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   579
    return res
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   580
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   581
  def getDjangoURLPatterns(self, 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
   582
    """Retrieves a list of sidebar entries for this view from self._params.
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   583
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 497
diff changeset
   584
    If self._params['django_patterns'] is None default entries will be constructed.
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   585
    """
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   586
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   587
    params = dicts.merge(params, self._params)
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   588
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   589
    # Return the found result
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   590
    if params['django_patterns']:
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   591
      return params['django_patterns']
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   592
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   593
    # Construct defaults manualy
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   594
    default_patterns = params['django_patterns_defaults']
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   595
    key_fields_pattern = self.getKeyFieldsPattern(params)
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   596
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   597
    patterns = []
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   598
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   599
    for url, module, name in default_patterns:
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
   600
      name_short = params['name_short']
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
   601
      name_plural = params['name_plural']
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   602
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   603
      name = name % {
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
   604
          'name': name_short, 
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
   605
          'name_plural': name_plural,
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   606
          }
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   607
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
   608
      module = module % params['module_name']
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   609
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   610
      url = url % {
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
   611
          'url_name': params['url_name'],
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   612
          'lnp': path_link_name.LINKNAME_ARG_PATTERN,
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   613
          'ulnp': path_link_name.LINKNAME_PATTERN_CORE,
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   614
          'key_fields': key_fields_pattern,
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   615
          }
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   616
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   617
      kwargs = {'page_name': name}
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   618
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   619
      item = (url, module, kwargs, name)
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   620
      patterns.append(item)
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   621
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   622
    return patterns