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