root / snf-astakos-app / astakos / im / tables.py @ 866e5768
History | View | Annotate | Download (13.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 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.templatetags.filters 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 |
append_url = ''
|
185 |
|
186 |
membership = user.get_membership(project) |
187 |
if membership is not None: |
188 |
allowed = membership_allowed_actions(membership, user) |
189 |
if 'leave' in allowed: |
190 |
url = reverse('astakos.im.views.project_leave',
|
191 |
args=(membership.id,)) |
192 |
action = _('Leave')
|
193 |
confirm = True
|
194 |
prompt = _('Are you sure you want to leave from the project?')
|
195 |
elif 'cancel' in allowed: |
196 |
url = reverse('astakos.im.views.project_cancel_member',
|
197 |
args=(membership.id,)) |
198 |
action = _('Cancel')
|
199 |
confirm = True
|
200 |
prompt = _('Are you sure you want to cancel the join request?')
|
201 |
|
202 |
if can_join_request(project, user):
|
203 |
url = reverse('astakos.im.views.project_join', args=(project.id,))
|
204 |
action = _('Join')
|
205 |
confirm = True
|
206 |
prompt = _('Are you sure you want to join this project?')
|
207 |
|
208 |
return {'action': action, |
209 |
'confirm': confirm,
|
210 |
'url': url,
|
211 |
'prompt': prompt}
|
212 |
|
213 |
|
214 |
class UserTable(tables.Table): |
215 |
|
216 |
def __init__(self, *args, **kwargs): |
217 |
self.user = None |
218 |
|
219 |
if 'request' in kwargs and kwargs.get('request').user: |
220 |
self.user = kwargs.get('request').user |
221 |
|
222 |
if 'user' in kwargs: |
223 |
self.user = kwargs.pop('user') |
224 |
|
225 |
super(UserTable, self).__init__(*args, **kwargs) |
226 |
|
227 |
|
228 |
def project_name_append(project, column): |
229 |
if project.has_pending_modifications():
|
230 |
return mark_safe("<br /><i class='tiny'>%s</i>" % |
231 |
_('modifications pending'))
|
232 |
return u'' |
233 |
|
234 |
|
235 |
# Table classes
|
236 |
class UserProjectsTable(UserTable): |
237 |
caption = _('My projects')
|
238 |
|
239 |
name = LinkColumn('astakos.im.views.project_detail',
|
240 |
coerce=lambda x: truncatename(x, 25), |
241 |
append=project_name_append, |
242 |
args=(A('id'),),
|
243 |
accessor='application.name')
|
244 |
|
245 |
issue_date = tables.DateColumn(verbose_name=_('Application'),
|
246 |
format=DEFAULT_DATE_FORMAT, |
247 |
accessor='application.issue_date')
|
248 |
start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT, |
249 |
accessor='application.start_date')
|
250 |
end_date = tables.DateColumn(verbose_name=_('Expiration'),
|
251 |
format=DEFAULT_DATE_FORMAT, |
252 |
accessor='application.end_date')
|
253 |
members_count = tables.Column(verbose_name=_("Members"), default=0, |
254 |
orderable=False)
|
255 |
membership_status = tables.Column(verbose_name=_("Status"),
|
256 |
empty_values=(), |
257 |
orderable=False)
|
258 |
project_action = RichLinkColumn(verbose_name=_('Action'),
|
259 |
extra_context=action_extra_context, |
260 |
orderable=False)
|
261 |
|
262 |
def render_membership_status(self, record, *args, **kwargs): |
263 |
if self.user.owns_project(record) or self.user.is_project_admin(): |
264 |
return record.state_display()
|
265 |
else:
|
266 |
return self.user.membership_display(record) |
267 |
|
268 |
def render_members_count(self, record, *args, **kwargs): |
269 |
append = ""
|
270 |
project = record |
271 |
if project is None: |
272 |
append = mark_safe("<i class='tiny'>%s</i>" % (_('pending'),)) |
273 |
|
274 |
c = project.count_pending_memberships() |
275 |
if c > 0: |
276 |
pending_members_url = reverse( |
277 |
'project_pending_members',
|
278 |
kwargs={'chain_id': record.id})
|
279 |
|
280 |
pending_members = "<i class='tiny'> - %d %s</i>" % (
|
281 |
c, _('pending'))
|
282 |
if (
|
283 |
self.user.owns_project(record) or |
284 |
self.user.is_project_admin()
|
285 |
): |
286 |
pending_members = ("<i class='tiny'>" +
|
287 |
" - <a href='%s'>%d %s</a></i>" %
|
288 |
(pending_members_url, c, _('pending')))
|
289 |
append = mark_safe(pending_members) |
290 |
members_url = reverse('project_approved_members',
|
291 |
kwargs={'chain_id': record.id})
|
292 |
members_count = record.members_count() |
293 |
if self.user.owns_project(record) or self.user.is_project_admin(): |
294 |
members_count = '<a href="%s">%d</a>' % (members_url,
|
295 |
members_count) |
296 |
return mark_safe(str(members_count) + append) |
297 |
|
298 |
class Meta: |
299 |
sequence = ('name', 'membership_status', 'issue_date', 'end_date', |
300 |
'members_count', 'project_action') |
301 |
attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'} |
302 |
template = "im/table_render.html"
|
303 |
empty_text = _('No projects')
|
304 |
exclude = ('start_date', )
|
305 |
|
306 |
|
307 |
def member_action_extra_context(membership, table, col): |
308 |
|
309 |
context = [] |
310 |
urls, actions, prompts, confirms = [], [], [], [] |
311 |
|
312 |
if membership.project.is_deactivated():
|
313 |
return context
|
314 |
|
315 |
if membership.state == ProjectMembership.REQUESTED:
|
316 |
urls = ['astakos.im.views.project_reject_member',
|
317 |
'astakos.im.views.project_accept_member']
|
318 |
actions = [_('Reject'), _('Accept')] |
319 |
prompts = [_('Are you sure you want to reject this member?'),
|
320 |
_('Are you sure you want to accept this member?')]
|
321 |
confirms = [True, True] |
322 |
|
323 |
if membership.state in ProjectMembership.ACCEPTED_STATES: |
324 |
urls = ['astakos.im.views.project_remove_member']
|
325 |
actions = [_('Remove')]
|
326 |
prompts = [_('Are you sure you want to remove this member?')]
|
327 |
confirms = [True, True] |
328 |
|
329 |
for i, url in enumerate(urls): |
330 |
context.append(dict(url=reverse(url, args=(membership.pk,)),
|
331 |
action=actions[i], prompt=prompts[i], |
332 |
confirm=confirms[i])) |
333 |
return context
|
334 |
|
335 |
|
336 |
class ProjectMembersTable(UserTable): |
337 |
input = "<input type='checkbox' name='all-none'/>"
|
338 |
check = tables.Column(accessor="person.id", verbose_name=mark_safe(input), |
339 |
orderable=False)
|
340 |
email = tables.Column(accessor="person.email", verbose_name=_('Email')) |
341 |
status = tables.Column(accessor="state", verbose_name=_('Status')) |
342 |
project_action = RichLinkColumn(verbose_name=_('Action'),
|
343 |
extra_context=member_action_extra_context, |
344 |
orderable=False)
|
345 |
|
346 |
def __init__(self, project, *args, **kwargs): |
347 |
self.project = project
|
348 |
super(ProjectMembersTable, self).__init__(*args, **kwargs) |
349 |
if not self.user.owns_project(self.project): |
350 |
self.exclude = ('project_action', ) |
351 |
|
352 |
def render_check(self, value, record, *args, **kwargs): |
353 |
checkbox = ("<input type='checkbox' value='%d' name ='actions'>" %
|
354 |
record.id) |
355 |
return mark_safe(checkbox)
|
356 |
|
357 |
def render_status(self, value, record, *args, **kwargs): |
358 |
return record.state_display()
|
359 |
|
360 |
class Meta: |
361 |
template = "im/table_render.html"
|
362 |
attrs = {'id': 'members-table', 'class': 'members-table alt-style'} |
363 |
empty_text = _('No members')
|