Package pyamf :: Package adapters :: Module _django_db_models_base

[hide private]
[frames] | no frames]

# Source Code for Module pyamf.adapters._django_db_models_base

      1  # Copyright (c) 2007-2009 The PyAMF Project.
      2  # See LICENSE for details.
      3
      4  """
      5  C{django.db.models} adapter module.
      6
      7  @see: U{Django Project<http://www.djangoproject.com>}
      8
      9  @since: 0.4.1
     10  """
     11
     12  from django.db.models.base import Model
     13  from django.db.models import fields
     14  from django.db.models.fields import related
     15
     16  import datetime
     17
     18  import pyamf
     19
     20 -class DjangoClassAlias(pyamf.ClassAlias):
     21 -    def getAttrs(self, obj, codec=None):
     22          static_attrs, dynamic_attrs = [], []
     23
     24          if hasattr(self, 'static_attrs'):
     25              static_attrs = self.static_attrs
     26          else:
     27              static_attrs = self.static_attrs = []
     28              self.fields = {}
     29
     30              for x in obj._meta.fields:
     31                  if x.name not in static_attrs:
     32                      self.fields[x.name] = x
     33                      static_attrs.append(x.name)
     34
     35              for k, v in self.klass.__dict__.iteritems():
     36                  if isinstance(v, property):
     37                      static_attrs.append(k)
     38                  elif isinstance(v, related.ReverseManyRelatedObjectsDescriptor):
     39                      if k not in static_attrs:
     40                          self.fields[k] = v.field
     41                          static_attrs.append(k)
     42
     43          return static_attrs, dynamic_attrs
     44
     45 -    def _encodeValue(self, field, value):
     46          if value is fields.NOT_PROVIDED:
     47              return pyamf.Undefined
     48
     49          # deal with dates ..
     50          if isinstance(field, fields.DateTimeField):
     51              return value
     52          elif isinstance(field, fields.DateField):
     53              return datetime.datetime(value.year, value.month, value.day, 0, 0, 0)
     54          elif isinstance(field, fields.TimeField):
     55              return datetime.datetime(1970, 1, 1,
     56                  value.hour, value.minute, value.second, value.microsecond)
     57
     58          return value
     59
     60 -    def _decodeValue(self, field, value):
     61          if value is pyamf.Undefined:
     62              return fields.NOT_PROVIDED
     63
     64          # deal with dates
     65          if isinstance(field, fields.DateTimeField):
     66              return value
     67          elif isinstance(field, fields.DateField):
     68              return datetime.date(value.year, value.month, value.day)
     69          elif isinstance(field, fields.TimeField):
     70              return datetime.time(value.hour, value.minute, value.second, value.microsecond)
     71
     72          return value
     73
     74 -    def getAttributes(self, obj, codec=None):
     75          from django.db import models
     76
     77          san, dan = self.getAttrs(obj)
     78          static_attrs, dynamic_attrs = {}, {}
     79
     80          for name in san:
     81              if name not in self.fields.keys():
     82                  static_attrs[name] = getattr(obj, name)
     83              else:
     84                  prop = self.fields[name]
     85
     86                  if isinstance(prop, related.ManyToManyField):
     87                      static_attrs[name] = [x for x in getattr(obj, name).all()]
     88                  elif isinstance(prop, models.ForeignKey):
     89                      if '_%s_cache' % name in obj.__dict__:
     90                          static_attrs[name] = getattr(obj, name)
     91                      else:
     92                          static_attrs[name] = None
     93                  else:
     94                      static_attrs[name] = self._encodeValue(prop, getattr(obj, name))
     95
     96          return static_attrs, dynamic_attrs
     97
     98 -    def applyAttributes(self, obj, attrs, codec=None):
     99          if not hasattr(self, 'static_attrs'):
    100              self.getAttrs(obj)
    101
    102          for n, f in self.fields.iteritems():
    103              attrs[f.attname] = self._decodeValue(f, attrs[n])
    104
    105          for f in self.klass.__dict__:
    106              prop = self.klass.__dict__[f]
    107
    108              if isinstance(prop, property) and f in attrs.keys():
    109                  if prop.fset is None:
    110                      del attrs[f]
    111
    112          return pyamf.ClassAlias.applyAttributes(self, obj, attrs)
    113
    114  pyamf.register_alias_type(DjangoClassAlias, Model)
    115