forked from rhizomedotorg/classic.rhizome.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.py
executable file
·56 lines (45 loc) · 2.46 KB
/
options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class PublisherOptions(object):
"""Option class which instance is accessible on all models which inherit from
publisher over `PublisherModel`._publisher_meta.
Populates all fields which should be excluded when the publish method take
place.
Attribute exclude_fields may inherit fields from parents, if there are some
excluded_fields defined.
PublisherOptions are configurable over class PublisherMeta if preset in
class definition in model. If exclude_fields are defined on model instance,
value of this field will be taken, and inheritance check for exclusions will
be skipped.
"""
exclude_fields = []
def __init__(self, name, bases, publisher_meta=None):
"""Build publisher meta, and inherit stuff from bases if required
"""
if publisher_meta and getattr(publisher_meta, 'exclude_fields', None):
self.exclude_fields = getattr(publisher_meta, 'exclude_fields', [])
return
exclude_fields = set()
all_bases = []
for direct_base in bases:
all_bases.append(direct_base)
for base in direct_base.mro():
if not base in all_bases:
all_bases.append(base)
for base in reversed(all_bases):
#for direct_base in bases:
#for base in reversed(direct_base.mro()):
pmeta = getattr(base, '_publisher_meta', None) or getattr(base, 'PublisherMeta', None)
if not pmeta:
continue
base_exclude_fields = getattr(pmeta, 'exclude_fields', None)
base_exclude_fields_append = getattr(pmeta, 'exclude_fields_append', None)
if base_exclude_fields and base_exclude_fields_append:
raise ValueError, ("Model %s extends defines PublisherMeta, but " +
"both - exclude_fields and exclude_fields_append"
"are defined!") % (name,)
if base_exclude_fields:
exclude_fields = exclude_fields.union(base_exclude_fields)
elif base_exclude_fields_append:
exclude_fields = exclude_fields.union(base_exclude_fields_append)
if publisher_meta and getattr(publisher_meta, 'exclude_fields_append', None):
exclude_fields = exclude_fields.union(publisher_meta.exclude_fields_append)
self.exclude_fields = list(exclude_fields)