root / snf-astakos-app / astakos / im / tables.py @ 05617ab9
History | View | Annotate | Download (12.6 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from collections import defaultdict |
35 |
|
36 |
from django.utils.translation import ugettext as _ |
37 |
from django.utils.safestring import mark_safe |
38 |
from django.template import Context, Template |
39 |
from django.template.loader import render_to_string |
40 |
from django.core.exceptions import PermissionDenied |
41 |
|
42 |
from django_tables2 import A |
43 |
import django_tables2 as tables |
44 |
|
45 |
from astakos.im.models import * |
46 |
from astakos.im.templatetags.filters import truncatename |
47 |
from astakos.im.functions import do_join_project_checks, \ |
48 |
do_leave_project_checks
|
49 |
|
50 |
DEFAULT_DATE_FORMAT = "d/m/Y"
|
51 |
|
52 |
|
53 |
class LinkColumn(tables.LinkColumn): |
54 |
|
55 |
def __init__(self, *args, **kwargs): |
56 |
self.coerce = kwargs.pop('coerce', None) |
57 |
self.append = kwargs.pop('append', None) |
58 |
super(LinkColumn, self).__init__(*args, **kwargs) |
59 |
|
60 |
def render(self, value, record, bound_column): |
61 |
link = super(LinkColumn, self).render(value, record, bound_column) |
62 |
extra = ''
|
63 |
if self.append: |
64 |
if callable(self.append): |
65 |
extra = self.append(record, bound_column)
|
66 |
else:
|
67 |
extra = self.append
|
68 |
return mark_safe(link + extra)
|
69 |
|
70 |
def render_link(self, uri, text, attrs=None): |
71 |
if self.coerce: |
72 |
text = self.coerce(text)
|
73 |
return super(LinkColumn, self).render_link(uri, text, attrs) |
74 |
|
75 |
|
76 |
# Helper columns
|
77 |
class RichLinkColumn(tables.TemplateColumn): |
78 |
|
79 |
method = 'POST'
|
80 |
|
81 |
confirm_prompt = _('Yes')
|
82 |
cancel_prompt = _('No')
|
83 |
confirm = True
|
84 |
|
85 |
prompt = _('Confirm action ?')
|
86 |
|
87 |
action_tpl = None
|
88 |
action = _('Action')
|
89 |
extra_context = lambda record, table, column: {}
|
90 |
|
91 |
url = None
|
92 |
url_args = () |
93 |
resolve_func = None
|
94 |
|
95 |
def __init__(self, *args, **kwargs): |
96 |
kwargs['template_name'] = kwargs.get('template_name', |
97 |
'im/table_rich_link_column.html')
|
98 |
for attr in ['method', 'confirm_prompt', |
99 |
'cancel_prompt', 'prompt', 'url', |
100 |
'url_args', 'action', 'confirm', |
101 |
'resolve_func', 'extra_context']: |
102 |
setattr(self, attr, kwargs.pop(attr, getattr(self, attr))) |
103 |
|
104 |
super(RichLinkColumn, self).__init__(*args, **kwargs) |
105 |
|
106 |
def render(self, record, table, value, bound_column, **kwargs): |
107 |
# If the table is being rendered using `render_table`, it hackily
|
108 |
# attaches the context to the table as a gift to `TemplateColumn`. If
|
109 |
# the table is being rendered via `Table.as_html`, this won't exist.
|
110 |
content = ''
|
111 |
for extra_context in self.get_template_context(record, table, value, |
112 |
bound_column, **kwargs): |
113 |
context = getattr(table, 'context', Context()) |
114 |
context.update(extra_context) |
115 |
try:
|
116 |
if self.template_code: |
117 |
content += Template(self.template_code).render(context)
|
118 |
else:
|
119 |
content += render_to_string(self.template_name, context)
|
120 |
finally:
|
121 |
context.pop() |
122 |
|
123 |
return mark_safe(content)
|
124 |
|
125 |
def get_confirm(self, record, table): |
126 |
if callable(self.confirm): |
127 |
return self.confirm(record, table) |
128 |
return self.confirm |
129 |
|
130 |
def resolved_url(self, record, table): |
131 |
if callable(self.url): |
132 |
return self.url(record, table) |
133 |
|
134 |
if not self.url: |
135 |
return '#' |
136 |
|
137 |
args = list(self.url_args) |
138 |
for index, arg in enumerate(args): |
139 |
if isinstance(arg, A): |
140 |
args[index] = arg.resolve(record) |
141 |
return reverse(self.url, args=args) |
142 |
|
143 |
def get_action(self, record, table): |
144 |
if callable(self.action): |
145 |
return self.action(record, table) |
146 |
return self.action |
147 |
|
148 |
def get_prompt(self, record, table): |
149 |
if callable(self.prompt): |
150 |
return self.prompt(record, table) |
151 |
return self.prompt |
152 |
|
153 |
def get_template_context(self, record, table, value, bound_column, **kwargs): |
154 |
context = {'default': bound_column.default,
|
155 |
'record': record,
|
156 |
'value': value,
|
157 |
'col': self, |
158 |
'url': self.resolved_url(record, table), |
159 |
'prompt': self.get_prompt(record, table), |
160 |
'action': self.get_action(record, table), |
161 |
'confirm': self.get_confirm(record, table) |
162 |
} |
163 |
|
164 |
# decide whether to return dict or a list of dicts in case we want to
|
165 |
# display multiple actions within a cell.
|
166 |
if self.extra_context: |
167 |
contexts = [] |
168 |
extra_contexts = self.extra_context(record, table, self) |
169 |
if isinstance(extra_contexts, list): |
170 |
for extra_context in extra_contexts: |
171 |
newcontext = dict(context)
|
172 |
newcontext.update(extra_context) |
173 |
contexts.append(newcontext) |
174 |
else:
|
175 |
context.update(extra_contexts) |
176 |
contexts = [context] |
177 |
else:
|
178 |
contexts = [context] |
179 |
|
180 |
return contexts
|
181 |
|
182 |
|
183 |
def action_extra_context(application, table, self): |
184 |
user = table.user |
185 |
url, action, confirm, prompt = '', '', True, '' |
186 |
append_url = ''
|
187 |
|
188 |
can_join = can_leave = False
|
189 |
member_status = application.user_status(user) |
190 |
project = application.get_project() |
191 |
|
192 |
if project:
|
193 |
try:
|
194 |
do_join_project_checks(project) |
195 |
can_join = True
|
196 |
except PermissionDenied:
|
197 |
pass
|
198 |
|
199 |
try:
|
200 |
do_leave_project_checks(project) |
201 |
can_leave = True
|
202 |
except PermissionDenied:
|
203 |
pass
|
204 |
|
205 |
if can_leave and user.is_project_accepted_member(application): |
206 |
url = 'astakos.im.views.project_leave'
|
207 |
action = _('Leave')
|
208 |
confirm = True
|
209 |
prompt = _('Are you sure you want to leave from the project ?')
|
210 |
elif can_join and user.is_project_member(application): |
211 |
url = 'astakos.im.views.project_join'
|
212 |
action = _('Join')
|
213 |
confirm = True
|
214 |
prompt = _('Are you sure you want to join this project ?')
|
215 |
else:
|
216 |
action = ''
|
217 |
confirm = False
|
218 |
url = None
|
219 |
|
220 |
url = reverse(url, args=(application.pk, )) + append_url if url else '' |
221 |
return {'action': action, |
222 |
'confirm': confirm,
|
223 |
'url': url,
|
224 |
'prompt': prompt}
|
225 |
|
226 |
|
227 |
class UserTable(tables.Table): |
228 |
|
229 |
def __init__(self, *args, **kwargs): |
230 |
self.user = None |
231 |
|
232 |
if 'request' in kwargs and kwargs.get('request').user: |
233 |
self.user = kwargs.get('request').user |
234 |
|
235 |
if 'user' in kwargs: |
236 |
self.user = kwargs.pop('user') |
237 |
|
238 |
super(UserTable, self).__init__(*args, **kwargs) |
239 |
|
240 |
|
241 |
def project_name_append(application, column): |
242 |
if application.has_pending_modifications():
|
243 |
return mark_safe("<br /><i class='tiny'>%s</i>" % \ |
244 |
_('modifications pending'))
|
245 |
return u'' |
246 |
|
247 |
# Table classes
|
248 |
class UserProjectApplicationsTable(UserTable): |
249 |
caption = _('My projects')
|
250 |
|
251 |
name = LinkColumn('astakos.im.views.project_detail',
|
252 |
coerce=lambda x: truncatename(x, 25), |
253 |
append=project_name_append, |
254 |
args=(A('pk'),))
|
255 |
issue_date = tables.DateColumn(verbose_name=_('Applied at'), format=DEFAULT_DATE_FORMAT)
|
256 |
start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT) |
257 |
end_date = tables.DateColumn(verbose_name=_('Expires at'), format=DEFAULT_DATE_FORMAT)
|
258 |
members_count = tables.Column(verbose_name=_("Members"), default=0, |
259 |
sortable=False)
|
260 |
membership_status = tables.Column(verbose_name=_("Status"), empty_values=(),
|
261 |
sortable=False)
|
262 |
project_action = RichLinkColumn(verbose_name=_('Action'),
|
263 |
extra_context=action_extra_context, |
264 |
sortable=False)
|
265 |
|
266 |
|
267 |
def render_membership_status(self, record, *args, **kwargs): |
268 |
if self.user.owns_project(record): |
269 |
return record.state_display()
|
270 |
else:
|
271 |
status = record.user_status(self.user)
|
272 |
return record.user_status_display(self.user) |
273 |
|
274 |
class Meta: |
275 |
model = ProjectApplication |
276 |
fields = ('name', 'membership_status', 'issue_date', 'end_date', 'members_count') |
277 |
attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'} |
278 |
template = "im/table_render.html"
|
279 |
empty_text = _('No projects')
|
280 |
exclude = ('start_date', )
|
281 |
|
282 |
class ProjectModificationApplicationsTable(UserProjectApplicationsTable): |
283 |
name = LinkColumn('astakos.im.views.project_detail',
|
284 |
verbose_name=_('Action'),
|
285 |
coerce= lambda x: 'review', |
286 |
args=(A('pk'),))
|
287 |
class Meta: |
288 |
attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'} |
289 |
fields = ('issue_date', 'membership_status') |
290 |
exclude = ('start_date', 'end_date', 'members_count', 'project_action') |
291 |
|
292 |
def member_action_extra_context(membership, table, col): |
293 |
|
294 |
context = [] |
295 |
urls, actions, prompts, confirms = [], [], [], [] |
296 |
|
297 |
if membership.state == ProjectMembership.REQUESTED:
|
298 |
urls = ['astakos.im.views.project_reject_member',
|
299 |
'astakos.im.views.project_accept_member']
|
300 |
actions = [_('Reject'), _('Approve')] |
301 |
prompts = [_('Are you sure you want to reject this member ?'),
|
302 |
_('Are you sure you want to approve this member ?')]
|
303 |
confirms = [True, True] |
304 |
|
305 |
if membership.state == ProjectMembership.ACCEPTED:
|
306 |
urls = ['astakos.im.views.project_remove_member']
|
307 |
actions = [_('Remove')]
|
308 |
if table.user == membership.person:
|
309 |
actions = [_('Leave')]
|
310 |
prompts = [_('Are you sure you want to remove this member ?')]
|
311 |
confirms = [True, True] |
312 |
|
313 |
|
314 |
for i, url in enumerate(urls): |
315 |
context.append(dict(url=reverse(url, args=(table.project.pk,
|
316 |
membership.person.pk)), |
317 |
action=actions[i], prompt=prompts[i], |
318 |
confirm=confirms[i])) |
319 |
return context
|
320 |
|
321 |
class ProjectApplicationMembersTable(UserTable): |
322 |
name = tables.Column(accessor="person.last_name", verbose_name=_('Name')) |
323 |
status = tables.Column(accessor="state", verbose_name=_('Status')) |
324 |
project_action = RichLinkColumn(verbose_name=_('Action'),
|
325 |
extra_context=member_action_extra_context, |
326 |
sortable=False)
|
327 |
|
328 |
|
329 |
def __init__(self, project, *args, **kwargs): |
330 |
self.project = project
|
331 |
super(ProjectApplicationMembersTable, self).__init__(*args, **kwargs) |
332 |
if not self.user.owns_project(self.project): |
333 |
self.exclude = ('project_action', ) |
334 |
|
335 |
|
336 |
def render_name(self, value, record, *args, **kwargs): |
337 |
return record.person.realname
|
338 |
|
339 |
def render_status(self, value, *args, **kwargs): |
340 |
return USER_STATUS_DISPLAY.get(value, 'Unknown') |
341 |
|
342 |
class Meta: |
343 |
template = "im/table_render.html"
|
344 |
model = ProjectMembership |
345 |
fields = ('name', 'status') |
346 |
attrs = {'id': 'members-table', 'class': 'members-table alt-style'} |
347 |
empty_text = _('No members')
|
348 |
|