app/soc/views/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 18 Apr 2009 14:04:39 +0000
changeset 2212 4095892a3c99
parent 2205 77273200ab4f
child 2330 068540d91bde
permissions -rw-r--r--
Removed unneeded imports and variables Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
363
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
1326
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
     8
#
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
1326
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    10
#
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Helpers functions for displaying views.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 428
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
516
ec1dcd70b97e The list method in models/base.py can now use a filter to make a selection of entities to show.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 514
diff changeset
    22
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    23
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
  ]
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
    26
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
    27
import csv
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
    28
import StringIO
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
    29
1268
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
    30
from google.appengine.ext import db
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
from django import http
1098
e5c06755ecd8 Use django.utils.simplejson in soc.views.models.base pick method instead of our standalone simplejson module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1035
diff changeset
    33
from django.utils import simplejson
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 957
diff changeset
    34
from django.utils.translation import ugettext
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    36
from soc.logic import dicts
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
from soc.views import helper
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
    38
from soc.views import out_of_band
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
    39
from soc.views.helper import decorators
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 611
diff changeset
    40
from soc.views.helper import forms
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
    41
from soc.views.helper import redirects
2081
0490222fab35 Fix wrong usage of helper requests module in soc.views.models.base after recent unused imports cleanup.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2076
diff changeset
    42
from soc.views.helper import requests
1349
d05a9bf08c11 Hopefully fix json caching issues in IE7.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1342
diff changeset
    43
from soc.views.helper import responses
613
4880ffa9f3ba Moved getDjangoURLPatterns to sitemap.sitemap
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
    44
from soc.views import sitemap
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
1796
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
    46
import soc.cache.logic
646
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
    47
import soc.logic
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
    48
import soc.logic.lists
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
    49
import soc.views.helper.lists
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
    50
import soc.views.helper.params
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
    51
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
    53
class View(object):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
  """Views for entity classes.
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
  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
    57
  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
    58
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
  self._logic: the logic singleton for this entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
  """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 957
diff changeset
    62
  DEF_CREATE_NEW_ENTITY_MSG_FMT = ugettext(
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
    63
      ' 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
    64
      ' <a href="%(create)s">Create '
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    65
      'a New %(entity_type)s</a> page.')
387
c55195361cb6 Addressed comments by Todd on r822
Sverre Rabbelier <srabbelier@gmail.com>
parents: 373
diff changeset
    66
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 957
diff changeset
    67
  DEF_CREATE_INSTRUCTION_MSG_FMT = ugettext(
663
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
    68
      'Please select a %s for the new %s.')
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
    69
