Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / tables.py @ 157c2721

History | View | Annotate | Download (14.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

    
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.id,))
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('astakos.im.views.project_cancel_member',
196
                          args=(membership.id,))
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('astakos.im.views.project_join', args=(project.id,))
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
    pending_apps = column.table.pending_apps
229
    app = pending_apps.get(project.id)
230
    if app and app.id != project.application_id:
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('astakos.im.views.project_detail',
249
                      coerce=lambda x: truncatename(x, 25),
250
                      append=project_name_append,
251
                      args=(A('id'),),
252
                      orderable=False,
253
                      accessor='application.name')
254

    
255
    issue_date = tables.DateColumn(verbose_name=_('Application'),
256
                                   format=DEFAULT_DATE_FORMAT,
257
                                   orderable=False,
258
                                   accessor='application.issue_date')
259
    start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT,
260
                                   orderable=False,
261
                                   accessor='application.start_date')
262
    end_date = tables.DateColumn(verbose_name=_('Expiration'),
263
                                 format=DEFAULT_DATE_FORMAT,
264
                                 orderable=False,
265
                                 accessor='application.end_date')
266
    members_count_f = tables.Column(verbose_name=_("Members"),
267
                                    empty_values=(),
268
                                    orderable=False)
269
    owner = tables.Column(verbose_name=_("Owner"),
270
                                    accessor='application.owner')
271
    membership_status = tables.Column(verbose_name=_("Status"),
272
                                      empty_values=(),
273
                                      orderable=False)
274
    project_action = RichLinkColumn(verbose_name=_('Action'),
275
                                    extra_context=action_extra_context,
276
                                    orderable=False)
277

    
278
    def render_membership_status(self, record, *args, **kwargs):
279
        if self.user.owns_project(record) or self.user.is_project_admin():
280
            return record.state_display()
281
        else:
282
            m = self.memberships.get(record.id)
283
            if m:
284
                return m.user_friendly_state_display()
285
            return _('Not a member')
286

    
287
    def render_members_count_f(self, record, *args, **kwargs):
288
        append = ""
289
        project = record
290
        if project is None:
291
            append = mark_safe("<i class='tiny'>%s</i>" % (_('pending'),))
292

    
293
        c = len(self.requested.get(project.id, []))
294
        if c > 0:
295
            pending_members_url = reverse(
296
                'project_pending_members',
297
                kwargs={'chain_id': record.id})
298

    
299
            pending_members = "<i class='tiny'> - %d %s</i>" % (
300
                c, _('pending'))
301
            if (
302
                self.user.owns_project(record) or
303
                self.user.is_project_admin()
304
            ):
305
                pending_members = ("<i class='tiny'>" +
306
                                   " - <a href='%s'>%d %s</a></i>" %
307
                                   (pending_members_url, c, _('pending')))
308
            append = mark_safe(pending_members)
309
        members_url = reverse('project_approved_members',
310
                              kwargs={'chain_id': record.id})
311
        members_count = len(self.accepted.get(project.id, []))
312
        if self.user.owns_project(record) or self.user.is_project_admin():
313
            members_count = '<a href="%s">%d</a>' % (members_url,
314
                                                     members_count)
315
        return mark_safe(str(members_count) + append)
316

    
317
    class Meta:
318
        sequence = ('name', 'membership_status','owner', 'issue_date', 'end_date',
319
                    'members_count_f', 'project_action')
320
        attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
321
        template = "im/table_render.html"
322
        empty_text = _('No projects')
323
        exclude = ('start_date', )
324

    
325

    
326
def member_action_extra_context(membership, table, col):
327

    
328
    context = []
329
    urls, actions, prompts, confirms = [], [], [], []
330

    
331
    if membership.project.is_deactivated():
332
        return context
333

    
334
    if membership.state == ProjectMembership.REQUESTED:
335
        urls = ['astakos.im.views.project_reject_member',
336
                'astakos.im.views.project_accept_member']
337
        actions = [_('Reject'), _('Accept')]
338
        prompts = [_('Are you sure you want to reject this member?'),
339
                   _('Are you sure you want to accept this member?')]
340
        confirms = [True, True]
341

    
342
    if membership.state in ProjectMembership.ACCEPTED_STATES:
343
        urls = ['astakos.im.views.project_remove_member']
344
        actions = [_('Remove')]
345
        prompts = [_('Are you sure you want to remove this member?')]
346
        confirms = [True, True]
347

    
348
    for i, url in enumerate(urls):
349
        context.append(dict(url=reverse(url, args=(membership.pk,)),
350
                            action=actions[i], prompt=prompts[i],
351
                            confirm=confirms[i]))
352
    return context
353

    
354

    
355
class ProjectMembersTable(UserTable):
356
    input = "<input type='checkbox' name='all-none'/>"
357
    check = tables.Column(accessor="person.id", verbose_name=mark_safe(input),
358
                          orderable=False)
359
    email = tables.Column(accessor="person.email", verbose_name=_('Email'),
360
                          orderable= False)
361
    status = tables.Column(accessor="state", verbose_name=_('Status'),
362
                          orderable= False)
363
    project_action = RichLinkColumn(verbose_name=_('Action'),
364
                                    extra_context=member_action_extra_context,
365
                                    orderable=False)
366

    
367
    def __init__(self, project, *args, **kwargs):
368
        self.project = project
369
        super(ProjectMembersTable, self).__init__(*args, **kwargs)
370
        if not self.user.owns_project(self.project):
371
            self.exclude = ('project_action', )
372

    
373
    def render_check(self, value, record, *args, **kwargs):
374
        checkbox = ("<input type='checkbox' value='%d' name ='actions'>" %
375
                    record.id)
376
        return mark_safe(checkbox)
377

    
378
    def render_status(self, value, record, *args, **kwargs):
379
        return record.state_display()
380

    
381
    class Meta:
382
        template = "im/table_render.html"
383
        attrs = {'id': 'members-table', 'class': 'members-table alt-style'}
384
        empty_text = _('No members')