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