507
3603fdafabf7 Fold the _rights dictionary into params
Sverre Rabbelier <srabbelier@gmail.com>
parents: 502
diff changeset
    70
  def __init__(self, params=None):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
    """
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
    72
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
    73
    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
    74
      params: This dictionary should be filled with the parameters
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    75
        specific to this entity. See the methods in this class on
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    76
        the fields it should contain, and how they are used.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
    79
    self._params = helper.params.constructParams(params)
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
    80
    self._logic = params['logic']
554
68c7a1dd3c52 Renamed templates/soc/entity/list/entity_*.html to *.html
Sverre Rabbelier <srabbelier@gmail.com>
parents: 553
diff changeset
    81
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
    82
  @decorators.merge_params
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
    83
  @decorators.check_access
710
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
    84
  def public(self, request, access_type,
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
    85
             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
    86
    """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
    87
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    88
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    89
      rights: The rights dictionary is used to check if the user has
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    90
        the required rights to view the public page for this entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    91
        See checkAccess for more details on how the rights dictionary
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    92
        is used to check access rights.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    93
      error_public: The error_public value is used as template when
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    94
        the key values (as defined by the page's url) do not
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    95
        correspond to an existing entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    96
      name: The name value is used to set the entity_type in the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    97
        context so that the template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    98
      public_template: The public_template value is used as template
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
    99
        to display the public page of the found entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   100
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
    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
   102
      request: the standard Django HTTP request object
957
5b657063b23b Add missing parameter description to docstring of soc.views.models.base module methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 939
diff changeset
   103
      access_type : the name of the access type which should be checked
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
   104
      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
   105
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
      kwargs: the Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
    # create default template context for use with any templates
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
    context = helper.responses.getUniversalContext(request)
1357
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1349
diff changeset
   111
    helper.responses.useJavaScript(context, params['js_uses_all'])
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   112
    context['page_name'] = page_name
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
    entity = None
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   114
    logic = params['logic']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   116
    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
   117
      #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
   118
      return http.HttpResponseRedirect('/')
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   119
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
    try:
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   121
      entity = logic.getFromKeyFieldsOr404(kwargs)
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   122
    except out_of_band.Error, error:
646
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
   123
      return helper.responses.errorResponse(
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
   124
          error, request, template=params['error_public'], context=context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
1991
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   126
    if not self._public(request, entity, context):
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   127
      redirect = params['public_redirect']
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   128
      if redirect:
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   129
        return http.HttpResponseRedirect(redirect)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
    context['entity'] = entity
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   132
    context['entity_type'] = params['name']
1500
a495ae7efc70 Fixed broken 'Edit <entity name> Settings' for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1497
diff changeset
   133
    context['entity_type_url'] = params['url_name']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
1677
b2cf6ad50a2a Make it possible to add some default values to the context
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1667
diff changeset
   135
    context = dicts.merge(params['context'], context)
b2cf6ad50a2a Make it possible to add some default values to the context
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1667
diff changeset
   136
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   137
    template = params['public_template']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   139
    return helper.responses.respond(request, template, context=context)
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   140
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   141
  @decorators.merge_params
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   142
  @decorators.check_access
1342
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   143
  def admin(self, request, access_type,
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   144
             page_name=None, params=None, **kwargs):
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   145
    """Displays the admin page for the entity specified by **kwargs.
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   146
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   147
    Params usage:
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   148
      rights: The rights dictionary is used to check if the user has
2021
3ab5d0d73f38 Fixed some typo in app/soc/views/models/base.py
Chen Lunpeng <forever.clp@gmail.com>
parents: 2002
diff changeset
   149
        the required rights to view the admin page for this entity.
1342
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   150
        See checkAccess for more details on how the rights dictionary
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   151
        is used to check access rights.
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   152
      name: The name value is used to set the entity_type in the
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   153
        context so that the template can refer to it.
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   154
      public_template: The public_template value is used as template
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   155
        to display the public page of the found entity.
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   156
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   157
    Args:
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   158
      request: the standard Django HTTP request object
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   159
      access_type : the name of the access type which should be checked
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   160
      page_name: the page name displayed in templates as page and header title
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   161
      params: a dict with params for this View
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   162
      kwargs: the Key Fields for the specified entity
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   163
    """
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   164
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   165
    # create default template context for use with any templates
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   166
    context = helper.responses.getUniversalContext(request)
1357
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1349
diff changeset
   167
    helper.responses.useJavaScript(context, params['js_uses_all'])
1342
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   168
    context['page_name'] = page_name
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   169
    logic = params['logic']
1342
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   170
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   171
    try:
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   172
      entity = logic.getFromKeyFieldsOr404(kwargs)
1342
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   173
    except out_of_band.Error, error:
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   174
      return helper.responses.errorResponse(error, request, context=context)
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   175
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   176
    form = params['admin_form'](instance=entity)
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   177
    template = params['admin_template']
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   178
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   179
    return self._constructResponse(request, entity, context, form,
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   180
                                   params, template=template)
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   181
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   182
  @decorators.merge_params
f8056a197fb8 Add an admin view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1339
diff changeset
   183
  @decorators.check_access
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   184
  def export(self, request, access_type,
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   185
             page_name=None, params=None, **kwargs):
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   186
    """Displays the export page for the entity specified by **kwargs.
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   187
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   188
    Params usage:
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   189
      rights: The rights dictionary is used to check if the user has
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   190
        the required rights to view the export page for this entity.
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   191
        See checkAccess for more details on how the rights dictionary
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   192
        is used to check access rights.
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   193
      error_export: The error_export value is used as template when
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   194
        the key values (as defined by the page's url) do not
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   195
        correspond to an existing entity.
1805
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   196
      Params is passed to download, refer to it's docstring for more
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   197
      details on how it uses it.
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   198
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   199
    Args:
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   200
      request: the standard Django HTTP request object
957
5b657063b23b Add missing parameter description to docstring of soc.views.models.base module methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 939
diff changeset
   201
      access_type : the name of the access type which should be checked
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   202
      page_name: the page name displayed in templates as page and header title
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   203
      params: a dict with params for this View
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   204
      kwargs: the Key Fields for the specified entity
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   205
    """
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   206
1805
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   207
    if not ('export_content_type' in params) and ('export_function' in params):
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   208
      return self.public(request, access_type, page_name=page_name,
874
30e9629bf590 Fixed two trivial bugs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 858
diff changeset
   209
                         params=params, **kwargs)
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   210
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   211
    # create default template context for use with any templates
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   212
    entity = None
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   213
    logic = params['logic']
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   214
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   215
    if not all(kwargs.values()):
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   216
      #TODO: Change this into a proper redirect
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   217
      return http.HttpResponseRedirect('/')
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   218
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   219
    try:
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   220
      entity = logic.getFromKeyFieldsOr404(kwargs)
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   221
    except out_of_band.Error, error:
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   222
      return helper.responses.errorResponse(
1805
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   223
          error, request, template=params['error_export'])
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   224
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   225
    export_function = params['export_function']
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   226
    data, filename = export_function(entity)
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   227
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   228
    return self.download(request, data, filename, params)
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   229
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   230
  def download(self, request, data, filename, params):
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   231
    """Returns data as a downloadable file with the specified name.
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   232
1805
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   233
    Params usage:
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   234
      export_template: The export_template value is used as template
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   235
        to display the export page of the found entity.
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   236
      export_content_type: The export_content_type value is used to set
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   237
        the Content-Type header of the HTTP response.  If empty (or None),
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   238
        public() is called instead.
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   239
      export_extension: The export_extension value is used as the suffix
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   240
        of the file that will be offered for download.
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   241
1805
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   242
    Args:
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   243
      request: the standard Django HTTP request object
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   244
      data: the data that should be offered as file content
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   245
      filename: the name the file should have
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   246
      params: a dict with params for this View
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   247
    """
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   248
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   249
    context = {}
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   250
    context['data'] = data
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   251
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   252
    template = params['export_template']
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   253
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   254
    response_args = {'mimetype': params['export_content_type']}
1399
5b154edd94ac Make it easier to support other extensions per-Model (such as .csv, etc.).
Todd Larsen <tlarsen@google.com>
parents: 1398
diff changeset
   255
1400
842958b8a55d Rebased my double work on top of Todd recent commit
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1399
diff changeset
   256
    export_extension = params['export_extension']
842958b8a55d Rebased my double work on top of Todd recent commit
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1399
diff changeset
   257
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1373
diff changeset
   258
    response_headers = {
1399
5b154edd94ac Make it easier to support other extensions per-Model (such as .csv, etc.).
Todd Larsen <tlarsen@google.com>
parents: 1398
diff changeset
   259
        'Content-Disposition': 'attachment; filename=%s%s' % (
1805
7204ec5ead23 Factor download functionality out of export()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1801
diff changeset
   260
            filename, export_extension),
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1373
diff changeset
   261
        }
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   262
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   263
    return helper.responses.respond(request, template, context=context,
1398
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1373
diff changeset
   264
                                    response_args=response_args,
e6a11f0dba68 Force browser to ask to save an Export, and provide a filename extension.
Todd Larsen <tlarsen@google.com>
parents: 1373
diff changeset
   265
                                    response_headers=response_headers)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   266
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   267
  @decorators.check_access
710
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   268
  def create(self, request, access_type,
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   269
             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
   270
    """Displays the create page for this entity type.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   271
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   272
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   273
      The params dictionary is passed on to edit, see the docstring
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   274
      for edit on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   275
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
   276
    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
   277
      request: the standard Django HTTP request object
957
5b657063b23b Add missing parameter description to docstring of soc.views.models.base module methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 939
diff changeset
   278
      access_type : the name of the access type which should be checked
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
   279
      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
   280
      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
   281
      kwargs: not used for create()
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   282
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   283
663
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   284
    new_params = dicts.merge(params, self._params)
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   285
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   286
    # redirect to scope selection view
663
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   287
    if ('scope_view' in new_params) and ('scope_path' not in kwargs):
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   288
      view = new_params['scope_view'].view
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   289
      redirect = new_params['scope_redirect']
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   290
      return self.select(request, view, redirect,
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   291
                         params=params, page_name=page_name, **kwargs)
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   292
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   293
    params = new_params
492
4abdeedfc08e Fixed redirection after creation to Just Work
Sverre Rabbelier <srabbelier@gmail.com>
parents: 490
diff changeset
   294
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   295
    context = helper.responses.getUniversalContext(request)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   296
    helper.responses.useJavaScript(context, params['js_uses_all'])
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   297
    context['page_name'] = page_name
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   298
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   299
    if request.method == 'POST':
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   300
      return self.createPost(request, context, params)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   301
    else:
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   302
      return self.createGet(request, context, params, kwargs)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   303
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   304
  def createGet(self, request, context, params, seed):
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   305
    """See editGet.
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   306
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   307
    Handles generating the patch to create new entities.
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   308
    """
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   309
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   310
    self._editSeed(request, seed)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   311
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   312
    if seed:
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   313
      # pass the seed through the  context to _constructResponse
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   314
      # it will be popped before dispatching to Django
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   315
      context['seed'] = seed
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   316
      form = params['create_form'](initial=seed)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   317
    else:
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   318
      form = params['create_form']()
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
   319
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   320
    return self._constructResponse(request, None, context, form, params)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   321
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   322
  def createPost(self, request, context, params):
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   323
    """See editPost.
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   324
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   325
    Handles the creation of new entities.
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   326
    """
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   327
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   328
    form = params['create_form'](request.POST)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   329
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   330
    if not form.is_valid():
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   331
      return self._constructResponse(request, None, context, form, params)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   332
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   333
    _, fields = forms.collectCleanedFields(form)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   334
    self._editPost(request, None, fields)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   335
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   336
    logic = params['logic']
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   337
    entity = logic.updateOrCreateFromFields(fields)
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   338
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   339
    page_params = params['edit_params']
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   340
    params['suffix'] = entity.key().id_or_name()
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   341
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   342
    request.path = params['edit_redirect'] % params
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   343
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   344
    return helper.responses.redirectToChangedSuffix(
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   345
        request, None, params=page_params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   346
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   347
  @decorators.merge_params
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   348
  @decorators.check_access
710
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   349
  def edit(self, request, access_type,
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   350
           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
   351
    """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
   352
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   353
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   354
      The params dictionary is passed on to either editGet or editPost
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   355
      depending on the method type of the request. See the docstring
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   356
      for editGet and editPost on how they use it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   357
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   358
      rights: The rights dictionary is used to check if the user has
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   359
        the required rights to edit (or create) a new entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   360
        See checkAccess for more details on how the rights dictionary
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   361
        is used to check access rights.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   362
      name: The name value is used to construct the message_fmt of the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   363
        raised error when there key_values do not define an existing
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   364
        entity. See DEF_CREATE_NEW_ENTITY_MSG_FMT on how the name
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   365
        (and the lower() version of it) is used.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   366
      missing_redirect: The missing_redirect value is also used to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   367
        construct the message_fmt mentioned above.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   368
      error_public: The error_public value is used as the template for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   369
        the error response mentioned above.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   370
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   371
    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
   372
      request: the standard Django HTTP request object
957
5b657063b23b Add missing parameter description to docstring of soc.views.models.base module methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 939
diff changeset
   373
      access_type : the name of the access type which should be checked
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
   374
      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
   375
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   376
      kwargs: The Key Fields for the specified entity
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   377
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   378
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   379
    logic = params['logic']
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   380
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
   381
    context = helper.responses.getUniversalContext(request)
1357
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1349
diff changeset
   382
    helper.responses.useJavaScript(context, params['js_uses_all'])
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 471
diff changeset
   383
    context['page_name'] = page_name
363
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
    try:
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   386
      entity = logic.getFromKeyFieldsOr404(kwargs)
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   387
    except out_of_band.Error, error:
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   388
      msg = self.DEF_CREATE_NEW_ENTITY_MSG_FMT % {
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   389
          'entity_type_lower' : params['name'].lower(),
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   390
          'entity_type' : params['name'],
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   391
          'create' : params['missing_redirect']
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   392
          }
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   393
      error.message_fmt = error.message_fmt + msg
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   394
      return helper.responses.errorResponse(
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   395
          error, request, context=context)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   396
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   397
    if request.method == 'POST':
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   398
      return self.editPost(request, entity, context, params=params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   399
    else:
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   400
      return self.editGet(request, entity, context, params=params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   401
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   402
  @decorators.merge_params
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   403
  def editPost(self, request, entity, context, params=None):
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   404
    """Processes POST requests for the specified entity.
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   405
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   406
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   407
      The params dictionary is passed to _constructResponse when the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   408
      form is not valid (see edit_form and create_form below). See
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   409
      the docstring of _constructResponse on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   410
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   411
      edit_form: The edit_form value is used as form when there is an
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   412
        existing entity. It is provided with with the request.POST
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   413
        dictionary on construction. The collectCleanedFields method
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   414
        is called with the newly constructed form. If the form is
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   415
        not valid, it is passed as argument to _constructResponse.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   416
      create_form: The create_form value is used in a similar way to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   417
        edit_form, only it is used when there is no existing entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   418
      edit_redirect: The edit_redirect value is used as the first part
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   419
        of the url if the form was valid. The last part of the url is
2161
3b5e0e0880cd Remove getKeySuffix and it's usage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2160
diff changeset
   420
        created using the .key().id_or_name() method of the entity.
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   421
      edit_params: The edit_params dictionary is used as argument to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   422
        redirectToChangedSuffix, it will be appended to the url in the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   423
        standard ?key=value format.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   424
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   425
    Args:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   426
      request: a django request object
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   427
      entity: the entity that will be modified or created, may be None
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   428
      context: the context dictionary that will be provided to Django
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   429
      params: required, a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   430
    """
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   431
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   432
    logic = params['logic']
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   433
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   434
    form = params['edit_form'](request.POST)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   435
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   436
    if not form.is_valid():
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   437
      return self._constructResponse(request, entity, context, form, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   438
2205
77273200ab4f Fix user_self calling editGet with a wrong parameter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2204
diff changeset
   439
    _, fields = forms.collectCleanedFields(form)
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
    self._editPost(request, entity, fields)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   442
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   443
    entity = logic.updateEntityProperties(entity, fields)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   444
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   445
    page_params = params['edit_params']
2161
3b5e0e0880cd Remove getKeySuffix and it's usage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2160
diff changeset
   446
    params['suffix'] = entity.key().id_or_name()
735
28225172af06 Make it possible to redirect anywhere after editPost
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   447
28225172af06 Make it possible to redirect anywhere after editPost
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   448
    request.path = params['edit_redirect'] % params
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   449
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   450
    return helper.responses.redirectToChangedSuffix(
735
28225172af06 Make it possible to redirect anywhere after editPost
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   451
        request, None, params=page_params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   452
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   453
  @decorators.merge_params
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   454
  def editGet(self, request, entity, context, params=None):
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   455
    """Processes GET requests for the specified entity.
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   456
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   457
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   458
      The params dictionary is passed to _constructResponse, see the
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   459
      docstring  of _constructResponse on how it uses it.
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   460
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   461
      save_message: The save_message list is used as argument to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   462
        getSingleIndexedParamValue when an existing entity was saved.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   463
      edit_form: The edit_form is used as form if there is an existing
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   464
        entity. The existing entity is passed as instance to it on
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   465
        construction. If key_name is part of it's fields it will be
2160
3f9dd37d98a8 Use key().id_or_name() instead of key().name()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2132
diff changeset
   466
        set to the entity's key().id_or_name() value. It is also passed as
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   467
        argument to the _editGet method. See the docstring for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   468
        _editGet on how it uses it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   469
      create_form: The create_form is used as form if there was no
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   470
        existing entity. If the seed argument is present, it is passed
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   471
        as the 'initial' argument on construction. Otherwise, it is
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   472
        called with no arguments.
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   473
      submit_msg_param_name: The submit_msg_param_name value is used
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   474
        as the key part in the ?key=value construct for the submit
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   475
        message parameter (see also save_message).
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   476
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   477
    Args:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   478
      request: the django request object
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   479
      entity: the entity that will be edited, may be None
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   480
      context: the context dictionary that will be provided to django
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   481
      seed: if no entity is provided, the initial values for the new entity
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   482
      params: required, a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   483
    """
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   484
2193
0b6ec4a223cf Comment out unused variables in soc.views.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2164
diff changeset
   485
    # logic = params['logic']
2164
94e498e3dd44 Brown paper bag fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2161
diff changeset
   486
    suffix = entity.key().id_or_name() if entity else None
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   487
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1357
diff changeset
   488
    # remove the params from the request, this is relevant only if
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   489
    # someone bookmarked a POST page.
2081
0490222fab35 Fix wrong usage of helper requests module in soc.views.models.base after recent unused imports cleanup.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2076
diff changeset
   490
    is_self_referrer = requests.isReferrerSelf(
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1357
diff changeset
   491
        request, suffix=suffix, url_name=params['url_name'])
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1357
diff changeset
   492
611
2ec30182e5f1 Move parameter construction into a seperate module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 610
diff changeset
   493
    if request.GET.get(params['submit_msg_param_name']):
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   494
      if (not entity) or (not is_self_referrer):
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   495
        return http.HttpResponseRedirect(request.path)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   496
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   497
    # note: no message will be displayed if parameter is not present
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   498
    context['notice'] = requests.getSingleIndexedParamValue(
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   499
        request, params['submit_msg_param_name'],
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   500
        values=params['save_message'])
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   501
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   502
    # populate form with the existing entity
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   503
    form = params['edit_form'](instance=entity)
939
b341698bf3b1 Make reference picker work for create too
Sverre Rabbelier <srabbelier@gmail.com>
parents: 937
diff changeset
   504
2204
ccbc8bb5ce86 Split edit and create into different methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2193
diff changeset
   505
    self._editGet(request, entity, form)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   506
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   507
    return self._constructResponse(request, entity, context, form, params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   508
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   509
  @decorators.merge_params
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   510
  @decorators.check_access
710
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   511
  def list(self, request, access_type,
1667
95bc81b4cd5c Updated list to use the new order in getForFields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1658
diff changeset
   512
           page_name=None, params=None, filter=None, order=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
   513
    """Displays the list page for the entity type.
1326
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   514
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
   515
    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
   516
      request: the standard Django HTTP request object
957
5b657063b23b Add missing parameter description to docstring of soc.views.models.base module methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 939
diff changeset
   517
      access_type : the name of the access type which should be checked
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
   518
      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
   519
      params: a dict with params for this View
516
ec1dcd70b97e The list method in models/base.py can now use a filter to make a selection of entities to show.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 514
diff changeset
   520
      filter: a dict for the properties that the entities should have
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   521
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   522
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   523
      The params dictionary is passed as argument to getListContent in
1326
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   524
      the soc.views.helper.list module. See the docstring for getListContent
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   525
      on how it uses it. The params dictionary is also passed as argument to
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   526
      the _list method. See the docstring for _list on how it uses it.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   527
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   528
1667
95bc81b4cd5c Updated list to use the new order in getForFields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1658
diff changeset
   529
    content = helper.lists.getListContent(request, params, filter, order=order)
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   530
    contents = [content]
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   531
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   532
    return self._list(request, params, contents, page_name)
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   533
1326
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   534
  def _list(self, request, params, contents, page_name, context=None):
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   535
    """Returns the list page for the specified contents.
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   536
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   537
    If the export parameter is present in request.GET a csv export of
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   538
    the specified list is returned instead, see csv().
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   539
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   540
    Args:
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   541
      request: the standard Django HTTP request object
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   542
      params: a dict with params for this View
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   543
      contents: a list of content dicts
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   544
      page_name: the page name displayed in templates as page and header title
1296
64918d0c97ea Implemented the 2nd part of bulk acceptance.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1269
diff changeset
   545
      context: the context for this page
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   546
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   547
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   548
      name: The name value is used to set the entity_type in the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   549
        context so that the template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   550
      name_plural: The name_plural value is used to set
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   551
        the entity_type_plural value in the context so that the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   552
        template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   553
      list_template: The list_template value is used as template for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   554
        to display the list of all entities for this View.
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   555
    """
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   556
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   557
    try:
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   558
      export = int(request.GET.get('export', -1))
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   559
      export = export if export >= 0 else None
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   560
    except ValueError:
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   561
      export = None
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   562
2119
15cfe49eea42 Brown paper bag fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2108
diff changeset
   563
    content = [i for i in contents if i.get('idx') == export]
2108
286aa6528e84 Properly handle exports when there are multiple lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2081
diff changeset
   564
    if len(content) == 1:
286aa6528e84 Properly handle exports when there are multiple lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2081
diff changeset
   565
      content = content[0]
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   566
      key_order = content.get('key_order')
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   567
1864
3d50c8c34d6a Don't create csv export when no key_order is defined
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1844
diff changeset
   568
      if key_order:
3d50c8c34d6a Don't create csv export when no key_order is defined
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1844
diff changeset
   569
        data = [i.toDict(key_order) for i in content['data']]
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   570
1864
3d50c8c34d6a Don't create csv export when no key_order is defined
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1844
diff changeset
   571
        filename = "export_%d" % export
3d50c8c34d6a Don't create csv export when no key_order is defined
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1844
diff changeset
   572
        return self.csv(request, data, filename, params, key_order)
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   573
1326
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   574
    context = dicts.merge(context,
1296
64918d0c97ea Implemented the 2nd part of bulk acceptance.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1269
diff changeset
   575
        helper.responses.getUniversalContext(request))
1357
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1349
diff changeset
   576
    helper.responses.useJavaScript(context, params['js_uses_all'])
555
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   577
    context['page_name'] = page_name
3cdfb42d941b Split the list view code up in three pieces
Sverre Rabbelier <srabbelier@gmail.com>
parents: 554
diff changeset
   578
    context['list'] = soc.logic.lists.Lists(contents)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   579
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   580
    context['entity_type'] = params['name']
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   581
    context['entity_type_plural'] = params['name_plural']
1967
3fe7ad2d40c0 Allow for a message to be displayed above the list view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1958
diff changeset
   582
    context['list_msg'] = params['list_msg']
1458
ec06d3ee73e4 Make it possible to display a message when there are no lists
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1427
diff changeset
   583
    context['no_lists_msg'] = params['no_lists_msg']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   584
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   585
    template = params['list_template']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   586
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   587
    return helper.responses.respond(request, template, context)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   588
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   589
  @decorators.merge_params
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   590
  @decorators.check_access
710
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   591
  def delete(self, request, access_type,
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   592
             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
   593
    """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
   594
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   595
    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
   596
      request: the standard Django HTTP request object
957
5b657063b23b Add missing parameter description to docstring of soc.views.models.base module methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 939
diff changeset
   597
      access_type : the name of the access type which should be checked
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
   598
      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
   599
      params: a dict with params for this View
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   600
      kwargs: The Key Fields for the specified entity
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   601
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   602
    Params usage:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   603
      rights: The rights dictionary is used to check if the user has
1326
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   604
        the required rights to delete the specified entity. See checkAccess
237d0974654e Minor style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   605
        for more details on how the rights dictionary is used to check access
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   606
        rights.
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   607
      name: used in the same way as in edit(), see it's docstring for
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   608
        a more detailed explanation on how it is used.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   609
      missing_redirect: see name
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   610
      error_edit: see name
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   611
      delete_redirect: The delete_redirect value is used as the url to
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   612
        redirect to after having successfully deleted the entity.
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   613
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   614
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   615
    logic = params['logic']
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   616
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   617
    try:
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   618
      entity = logic.getFromKeyFieldsOr404(kwargs)
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   619
    except out_of_band.Error, error:
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   620
      error.message_fmt = (
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   621
        error.message_fmt + self.DEF_CREATE_NEW_ENTITY_MSG_FMT % {
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   622
          'entity_type_lower' : params['name'].lower(),
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   623
          'entity_type' : params['name'],
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 542
diff changeset
   624
          'create' : params['missing_redirect']})
646
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 639
diff changeset
   625
      return helper.responses.errorResponse(
1357
3dd1507aa723 Prepare all views for a new modular approach to JS loading
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1349
diff changeset
   626
          error, request, template=params['error_edit'])
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   627
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   628
    if not logic.isDeletable(entity):
1594
b80bf089d5f7 Display a notice if the entity cannot be deleted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1500
diff changeset
   629
      page_params = params['cannot_delete_params']
2160
3f9dd37d98a8 Use key().id_or_name() instead of key().name()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2132
diff changeset
   630
      params['suffix'] = entity.key().id_or_name()
1594
b80bf089d5f7 Display a notice if the entity cannot be deleted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1500
diff changeset
   631
      request.path = params['edit_redirect'] % params
b80bf089d5f7 Display a notice if the entity cannot be deleted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1500
diff changeset
   632
b80bf089d5f7 Display a notice if the entity cannot be deleted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1500
diff changeset
   633
      # redirect to the edit page
b80bf089d5f7 Display a notice if the entity cannot be deleted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1500
diff changeset
   634
      # display notice that entity could not be deleted
b80bf089d5f7 Display a notice if the entity cannot be deleted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1500
diff changeset
   635
      return helper.responses.redirectToChangedSuffix(
b80bf089d5f7 Display a notice if the entity cannot be deleted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1500
diff changeset
   636
          request, None, params=page_params)
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   637
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   638
    logic.delete(entity)
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   639
    redirect = params['delete_redirect']
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   640
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   641
    return http.HttpResponseRedirect(redirect)
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   642
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   643
  def select(self, request, view, redirect,
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   644
             page_name=None, params=None, filter=None):
668
77f9a6ea6e67 Some style and typo fixes in different modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 663
diff changeset
   645
    """Displays a list page allowing the user to select an entity.
653
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   646
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   647
    After having selected the Scope, the user is redirected to the
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   648
    'create a new entity' page with the scope_path set appropriately.
653
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   649
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   650
    Params usage:
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   651
      The params dictionary is also passed to getListContent from
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   652
        the helper.list module, please refer to its docstring also.
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   653
      The params dictionary is passed to self._list as well, refer
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   654
        to its docstring for details on how it uses it.
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   655
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   656
    Args:
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   657
      request: the standard Django HTTP request object
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   658
      view: the view for which to generate the select page
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   659
      redirect: the redirect to use
653
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   660
      page_name: the page name displayed in templates as page and header title
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   661
      params: a dict with params for this View
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   662
      filter: a filter that all displayed entities should satisfy
653
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   663
    """
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   664
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   665
    params = dicts.merge(params, view.getParams())
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   666
    params = dicts.merge(params, self._params)
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   667
    params['list_action'] = (redirect, self._params)
663
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   668
    params['list_description'] = self.DEF_CREATE_INSTRUCTION_MSG_FMT % (
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 662
diff changeset
   669
        params['name'], self._params['name'])
653
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   670
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   671
    content = helper.lists.getListContent(request, params, filter=filter)
653
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   672
    contents = [content]
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   673
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   674
    return self._list(request, params, contents, page_name)
5105939d3bb4 Make the 'select entity' View generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 652
diff changeset
   675
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1879
diff changeset
   676
  def _getData(self, model, filter, order, logic):
1796
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   677
    """Retrieves the pick data for this query.
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   678
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   679
    Args:
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   680
      model: the model that is being queried
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   681
      filter: the filters that apply
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   682
      logic: the logic that will be used for the query
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   683
    """
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   684
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1879
diff changeset
   685
    entities = logic.getForFields(filter=filter, order=order, limit=1000)
1796
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   686
    return entities
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   687
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   688
  @decorators.merge_params
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   689
  @decorators.check_access
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   690
  def pick(self, request, acces_type, page_name=None, params=None):
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   691
    """Displays a list page allowing the user to select an entity.
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   692
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   693
    After having selected an entity, the user is redirected to the
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   694
    return_url as specified in the GET args.
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   695
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   696
    Params usage:
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   697
      The params dictionary is passed to self.select, refer
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   698
        to its docstring for details on how it uses it.
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   699
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   700
    Args:
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   701
      request: the standard Django HTTP request object
957
5b657063b23b Add missing parameter description to docstring of soc.views.models.base module methods.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 939
diff changeset
   702
      access_type : the name of the access type which should be checked
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   703
      page_name: the page name displayed in templates as page and header title
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   704
      params: a dict with params for this View
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   705
    """
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   706
1784
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   707
    logic = params['logic']
d5dacf2be926 Use params['logic'] instead of self._logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1703
diff changeset
   708
1304
b736aed728c2 Convert the QueryDict to a regular dictionary
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1296
diff changeset
   709
    # convert to a regular dict
b736aed728c2 Convert the QueryDict to a regular dictionary
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1296
diff changeset
   710
    filter = {}
b736aed728c2 Convert the QueryDict to a regular dictionary
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1296
diff changeset
   711
    for key in request.GET.keys():
b736aed728c2 Convert the QueryDict to a regular dictionary
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1296
diff changeset
   712
      # need to use getlist as we want to support multiple values
b736aed728c2 Convert the QueryDict to a regular dictionary
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1296
diff changeset
   713
      filter[key] = request.GET.getlist(key)
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   714
1796
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   715
    if params['cache_pick']:
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1879
diff changeset
   716
      fun =  soc.cache.logic.cache(self._getData)
1796
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   717
    else:
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1879
diff changeset
   718
      fun = self._getData
1796
5cd78a7aacdd Allow for query caching in pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1790
diff changeset
   719
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1879
diff changeset
   720
    order = []
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1879
diff changeset
   721
    entities = fun(logic.getModel(), filter, order, logic)
1790
7655a46dcfa3 Retrieve all entities rather than just the first 1k
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1784
diff changeset
   722
2132
37c6fe0c426c Make it possible to specify the key_order for pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2119
diff changeset
   723
    key_order = params.get('cache_pick_order')
37c6fe0c426c Make it possible to specify the key_order for pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2119
diff changeset
   724
    data = [i.toDict(key_order) for i in entities]
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   725
1703
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   726
    return self.json(request, data)
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   727
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   728
  def json(self, request, data):
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   729
    """Returns data as a json object.
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   730
    """
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   731
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   732
    to_json = {
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   733
        'data': data,
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   734
        }
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   735
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   736
    json = simplejson.dumps(to_json)
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   737
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   738
    context = {'json': json}
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   739
    template = 'soc/json.html'
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 973
diff changeset
   740
1331
e6fc2238bab0 Added no-cache headers to pick and bulk-accept views.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1326
diff changeset
   741
    response = responses.respond(request, template, context)
1703
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   742
1349
d05a9bf08c11 Hopefully fix json caching issues in IE7.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1342
diff changeset
   743
    # if the browser supports HTTP/1.1
d05a9bf08c11 Hopefully fix json caching issues in IE7.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1342
diff changeset
   744
    # post-check and pre-check and no-store for IE7
1658
65147d9fbd96 Fix too long lines and rename unused loop variables to _ in soc.views.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1594
diff changeset
   745
    response['Cache-Control'] = 'no-store, no-cache, must-revalidate, ' \
65147d9fbd96 Fix too long lines and rename unused loop variables to _ in soc.views.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1594
diff changeset
   746
        'post-check=0, pre-check=0'
1703
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   747
1349
d05a9bf08c11 Hopefully fix json caching issues in IE7.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1342
diff changeset
   748
    # if the browser supports HTTP/1.0
1331
e6fc2238bab0 Added no-cache headers to pick and bulk-accept views.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1326
diff changeset
   749
    response['Pragma'] = 'no-cache'
1703
6d6813e83756 Factor out the 'json' part from pick
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1700
diff changeset
   750
1331
e6fc2238bab0 Added no-cache headers to pick and bulk-accept views.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1326
diff changeset
   751
    return response
933
7331232d6356 Added a 'pick' view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 880
diff changeset
   752
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   753
  def csv(self, request, data, filename, params, key_order=None):
1879
60e48bc3aee3 Some minor style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1878
diff changeset
   754
    """Returns data as a csv file.
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   755
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   756
    If key_order is set data should be a sequence of dicts, otherwise
1879
60e48bc3aee3 Some minor style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1878
diff changeset
   757
    data should be a sequence of lists, see csv.writer and
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   758
    csv.DictWriter for more information.
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   759
    """
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   760
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   761
    params = params.copy()
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   762
    params['export_extension'] = '.csv'
1844
006ca71ed07a Set the content type to text/csv for csv exports
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1843
diff changeset
   763
    params['export_content_type'] = 'text/csv'
2076
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   764
    # fieldnames = params['csv_fieldnames']
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   765
2076
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   766
    file_handler = StringIO.StringIO()
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   767
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   768
    if key_order:
2076
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   769
      writer = csv.DictWriter(file_handler, key_order, dialect='excel')
1843
af89d570bf6f Add row headings to the csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1809
diff changeset
   770
      writer.writerow(dicts.identity(key_order))
1879
60e48bc3aee3 Some minor style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1878
diff changeset
   771
60e48bc3aee3 Some minor style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1878
diff changeset
   772
      # encode the data to UTF-8 to ensure compatibiliy
1878
25116444358c Fix missing utf encoding of data when exporting lists to csv file.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1864
diff changeset
   773
      for row_dict in data:
25116444358c Fix missing utf encoding of data when exporting lists to csv file.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1864
diff changeset
   774
        for key in row_dict.keys():
2002
7698408e10a5 Fix csv export of properties that are not string (like datetime.time). Also replace name_on_documents with name helper property.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1991
diff changeset
   775
          value = row_dict[key]
2076
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   776
          if isinstance(value, basestring):
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   777
            row_dict[key] = value.encode("utf-8")
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   778
          else:
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   779
            row_dict[key] = str(value)
1878
25116444358c Fix missing utf encoding of data when exporting lists to csv file.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1864
diff changeset
   780
        writer.writerow(row_dict)
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   781
    else:
2076
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   782
      writer = csv.writer(file_handler, dialect='excel')
1879
60e48bc3aee3 Some minor style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1878
diff changeset
   783
60e48bc3aee3 Some minor style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1878
diff changeset
   784
      # encode the data to UTF-8 to ensure compatibiliy
1878
25116444358c Fix missing utf encoding of data when exporting lists to csv file.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1864
diff changeset
   785
      for row in data:
2002
7698408e10a5 Fix csv export of properties that are not string (like datetime.time). Also replace name_on_documents with name helper property.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1991
diff changeset
   786
        if row:
7698408e10a5 Fix csv export of properties that are not string (like datetime.time). Also replace name_on_documents with name helper property.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1991
diff changeset
   787
          writer.writerow(row.encode("utf-8"))
7698408e10a5 Fix csv export of properties that are not string (like datetime.time). Also replace name_on_documents with name helper property.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1991
diff changeset
   788
        else:
7698408e10a5 Fix csv export of properties that are not string (like datetime.time). Also replace name_on_documents with name helper property.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1991
diff changeset
   789
          writer.writerow(row)
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   790
2076
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   791
    data = file_handler.getvalue()
1806
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   792
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   793
    return self.download(request, data, filename, params)
d321d5123928 Added generic csv export to all list views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1805
diff changeset
   794
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   795
  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
   796
    """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
   797
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   798
    Args:
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   799
      request: the django request object
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   800
      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
   801
      fields: the new field values
363
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   802
    """
d35ffa6ca643 Add a framework for generic views
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   803
1700
599a5ff8f422 Clean document references
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1677
diff changeset
   804
    references = self._params['references']
599a5ff8f422 Clean document references
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1677
diff changeset
   805
    for field_name, original_name, _ in references:
1268
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   806
      if field_name not in fields:
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   807
        continue
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   808
1700
599a5ff8f422 Clean document references
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1677
diff changeset
   809
      entity = fields.get('resolved_%s' % field_name)
1268
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   810
      fields[original_name] = entity
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   811
662
0e89b027b140 Make use of the new generic key_name by lookup up scope_path
Sverre Rabbelier <srabbelier@gmail.com>
parents: 653
diff changeset
   812
    # If scope_logic is not defined, this entity has no scope
677
c8f24e3078b8 Fix scope lookup for unscoped entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 668
diff changeset
   813
    if not self._params['scope_logic']:
662
0e89b027b140 Make use of the new generic key_name by lookup up scope_path
Sverre Rabbelier <srabbelier@gmail.com>
parents: 653
diff changeset
   814
      return
0e89b027b140 Make use of the new generic key_name by lookup up scope_path
Sverre Rabbelier <srabbelier@gmail.com>
parents: 653
diff changeset
   815
714
3e2ce3d8057a Add missing dots in docstrings, proper sorting of imports and small docstring typo fixes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 710
diff changeset
   816
    # If this entity is unscoped, do not try to retrieve a scope
701
d6f5d34cff5a If an entity is unscoped, do not try to retrieve a scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 677
diff changeset
   817
    if 'scope_path' not in fields:
d6f5d34cff5a If an entity is unscoped, do not try to retrieve a scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 677
diff changeset
   818
      return
d6f5d34cff5a If an entity is unscoped, do not try to retrieve a scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 677
diff changeset
   819
1427
00a118a442f7 Scope is already set to entity.scope, so no need to overwrite in base._editPost().
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1424
diff changeset
   820
    if not entity:
1424
f8c762a33a43 Since scope does not change we do not need to call getFromKeyName every time.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1414
diff changeset
   821
      scope = self._params['scope_logic'].logic.getFromKeyName(
f8c762a33a43 Since scope does not change we do not need to call getFromKeyName every time.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1414
diff changeset
   822
          fields['scope_path'])
f8c762a33a43 Since scope does not change we do not need to call getFromKeyName every time.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1414
diff changeset
   823
      fields['scope'] = scope
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   824
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   825
  def _public(self, request, entity, context):
880
d67557c612de Fixed typo in r1446.
Todd Larsen <tlarsen@google.com>
parents: 876
diff changeset
   826
    """Performs any required processing to get an entity's public page.
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   827
1991
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   828
    Should return True iff the public page should be displayed.
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   829
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   830
    Args:
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   831
      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
   832
      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
   833
      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
   834
    """
1991
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   835
3aeda3007467 Make _public return True iff public page should be shown
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1967
diff changeset
   836
    return True
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
   837
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   838
  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
   839
    """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
   840
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   841
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   842
      request: the django request object
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   843
      entity: the entity to get
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   844
      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
   845
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   846
662
0e89b027b140 Make use of the new generic key_name by lookup up scope_path
Sverre Rabbelier <srabbelier@gmail.com>
parents: 653
diff changeset
   847
    # fill in the email field with the data from the entity
677
c8f24e3078b8 Fix scope lookup for unscoped entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 668
diff changeset
   848
    if 'scope_path' in form.fields:
c8f24e3078b8 Fix scope lookup for unscoped entities
Sverre Rabbelier <srabbelier@gmail.com>
parents: 668
diff changeset
   849
      form.fields['scope_path'].initial = entity.scope_path
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   850
1700
599a5ff8f422 Clean document references
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1677
diff changeset
   851
    for field_name, _, getter in self._params['references']:
1268
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   852
      try:
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   853
        field = getter(entity)
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   854
        form.fields[field_name].initial = field.link_id if field else None
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   855
      except db.Error:
2076
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   856
        # TODO(Pawel.Solyga): use logging to log exception
1cd180cc56c9 Style fixes and removal of unused imports in soc.views.models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2021
diff changeset
   857
        return
1268
9d092b702f92 Resolve references if they are specified
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1227
diff changeset
   858
1414
69c812b91d37 Redone form field filling via request.GET to work on multiple fields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1400
diff changeset
   859
    for field, value in request.GET.iteritems():
69c812b91d37 Redone form field filling via request.GET to work on multiple fields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1400
diff changeset
   860
      if field in form.fields:
69c812b91d37 Redone form field filling via request.GET to work on multiple fields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1400
diff changeset
   861
        form.fields[field].initial = value
934
9fcc08971efe Add a 'pick' view to the appropriate views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 933
diff changeset
   862
497
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   863
  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
   864
    """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
   865
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   866
    Args:
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   867
      request: the django request object
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   868
      seed: the fields to seed the create page with
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   869
    """
1414
69c812b91d37 Redone form field filling via request.GET to work on multiple fields.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1400
diff changeset
   870
    pass
497
73666a7cd718 Added a default _editSeed method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 495
diff changeset
   871
1269
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   872
  def _editContext(self, request, context):
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   873
    """Performs any required processing on the context for edit pages.
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   874
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   875
    Args:
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   876
      request: the django request object
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   877
      context: the context dictionary that will be used
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   878
    """
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   879
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   880
    pass
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   881
1339
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   882
  def _constructResponse(self, request, entity, context,
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   883
                         form, params, template=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
   884
    """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
   885
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   886
    Args:
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   887
      request: the django request object
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   888
      entity: the entity that is used and set in the context
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   889
      context: the context to be used
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   890
      form: the form that will be used and set in the context
471
dcb1f7821b39 Remove stop parameter from home_settings and site_settings views. This is something Sverre didn't mean to commit :-) Add missing params description to the models Base class docs.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   891
      params: a dict with params for this View
1339
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   892
      template: if specified, this template is
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   893
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   894
    Params usage:
2021
3ab5d0d73f38 Fixed some typo in app/soc/views/models/base.py
Chen Lunpeng <forever.clp@gmail.com>
parents: 2002
diff changeset
   895
      name: The name value is used to set the entity_type
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   896
       value in the context so that the template can refer to it.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   897
      name_plural: same as name, but used to set entity_type_plural
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   898
      name_short: same as name, but used to set entity_type_short
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   899
      url_name: same as name, but used to set entity_type_url
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   900
      edit_template: The edit_template value is used as template when
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   901
        there is an existing entity to display the edit page for the
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   902
        specified entity.
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   903
      create_template: similar to edit_template, but is used when
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   904
        there is no existing entity.
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   905
    """
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   906
2193
0b6ec4a223cf Comment out unused variables in soc.views.models.base module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2164
diff changeset
   907
    # logic = params['logic']
2164
94e498e3dd44 Brown paper bag fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2161
diff changeset
   908
    suffix = entity.key().id_or_name() if entity else None
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   909
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   910
    context['form'] = form
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   911
    context['entity'] = entity
403
d3e545a8bd26 Some more improvements to the generic view code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 402
diff changeset
   912
    context['entity_suffix'] = suffix
455
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   913
    context['entity_type'] = params['name']
4d98832b43ba Added support for providing a custom params dict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 446
diff changeset
   914
    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
   915
    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
   916
    context['entity_type_url'] = params['url_name']
1035
04229d710ce9 Allow for setting a edit_cancel_redirect
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1017
diff changeset
   917
    context['edit_cancel_redirect'] = params.get('edit_cancel_redirect')
935
09f47e08f805 Adust the as_table tag to render a pick link if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 934
diff changeset
   918
    context['return_url'] = request.path
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   919
874
30e9629bf590 Fixed two trivial bugs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 858
diff changeset
   920
    if params.get('export_content_type') and entity:
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   921
      context['export_link'] = redirects.getExportRedirect(entity, params)
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   922
1339
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   923
    if not template:
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   924
      if entity:
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   925
        template = params['edit_template']
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   926
      else:
c007dc7d00ca Make _constructResponsoe take an optional template
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1331
diff changeset
   927
        template = params['create_template']
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   928
1269
62b1e9ee46c5 Add an _editContext hook
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1268
diff changeset
   929
    self._editContext(request, context)
1497
1387f0d2cf45 Pass the seed to _constructResponse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1468
diff changeset
   930
1387f0d2cf45 Pass the seed to _constructResponse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1468
diff changeset
   931
    # remove the seed from the context before dispatching to Django
1387f0d2cf45 Pass the seed to _constructResponse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1468
diff changeset
   932
    context.pop('seed', None)
1387f0d2cf45 Pass the seed to _constructResponse
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1468
diff changeset
   933
399
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   934
    return helper.responses.respond(request, template, context)
b82852e6963e Various improvements to the generic View code
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
   935
652
02323664d532 Cleanups in preparation of general selector view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   936
  def getParams(self):
668
77f9a6ea6e67 Some style and typo fixes in different modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 663
diff changeset
   937
    """Returns this view's params attribute.
652
02323664d532 Cleanups in preparation of general selector view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   938
    """
02323664d532 Cleanups in preparation of general selector view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   939
02323664d532 Cleanups in preparation of general selector view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   940
    return self._params
02323664d532 Cleanups in preparation of general selector view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 651
diff changeset
   941
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   942
  @decorators.merge_params
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 977
diff changeset
   943
  def getSidebarMenus(self, id, user, 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
   944
    """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
   945
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   946
    Args:
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   947
      params: a dict with params for this View
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   948
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   949
    Params usage:
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   950
      The params dictionary is passed as argument to getSidebarItems
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   951
      from the soc.views.sitemap.sidebar module, see the docstring
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   952
      of _getSidebarItems on how it uses it.
493
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   953
    """
6976c4d8d0ac Refactor the sidebar to use Django templates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 492
diff changeset
   954
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 977
diff changeset
   955
    return sitemap.sidebar.getSidebarMenus(id, user, params=params)
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   956
876
0c1329d4b514 Make use of the new decorators in all applicable views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 874
diff changeset
   957
  @decorators.merge_params
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   958
  def getDjangoURLPatterns(self, params=None):
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   959
    """Retrieves a list of sidebar entries for this view
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   960
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   961
    Params usage:
614
53a3e46fc512 Moved getSidebarLinks to sitemap.sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 613
diff changeset
   962
      The params dictionary is passed to the getDjangoURLPatterns
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   963
      function in the soc.views.sitemap.sitemap module, see the
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 632
diff changeset
   964
      docstring of getDjangoURLPatterns on how it uses it.
610
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   965
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   966
    Args:
e0bd276ffd82 Documented params usage in soc/views/models/base.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 606
diff changeset
   967
      params: a dict with params for this View
494
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   968
    """
5e9c656a1b68 Refactored SiteMap to be generic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 493
diff changeset
   969
613
4880ffa9f3ba Moved getDjangoURLPatterns to sitemap.sitemap
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   970
    return sitemap.sitemap.getDjangoURLPatterns(params)
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 755
diff changeset
   971