app/soc/models/base.py
author Lennard de Rijk <ljvderijk@gmail.com>
Wed, 04 Mar 2009 17:04:25 +0000
changeset 1623 8b70d6bb3f8f
parent 1308 35b75ffcbb37
child 1728 0bda51fe91bf
permissions -rw-r--r--
Renamed rankerroot to ranker_root and made some changes due to comments. Patch by: Lennard de Rijk Reviewed 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
#
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.
110
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
391
849aa913e9c8 Address comments to r844 and r845.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 385
diff changeset
    30
from soc.views.helper import forms as forms_helper
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
class ModelWithFieldAttributes(db.Model):
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
  """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
    35
  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
  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
    37
  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
    38
  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
    39
  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
    40
  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
    41
  attached to each corresponding Form field.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  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
    44
  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
    45
  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
    46
  be familiar to view creators.  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
  """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
  _fields_cache = None
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    50
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    51
  def toDict(self):
1032
ed83d42d1ed7 Style fixes in soc.views.helper.redirects and soc.models.base modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 977
diff changeset
    52
    """Returns a dict with all StringProperty values of this entity.
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    53
    """
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    54
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    55
    result = {}
1213
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    56
    props = self.properties()
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    57
1213
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    58
    for key, value in props.iteritems():
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    59
      # Skip everything but StringProperties
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    60
      if not isinstance(value, db.StringProperty):
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    61
        continue
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    62
      result[key] = getattr(self, key)
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    63
1213
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    64
    if hasattr(self, 'name'):
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    65
      name_prop = getattr(self, 'name')
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    66
      if callable(name_prop):
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    67
        result['name'] = name_prop()
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    68
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    69
    return result
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    70
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
  @classmethod
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
  def fields(cls):
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
    """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
    74
    
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    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
    76
    Property attribute naming, accessing, for example:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
      {{ entity.property.verbose_name }}
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
    is accomplished using:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
      {{ entity.fields.property.label }}
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
    Args:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
      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
    83
        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
    84
        template engine.
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
    Returns:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
      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
    88
      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
    89
      Property itself via the Model entity.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
    """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
    if not cls._fields_cache or (cls != cls._fields_cache.__class__.Meta.model):
751
16dffe0b6336 Fix inheritance in soc.models.base module. FieldsProxy inherited from DbModelForm which was deleted in previous commits (replace that with BaseForm).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 409
diff changeset
    92
      class FieldsProxy(forms_helper.BaseForm):
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
        """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
    94
        """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
      
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
        class Meta:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
          """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
    98
          """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
          #: 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
   100
          model = cls
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
      
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
      cls._fields_cache = FieldsProxy()
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 391
diff changeset
   103
    return cls._fields_cache