forked from treeio/treeio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
135 lines (108 loc) · 4.55 KB
/
Copy pathobjects.py
File metadata and controls
135 lines (108 loc) · 4.55 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# encoding: utf-8
# Copyright 2011 Tree.io Limited
# This file is part of Treeio.
# License www.tree.io/license
"""
Contact module helpers
"""
from treeio.core.models import Module
from django.utils.translation import ugettext_lazy as _
def _get_module_objects(module, current_user, related, getter_name):
try:
import_name = module.name + ".identities"
modident = __import__(import_name, fromlist=[str(module.name)])
getter = getattr(modident, getter_name)
return getter(current_user, related)
except ImportError:
pass
except AttributeError:
pass
except KeyError:
pass
return {}
def _preformat_objects(modules, objects):
"""
Formats objects returned from get_contact_objects or get_user_objects
for a more convenient output and templating, structured by module
"""
output = {}
if objects:
for module in modules:
output[module.name] = {'module': module,
'label': _(module.title),
'count': 0,
'objects': {}}
for key in objects:
if objects[key]['module'] == module:
if hasattr(objects[key]['objects'], 'count'):
output[module.name]['count'] += objects[key]['objects'].count()
objects[key]['label'] = _(objects[key]['label'])
output[module.name]['objects'][key] = objects[key]
return output
def get_contact_objects(current_user, contact, module=None, preformat=False):
"""
Returns a dictionary with keys specified as contact attributes
and values as dictionaries with labels and set of relevant objects.
Only modules enabled for the current_user are considered.
"""
objects = dict()
if module:
contact_objects = _get_module_objects(module, current_user, contact, 'get_contact_objects')
if contact_objects:
for key in contact_objects:
contact_objects[key]['module'] = module
objects.update(contact_objects)
if contact.related_user:
try:
objects.update(get_user_objects(current_user, contact.related_user.user, module))
except:
pass
modules = [module]
else:
perspective = current_user.get_perspective()
modules = perspective.modules.filter(display=True).order_by('title')
if not modules:
modules = Module.objects.filter(display=True).order_by('title')
for module in modules:
contact_objects = _get_module_objects(module, current_user, contact, 'get_contact_objects')
if contact_objects:
for key in contact_objects:
contact_objects[key]['module'] = module
objects.update(contact_objects)
if contact.related_user:
try:
objects.update(get_user_objects(current_user, contact.related_user.user, module))
except:
pass
if preformat:
return _preformat_objects(modules, objects)
return objects
def get_user_objects(current_user, user, module=None, preformat=False):
"""
Returns a dictionary with keys specified as user attributes
and values as dictionaries with labels, number of relevant objects,
and optionally the actual set of relevant objects.
Only modules enabled for the current_user are considered.
"""
objects = dict()
if module:
user_objects = _get_module_objects(module, current_user, user, 'get_user_objects')
if user_objects:
for key in user_objects:
user_objects[key]['module'] = module
objects['related_user.'+key] = user_objects[key]
modules = [module]
else:
perspective = current_user.get_perspective()
modules = perspective.modules.filter(display=True).order_by('title')
if not modules:
modules = Module.objects.filter(display=True).order_by('title')
for module in modules:
user_objects = _get_module_objects(module, current_user, user, 'get_user_objects')
if user_objects:
for key in user_objects:
user_objects[key]['module'] = module
objects['related_user.'+key] = user_objects[key]
if preformat:
return _preformat_objects(modules, objects)
return objects