root / snf-astakos-app / astakos / im / tables.py @ a442c81d
History | View | Annotate | Download (14.3 kB)
1 |
# Copyright 2011-2014 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 django.utils.translation import ugettext as _ |
35 |
from django.utils.safestring import mark_safe |
36 |
from django.template import Context, Template |
37 |
from django.template.loader import render_to_string |
38 |
|
39 |
from django_tables2 import A |
40 |
import django_tables2 as tables |
41 |
|
42 |
from astakos.im.models import * |
43 |
from astakos.im.util import truncatename |
44 |
from astakos.im.functions import can_join_request, membership_allowed_actions |
45 |
|
46 |
|
47 |
DEFAULT_DATE_FORMAT = "d/m/Y"
|
48 |
|
49 |
|
50 |
class LinkColumn(tables.LinkColumn): |
51 |
|
52 |
def __init__(self, *args, **kwargs): |
53 |
self.coerce = kwargs.pop('coerce', None) |
54 |
self.append = kwargs.pop('append', None) |
55 |
super(LinkColumn, self).__init__(*args, **kwargs) |
56 |
|
57 |
def render(self, value, record, bound_column): |
58 |
link = super(LinkColumn, self).render(value, record, bound_column) |
59 |
extra = ''
|
60 |
if self.append: |
61 |
if callable(self.append): |
62 |
extra = self.append(record, bound_column)
|
63 |
else:
|
64 |
extra = self.append
|
65 |
return mark_safe(link + extra)
|
66 |
|
67 |
def render_link(self, uri, text, attrs=None): |
68 |
if self.coerce: |
69 |
text = self.coerce(text)
|
70 |
return super(LinkColumn, self).render_link(uri, text, attrs) |
71 |
|
72 |
|
73 |
# Helper columns
|
74 |
class RichLinkColumn(tables.TemplateColumn): |
75 |
|
76 |
method = 'POST'
|
77 |
|
78 |
confirm_prompt = _('Yes')
|
79 |
cancel_prompt = _('No')
|
80 |
confirm = True
|
81 |
|
82 |
prompt = _('Confirm action ?')
|
83 |
|
84 |
action_tpl = None
|
85 |
action = _('Action')
|
86 |
extra_context = lambda record, table, column: {}
|
87 |
|
88 |
url = None
|
89 |
url_args = () |
90 |
resolve_func = None
|
91 |
|
92 |
def __init__(self, *args, **kwargs): |
93 |
kwargs['template_name'] = kwargs.get('template_name', |
94 |
'im/table_rich_link_column.html')
|
95 |
for attr in ['method', 'confirm_prompt', |
96 |
'cancel_prompt', 'prompt', 'url', |
97 |
'url_args', 'action', 'confirm', |
98 |
'resolve_func', 'extra_context']: |
99 |
setattr(self, attr, kwargs.pop(attr, getattr(self, attr))) |
100 |
|
101 |
super(RichLinkColumn, self).__init__(*args, **kwargs) |
102 |
|
103 |
def render(self, record, table, value, bound_column, **kwargs): |
104 |
# If the table is being rendered using `render_table`, it hackily
|
105 |
# attaches the context to the table as a gift to `TemplateColumn`. If
|
106 |
# the table is being rendered via `Table.as_html`, this won't exist.
|
107 |
content = ''
|
108 |
for extra_context in self.get_template_context(record, table, value, |
109 |
bound_column, **kwargs): |
110 |
context = getattr(table, 'context', Context()) |
111 |
context.update(extra_context) |
112 |
try:
|
113 |
if self.template_code: |
114 |
content += Template(self.template_code).render(context)
|
115 |
else:
|
116 |
content += render_to_string(self.template_name, context)
|
117 |
finally:
|
118 |
context.pop() |
119 |
|
120 |
return mark_safe(content)
|
121 |
|
122 |
def get_confirm(self, record, table): |
123 |
if callable(self.confirm): |
124 |
return self.confirm(record, table) |
125 |
return self.confirm |
126 |
|
127 |
def resolved_url(self, record, table): |
128 |
if callable(self.url): |
129 |
return self.url(record, table) |
130 |
|
131 |
if not self.url: |
132 |
return '#' |
133 |
|
134 |
args = list(self.url_args) |
135 |
for index, arg in enumerate(args): |
136 |
if isinstance(arg, A): |
137 |
args[index] = arg.resolve(record) |
138 |
return reverse(self.url, args=args) |
139 |
|
140 |
def get_action(self, record, table): |
141 |
if callable(self.action): |
142 |
return self.action(record, table) |
143 |
return self.action |
144 |
|
145 |
def get_prompt(self, record, table): |
146 |
if callable(self.prompt): |
147 |
return self.prompt(record, table) |
148 |
return self.prompt |
149 |
|
150 |
def get_template_context(self, record, table, value, bound_column, |
151 |
**kwargs): |
152 |
context = {'default': bound_column.default,
|
153 |
'record': record,
|
154 |
'value': value,
|
155 |
'col': self, |
156 |
'url': self.resolved_url(record, table), |
157 |
'prompt': self.get_prompt(record, table), |
158 |
'action': self.get_action(record, table), |
159 |
'confirm': self.get_confirm(record, table) |
160 |
} |
161 |
|
162 |
# decide whether to return dict or a list of dicts in case we want to
|
163 |
# display multiple actions within a cell.
|
164 |
if self.extra_context: |
165 |
contexts = [] |
166 |
extra_contexts = self.extra_context(record, table, self) |
167 |
if isinstance(extra_contexts, list): |
168 |
for extra_context in extra_contexts: |
169 |
newcontext = dict(context)
|
170 |
newcontext.update(extra_context) |
171 |
contexts.append(newcontext) |
172 |
else:
|
173 |
context.update(extra_contexts) |
174 |
contexts = [context] |
175 |
else:
|
176 |
contexts = [context] |
177 |
|
178 |
return contexts
|
179 |
|
180 |
|
181 |
def action_extra_context(project, table, self): |
182 |
user = table.user |
183 |
url, action, confirm, prompt = '', '', True, '' |
184 |
|
185 |
membership = table.memberships.get(project.id) |
186 |
if membership is not None: |
187 |
allowed = membership_allowed_actions(membership, user) |
188 |
if 'leave' in allowed: |
189 |
url = reverse('astakos.im.views.project_leave',
|
190 |
args=(membership.project.uuid,)) |
191 |
action = _('Leave')
|
192 |
confirm = True
|
193 |
prompt = _('Are you sure you want to leave from the project?')
|
194 |
elif 'cancel' in allowed: |
195 |
url = reverse('project_cancel_join',
|
196 |
args=(project.uuid,)) |
197 |
action = _('Cancel')
|
198 |
confirm = True
|
199 |
prompt = _('Are you sure you want to cancel the join request?')
|
200 |
|
201 |
if can_join_request(project, user, membership):
|
202 |
url = reverse('project_join', args=(project.uuid,))
|
203 |
action = _('Join')
|
204 |
confirm = True
|
205 |
prompt = _('Are you sure you want to join this project?')
|
206 |
|
207 |
return {'action': action, |
208 |
'confirm': confirm,
|
209 |
'url': url,
|
210 |
'prompt': prompt}
|
211 |
|
212 |
|
213 |
class UserTable(tables.Table): |
214 |
|
215 |
def __init__(self, *args, **kwargs): |
216 |
self.user = None |
217 |
|
218 |
if 'request' in kwargs and kwargs.get('request').user: |
219 |
self.user = kwargs.get('request').user |
220 |
|
221 |
if 'user' in kwargs: |
222 |
self.user = kwargs.pop('user') |
223 |
|
224 |
super(UserTable, self).__init__(*args, **kwargs) |
225 |
|
226 |
|
227 |
def project_name_append(project, column): |
228 |
if project.state != project.UNINITIALIZED and \ |
229 |
project.last_application is not None and \ |
230 |
project.last_application.state == ProjectApplication.PENDING: |
231 |
return mark_safe("<br /><i class='tiny'>%s</i>" % |
232 |
_('modifications pending'))
|
233 |
return u'' |
234 |
|
235 |
|
236 |
# Table classes
|
237 |
class UserProjectsTable(UserTable): |
238 |
|
239 |
def __init__(self, *args, **kwargs): |
240 |
self.pending_apps = kwargs.pop('pending_apps') |
241 |
self.memberships = kwargs.pop('memberships') |
242 |
self.accepted = kwargs.pop('accepted') |
243 |
self.requested = kwargs.pop('requested') |
244 |
super(UserProjectsTable, self).__init__(*args, **kwargs) |
245 |
|
246 |
caption = _('My projects')
|
247 |
|
248 |
name = LinkColumn('project_detail',
|
249 |
coerce=lambda x: truncatename(x, 25), |
250 |
append=project_name_append, |
251 |
args=(A('uuid'),),
|
252 |
orderable=False,
|
253 |
accessor='realname')
|
254 |
|
255 |
creation_date = tables.DateColumn(verbose_name=_('Application'),
|
256 |
format=DEFAULT_DATE_FORMAT, |
257 |
orderable=False,
|
258 |
accessor='creation_date')
|
259 |
end_date = tables.DateColumn(verbose_name=_('Expiration'),
|
260 |
format=DEFAULT_DATE_FORMAT, |
261 |
orderable=False,
|
262 |
accessor='end_date')
|
263 |
members_count_f = tables.Column(verbose_name=_("Members"),
|
264 |
empty_values=(), |
265 |
orderable=False)
|
266 |
membership_status = tables.Column(verbose_name=_("Status"),
|
267 |
empty_values=(), |
268 |
orderable=False)
|
269 |
project_action = RichLinkColumn(verbose_name=_('Action'),
|
270 |
extra_context=action_extra_context, |
271 |
orderable=False)
|
272 |
|
273 |
def render_membership_status(self, record, *args, **kwargs): |
274 |
if self.user.owns_project(record) or self.user.is_project_admin(): |
275 |
return record.state_display()
|
276 |
else:
|
277 |
m = self.memberships.get(record.id)
|
278 |
if m:
|
279 |
return m.user_friendly_state_display()
|
280 |
return _('Not a member') |
281 |
|
282 |
def render_members_count_f(self, record, *args, **kwargs): |
283 |
append = ""
|
284 |
project = record |
285 |
if project is None: |
286 |
append = mark_safe("<i class='tiny'>%s</i>" % (_('pending'),)) |
287 |
|
288 |
c = len(self.requested.get(project.id, [])) |
289 |
if c > 0: |
290 |
pending_members_url = reverse( |
291 |
'project_pending_members',
|
292 |
kwargs={'project_uuid': record.uuid})
|
293 |
|
294 |
pending_members = "<i class='tiny'> - %d %s</i>" % (
|
295 |
c, _('pending'))
|
296 |
if (
|
297 |
self.user.owns_project(record) or |
298 |
self.user.is_project_admin()
|
299 |
): |
300 |
pending_members = ("<i class='tiny'>" +
|
301 |
" - <a href='%s'>%d %s</a></i>" %
|
302 |
(pending_members_url, c, _('pending')))
|
303 |
append = mark_safe(pending_members) |
304 |
members_url = reverse('project_approved_members',
|
305 |
kwargs={'project_uuid': record.uuid})
|
306 |
members_count = len(self.accepted.get(project.id, [])) |
307 |
if self.user.owns_project(record) or self.user.is_project_admin(): |
308 |
members_count = '<a href="%s">%d</a>' % (members_url,
|
309 |
members_count) |
310 |
return mark_safe(str(members_count) + append) |
311 |
|
312 |
class Meta: |
313 |
sequence = ('name', 'membership_status', 'creation_date', 'end_date', |
314 |
'members_count_f', 'project_action') |
315 |
attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'} |
316 |
template = "im/table_render.html"
|
317 |
empty_text = _('No projects')
|
318 |
|
319 |
|
320 |
def member_action_extra_context(membership, table, col): |
321 |
|
322 |
context = [] |
323 |
urls, actions, prompts, confirms = [], [], [], [] |
324 |
|
325 |
if membership.project.is_deactivated():
|
326 |
return context
|
327 |
|
328 |
if membership.state == ProjectMembership.REQUESTED:
|
329 |
urls = ['project_reject_member',
|
330 |
'project_accept_member']
|
331 |
actions = [_('Reject'), _('Accept')] |
332 |
prompts = [_('Are you sure you want to reject this member?'),
|
333 |
_('Are you sure you want to accept this member?')]
|
334 |
confirms = [True, True] |
335 |
|
336 |
if membership.state in ProjectMembership.ACCEPTED_STATES: |
337 |
urls = ['project_remove_member']
|
338 |
actions = [_('Remove')]
|
339 |
prompts = [_('Are you sure you want to remove this member?')]
|
340 |
confirms = [True, True] |
341 |
|
342 |
for i, url in enumerate(urls): |
343 |
context.append(dict(url=reverse(url, args=(membership.project.uuid,
|
344 |
membership.pk,)), |
345 |
action=actions[i], prompt=prompts[i], |
346 |
confirm=confirms[i])) |
347 |
return context
|
348 |
|
349 |
|
350 |
class ProjectMembersTable(UserTable): |
351 |
input = "<input type='checkbox' name='all-none'/>"
|
352 |
check = tables.Column(accessor="person.id", verbose_name=mark_safe(input), |
353 |
orderable=False)
|
354 |
email = tables.Column(accessor="person.email", verbose_name=_('Email'), |
355 |
orderable= False)
|
356 |
status = tables.Column(accessor="state", verbose_name=_('Status'), |
357 |
orderable= False)
|
358 |
project_action = RichLinkColumn(verbose_name=_('Action'),
|
359 |
extra_context=member_action_extra_context, |
360 |
orderable=False)
|
361 |
|
362 |
def __init__(self, project, *args, **kwargs): |
363 |
self.project = project
|
364 |
super(ProjectMembersTable, self).__init__(*args, **kwargs) |
365 |
if not self.user.owns_project(self.project): |
366 |
self.exclude = ('project_action', ) |
367 |
|
368 |
def render_check(self, value, record, *args, **kwargs): |
369 |
checkbox = ("<input type='checkbox' value='%d' name ='actions'>" %
|
370 |
record.id) |
371 |
return mark_safe(checkbox)
|
372 |
|
373 |
def render_status(self, value, record, *args, **kwargs): |
374 |
return record.state_display()
|
375 |
|
376 |
class Meta: |
377 |
template = "im/table_render.html"
|
378 |
attrs = {'id': 'members-table', 'class': 'members-table alt-style'} |
379 |
empty_text = _('No members')
|