[Categories] use categories from Django ORM
[mygpo.git] / mygpo / core / proxy.py
blob6e77ca075692ef8a52e4d0460ff56a4b4c18ea02
1 from abc import ABCMeta
3 from couchdbkit.ext.django.schema import DocumentMeta
7 class DocumentABCMeta(ABCMeta, DocumentMeta):
8 """ Meta class must be subclass of all parents' meta classes """
9 pass
13 def proxy_object(obj, **kwargs):
14 """ Proxies an object to make it arbitrarily modifiable
16 It is not possible to add arbitrary values to an couchdbkit Document,
17 because all values have to fit the schema and have to be JSON serializable
18 """
20 class ProxyObject(object):
21 """ Proxy for obj that can have properties of arbitrary type """
23 # ensure that proxied objects can be proxied again
24 __metaclass__ = DocumentABCMeta
26 def __init__(self, obj, **kwargs):
27 self.obj = obj
29 for key, val in kwargs.items():
30 setattr(self, key, val)
33 def __getattr__(self, attr):
34 return getattr(self.obj, attr)
37 def __eq__(self, other):
38 # if "other" is of the same type as proxied obj, compare directly
39 if isinstance(other, ProxyObject):
40 return self.obj == other.obj
42 else:
43 return self.obj == other
45 def __hash__(self):
46 return hash(self.obj)
49 cls = obj.__class__
50 cls.register(ProxyObject)
52 return ProxyObject(obj, **kwargs)