app/soc/models/base.py
author Chen Lunpeng <forever.clp@gmail.com>
Sat, 18 Oct 2008 05:59:04 +0000
changeset 381 8b9e8f5d119b
parent 316 9efdc7bc3565
child 385 6d410bf49a82
permissions -rw-r--r--
Fixed not working to edit a doc that selected from the docs list view. soc/templates/soc/docs/list/docs_row.html not full updated. Patch by: Chen Lunpeng Review by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
# 
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
# 
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Module containing enhanced db.Model classes.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
The classes in this module are intended to serve as base classes for all
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
Melange Datastore Models.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
"""
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
__authors__ = [
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
  '"Todd Larsen" <tlarsen@google.com>',
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
]
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
from google.appengine.ext import db
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 110
diff changeset
    30
from soc.views import helper
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 274
diff changeset
    31
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 110
diff changeset
    32
import soc.views.helper.forms
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
class ModelWithFieldAttributes(db.Model):
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
  """A db.Model extension that provides access to Model properties attributes.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
  Due to the way the Property class in Google App Engine implements __get__()
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
  and __set__(), it is not possible to access attributes of Model properties,
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  such as verbose_name, from within a Django template.  This class works
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
  around that limitation by creating an inner Form class per Model class,
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
  since an unbound Form object contains (most of?) the property attributes
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  attached to each corresponding Form field.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
  Some are attributes are renamed during the conversion from a Model Property
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  to a Form field; for example, verbose_name becomes label.  This is tolerable
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
  because any actual Form code refers to these new names, so they are should
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
  be familiar to view creators.  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
  """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
  _fields_cache = None
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
  @classmethod
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
  def fields(cls):
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
    """Called by the Django template engine during template instantiation.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
    
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
    Since the attribute names use the Form fields naming instead of the
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
    Property attribute naming, accessing, for example:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
      {{ entity.property.verbose_name }}
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
    is accomplished using:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
      {{ entity.fields.property.label }}
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
    
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
    Args:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
      cls: Model class, so that each Model class can create its own
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
        unbound Form the first time fields() is called by the Django
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
        template engine.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
 
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
    Returns:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
      A (created-on-first-use) unbound Form object that can be used to
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
      access Property attributes that are not accessible from the
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
      Property itself via the Model entity.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
    """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
    if not cls._fields_cache or (cls != cls._fields_cache.__class__.Meta.model):
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 110
diff changeset
    74
      class FieldsProxy(helper.forms.DbModelForm):
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
        """Form used as a proxy to access User model properties attributes.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
        """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
      
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
        class Meta:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
          """Inner Meta class that pairs the User Model with this "form".
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
          """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
          #: db.Model subclass for which to access model properties attributes
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
          model = cls
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
      
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
      cls._fields_cache = FieldsProxy()
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
    return cls._fields_cache