-
Notifications
You must be signed in to change notification settings - Fork 0
/
menus.py
273 lines (217 loc) · 8.93 KB
/
menus.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# -*- coding: UTF-8 -*-
# Copyright (C) 2010-2011 Hervé Cauwelier <herve@itaapy.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from itools
from itools.core import thingy
from itools.database import OrQuery, PhraseQuery, AndQuery
from itools.gettext import MSG
from itools.web import get_context
# Import from ikaaro
from ikaaro.utils import CMSTemplate
# Import from crm
from base_views import Icon, StatusIcon
from utils import get_crm, get_crm_path_query, get_contact_title
class item(thingy):
title = u""
icon = None
href = None
css_class = None
selected = False
class ContextMenu(CMSTemplate):
template = '/ui/crm/generic/menu.xml'
def items(self):
raise NotImplementedError
class ContactsMenu(ContextMenu):
title = MSG(u"Related Contacts")
def get_crm_path_query(self, context):
"""Shortcut.
"""
return get_crm_path_query(get_crm(context.resource))
def get_companies(self, context):
"""Get a list of companies related to context.resource.
"""
raise NotImplementedError
def get_contacts(self, context):
"""Get a list of all contacts from these companies.
"""
company_names = self.get_companies(context)
query = AndQuery(self.get_crm_path_query(context),
PhraseQuery('format', 'contact'),
OrQuery(*[PhraseQuery('crm_p_company', company)
for company in company_names if company]))
results = context.root.search(query)
for brain in results.get_documents(sort_by='title'):
yield brain
def is_selected(self, brain, resource, context):
return brain.abspath == context.abspath
def items(self):
context = get_context()
resource = context.resource
items = []
for brain in self.get_contacts(context):
items.append(item(
title=get_contact_title(brain, context),
icon=Icon('crm16-contact'),
href=context.get_link(brain),
selected=self.is_selected(brain, resource, context)))
# New contact
if resource.class_id == 'mission':
items.append(item(
title=MSG(u"Link Existing Contact"),
icon=Icon('crm16-contact-add'),
href=';add_contacts',
selected=False))
m_contact = resource.get_property('crm_m_contact')
if m_contact:
contacts = resource.get_resource('../../contacts')
contact = contacts.get_resource(m_contact[0])
p_company = contact.get_property('crm_p_company')
items.append(item(
title=MSG(u"New Contact"),
icon=Icon('crm16-contact-add'),
href='../../contacts/?crm_p_company=' + p_company,
selected=False))
elif resource.class_id == 'contact':
p_company = resource.get_property('crm_p_company')
items.append(item(
title=MSG(u"New Contact"),
icon=Icon('crm16-contact-add'),
href='../?crm_p_company=' + p_company,
selected=False))
elif resource.class_id == 'company':
items.append(item(
title=MSG(u"New Contact"),
icon=Icon('crm16-contact-add'),
href='../../contacts/?crm_p_company=' + resource.name,
selected=False))
return items
class ContactsByMissionMenu(ContactsMenu):
def is_selected(self, brain, resource, context):
return brain.name in resource.get_property('crm_m_contact')
def get_companies(self, context):
"""From mission to companies.
"""
root = context.root
resource = context.resource
query = AndQuery(self.get_crm_path_query(context),
PhraseQuery('format', 'contact'),
OrQuery(*[PhraseQuery('name', contact)
for contact in resource.get_property('crm_m_contact')]))
results = root.search(query)
return (brain.crm_p_company for brain in results.get_documents())
class ContactsByContactMenu(ContactsMenu):
def is_selected(self, brain, resource, context):
return brain.name == resource.name
def get_companies(self, context):
"""From contact to companies.
"""
return [context.resource.get_property('crm_p_company')]
class ContactsByCompanyMenu(ContactsMenu):
def is_selected(self, brain, resource, context):
return brain.crm_p_company == resource.name
def get_companies(self, context):
"""From company to... companies.
"""
return [context.resource.name]
class MissionsMenu(ContextMenu):
title = MSG(u"Related Missions")
contact_menu = None
def items(self):
context = get_context()
resource = context.resource
abspath = resource.abspath
root = context.root
contact_names = [brain.name
for brain in self.contact_menu.get_contacts(context)]
query = AndQuery(self.contact_menu.get_crm_path_query(context),
PhraseQuery('format', 'mission'),
OrQuery(*[PhraseQuery('crm_m_contact', contact)
for contact in contact_names]))
results = root.search(query)
items = []
for brain in results.get_documents(sort_by='mtime', reverse=True):
selected = False
if resource.class_id == 'mission':
selected = brain.abspath == abspath
elif resource.class_id == 'contact':
selected = (resource.name in brain.crm_m_contact)
items.append(item(
title=brain.title,
icon=StatusIcon(brain.crm_m_status),
href=context.get_link(brain),
selected=selected))
# New mission
if resource.class_id == 'mission':
m_contact = resource.get_property('crm_m_contact')[0]
items.append(item(
title=MSG(u"New Mission"),
icon=Icon('crm16-mission-add'),
href=('../;new_mission?crm_m_contact=' + m_contact),
selected=False))
elif resource.class_id == 'contact':
items.append(item(
title=MSG(u"New Mission"),
icon=Icon('crm16-mission-add'),
href=('../../missions/;new_mission?crm_m_contact=' +
resource.name),
selected=False))
return items
class CompaniesMenu(ContextMenu):
title = u"Related Companies"
def items(self):
context = get_context()
resource = context.resource
items = []
if resource.class_id in ('contact', 'mission'):
todo = []
if resource.class_id == 'contact':
p_company = resource.get_property('crm_p_company')
if p_company:
todo.append(p_company)
elif resource.class_id == 'mission':
contacts = resource.get_resource('../../contacts')
for m_contact in resource.get_property('crm_m_contact'):
contact = contacts.get_resource(m_contact)
p_company = contact.get_property('crm_p_company')
if p_company not in todo:
todo.append(p_company)
companies = resource.get_resource('../../companies')
for p_company in todo:
company = companies.get_resource(p_company)
items.append(item(
title=company.get_property('title'),
icon=Icon('crm16-company'),
href=context.get_link(company),
selected=True))
items.append(item(
title=MSG(u"New Company"),
icon=Icon('crm16-company-add'),
href=context.get_link(companies),
selected=False))
return items
class CompanyMenu(ContextMenu):
title = MSG(u"New Company")
contact_menu = None
def items(self):
context = get_context()
resource = context.resource
items = []
if resource.class_id == 'company':
items.append(item(
title=MSG(u"New Company"),
icon=Icon('crm16-company-add'),
href='..',
selected=False))
return items