~replaceafill/schooltool/schooltool.rest

« back to all changes in this revision

Viewing changes to src/schooltool/rest/common.py

  • Committer: Justas Sadzevicius
  • Date: 2013-07-17 13:19:49 UTC
  • Revision ID: justas@pov.lt-20130717131949-dr7gc640jt194lw2
Initial egg with larz.restful hooked up and a simple person schema.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from zope.interface import implements, providedBy
 
3
from zope.publisher.interfaces import IPublishTraverse
 
4
from zope.publisher.interfaces import NotFound
 
5
from zope.security.checker import ProxyFactory
 
6
from zope.location.location import locate
 
7
 
 
8
from schooltool.rest.interfaces import ICommonEntry
 
9
from schooltool.rest.interfaces import ST_CONTEXT_IFC, ST_CONTEXT_ATTR
 
10
 
 
11
 
 
12
class TraverseViaGet(object):
 
13
    implements(IPublishTraverse)
 
14
 
 
15
    def publishTraverse(self, request, name):
 
16
        marker = object()
 
17
        value = self.get(name, marker)
 
18
        if (value is marker or
 
19
            value is None):
 
20
            raise NotFound(self, name, request)
 
21
        return value
 
22
 
 
23
 
 
24
class Entry(object):
 
25
    implements(ICommonEntry)
 
26
 
 
27
    __name__ = None
 
28
    __parent__ = None
 
29
    context = None
 
30
 
 
31
    def __init__(self, obj, parent=None):
 
32
        self.context = obj
 
33
        if parent is not None:
 
34
            locate(self, parent)
 
35
        self.__name__ = getattr(obj, '__name__', None)
 
36
 
 
37
    def __getattr__(self, name):
 
38
        for ifc in providedBy(self):
 
39
            if name not in ifc:
 
40
                continue
 
41
            adapter = ifc[name].queryTaggedValue(ST_CONTEXT_IFC)
 
42
            if adapter is None:
 
43
                continue
 
44
            attr = ifc[name].queryTaggedValue(ST_CONTEXT_ATTR)
 
45
            if attr is None:
 
46
                continue
 
47
            context = object.__getattribute__(self, attr)
 
48
            target = adapter(ProxyFactory(context))
 
49
            return getattr(target, name)
 
50
        raise AttributeError(self, name)
 
51
 
 
52
    def __setattr__(self, name, value):
 
53
        for ifc in providedBy(self):
 
54
            if name not in ifc:
 
55
                continue
 
56
            adapter = ifc[name].queryTaggedValue(ST_CONTEXT_IFC)
 
57
            if adapter is None:
 
58
                continue
 
59
            attr = ifc[name].queryTaggedValue(ST_CONTEXT_ATTR)
 
60
            if attr is None:
 
61
                continue
 
62
            context = object.__getattribute__(self, attr)
 
63
            target = adapter(ProxyFactory(context))
 
64
            setattr(target, name, value)
 
65
            return
 
66
        object.__setattr__(self, name, value)