app/soc/models/base.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 24 Oct 2009 16:23:31 -0700
changeset 3043 187c1709756b
parent 2985 f1ed0ecd0128
permissions -rw-r--r--
GSoC Views
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>',
2767
4011d44ba0b6 Changed to_dict to call all callable values when their key is in field_names.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2074
diff changeset
    25
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
110
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
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
from google.appengine.ext import db
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
391
849aa913e9c8 Address comments to r844 and r845.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 385
diff changeset
    31
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
    32
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
class ModelWithFieldAttributes(db.Model):
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
  """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
    36
  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
  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
    38
  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
    39
  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
    40
  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
    41
  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
    42
  attached to each corresponding Form field.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
  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
    45
  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
    46
  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
    47
  be familiar to view creators.  
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
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  _fields_cache = None
1809
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    51
  DICT_TYPES = (db.StringProperty, db.IntegerProperty)
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    52
1809
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    53
  def toDict(self, field_names=None):
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    54
    """Returns a dict with all specified values of this entity.
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    55
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    56
    Args:
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    57
      field_names: the fields that should be included, defaults to
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    58
        all fields that are of a type that is in DICT_TYPES.
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    59
    """
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    60
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    61
    result = {}
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    62
1809
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    63
    if not field_names:
2074
5c75312566d5 Fix too long lines, style and remove unused imports in soc.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2059
diff changeset
    64
      props = self.properties().iteritems()
5c75312566d5 Fix too long lines, style and remove unused imports in soc.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2059
diff changeset
    65
      field_names = [k for k, v in props if isinstance(v, self.DICT_TYPES)]
1809
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    66
2059
4037b147ed10 Make it possible to retrieve all properties in toDict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1845
diff changeset
    67
    for key in field_names:
1809
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    68
      # Skip everything that is not valid
2059
4037b147ed10 Make it possible to retrieve all properties in toDict
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1845
diff changeset
    69
      if not hasattr(self, key):
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    70
        continue
1809
66aec0241d61 Make it possible to specify the key order for csv export
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1728
diff changeset
    71
2767
4011d44ba0b6 Changed to_dict to call all callable values when their key is in field_names.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2074
diff changeset
    72
      value = getattr(self, key)
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    73
2767
4011d44ba0b6 Changed to_dict to call all callable values when their key is in field_names.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2074
diff changeset
    74
      if callable(value):
4011d44ba0b6 Changed to_dict to call all callable values when their key is in field_names.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2074
diff changeset
    75
        value = value()
4011d44ba0b6 Changed to_dict to call all callable values when their key is in field_names.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2074
diff changeset
    76
4011d44ba0b6 Changed to_dict to call all callable values when their key is in field_names.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2074
diff changeset
    77
      result[key] = value
1213
365b4a2df40d Make toDict "dereference" the 'name' property if it is present
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1032
diff changeset
    78
977
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    79
    return result
d212d5f4c41a Have /entity/pick return a JSON object instead of a user view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 751
diff changeset
    80
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
  @classmethod
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
  def fields(cls):
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
    """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
    84
    
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
    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
    86
    Property attribute naming, accessing, for example:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
      {{ entity.property.verbose_name }}
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
    is accomplished using:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
      {{ entity.fields.property.label }}
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
    Args:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
      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
    93
        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
    94
        template engine.
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
    Returns:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
      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
    98
      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
    99
      Property itself via the Model entity.
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
    """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
    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
   102
      class FieldsProxy(forms_helper.BaseForm):
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
        """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
   104
        """
2985
f1ed0ecd0128 Fixed whitespace in base model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2767
diff changeset
   105
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
        class Meta:
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
          """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
   108
          """
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
          #: 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
   110
          model = cls
2985
f1ed0ecd0128 Fixed whitespace in base model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2767
diff changeset
   111
110
e310681d5509 Base classes for all Datastore Models in Melange.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   112
      cls._fields_cache = FieldsProxy()
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 391
diff changeset
   113
    return cls._fields_cache