Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / tables.py @ 8a084e9f

History | View | Annotate | Download (13.5 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 (join_project_checks,
48
                                  leave_project_checks,
49
                                  cancel_membership_checks)
50

    
51
DEFAULT_DATE_FORMAT = "d/m/Y"
52

    
53

    
54
class LinkColumn(tables.LinkColumn):
55

    
56
    def __init__(self, *args, **kwargs):
57
        self.coerce = kwargs.pop('coerce', None)
58
        self.append = kwargs.pop('append', None)
59
        super(LinkColumn, self).__init__(*args, **kwargs)
60

    
61
    def render(self, value, record, bound_column):
62
        link = super(LinkColumn, self).render(value, record, bound_column)
63
        extra = ''
64
        if self.append:
65
            if callable(self.append):
66
                extra = self.append(record, bound_column)
67
            else:
68
                extra = self.append
69
        return mark_safe(link + extra)
70

    
71
    def render_link(self, uri, text, attrs=None):
72
        if self.coerce:
73
            text = self.coerce(text)
74
        return super(LinkColumn, self).render_link(uri, text, attrs)
75

    
76

    
77
# Helper columns
78
class RichLinkColumn(tables.TemplateColumn):
79

    
80
    method = 'POST'
81

    
82
    confirm_prompt = _('Yes')
83
    cancel_prompt = _('No')
84
    confirm = True
85

    
86
    prompt = _('Confirm action ?')
87

    
88
    action_tpl = None
89
    action = _('Action')
90
    extra_context = lambda record, table, column: {}
91

    
92
    url = None
93
    url_args = ()
94
    resolve_func = None
95

    
96
    def __init__(self, *args, **kwargs):
97
        kwargs['template_name'] = kwargs.get('template_name',
98
                                             'im/table_rich_link_column.html')
99
        for attr in ['method', 'confirm_prompt',
100
                     'cancel_prompt', 'prompt', 'url',
101
                     'url_args', 'action', 'confirm',
102
                     'resolve_func', 'extra_context']:
103
            setattr(self, attr, kwargs.pop(attr, getattr(self, attr)))
104

    
105
        super(RichLinkColumn, self).__init__(*args, **kwargs)
106

    
107
    def render(self, record, table, value, bound_column, **kwargs):
108
        # If the table is being rendered using `render_table`, it hackily
109
        # attaches the context to the table as a gift to `TemplateColumn`. If
110
        # the table is being rendered via `Table.as_html`, this won't exist.
111
        content = ''
112
        for extra_context in self.get_template_context(record, table, value,
113
                                                       bound_column, **kwargs):
114
            context = getattr(table, 'context', Context())
115
            context.update(extra_context)
116
            try:
117
                if self.template_code:
118
                    content += Template(self.template_code).render(context)
119
                else:
120
                    content += render_to_string(self.template_name, context)
121
            finally:
122
                context.pop()
123

    
124
        return mark_safe(content)
125

    
126
    def get_confirm(self, record, table):
127
        if callable(self.confirm):
128
            return self.confirm(record, table)
129
        return self.confirm
130

    
131
    def resolved_url(self, record, table):
132
        if callable(self.url):
133
            return self.url(record, table)
134

    
135
        if not self.url:
136
            return '#'
137

    
138
        args = list(self.url_args)
139
        for index, arg in enumerate(args):
140
            if isinstance(arg, A):
141
                args[index] = arg.resolve(record)
142
        return reverse(self.url, args=args)
143

    
144
    def get_action(self, record, table):
145
        if callable(self.action):
146
            return self.action(record, table)
147
        return self.action
148

    
149
    def get_prompt(self, record, table):
150
        if callable(self.prompt):
151
            return self.prompt(record, table)
152
        return self.prompt
153

    
154
    def get_template_context(self, record, table, value, bound_column, **kwargs):
155
        context = {'default': bound_column.default,
156
                   'record': record,
157
                   'value': value,
158
                   'col': self,
159
                   'url': self.resolved_url(record, table),
160
                   'prompt': self.get_prompt(record, table),
161
                   'action': self.get_action(record, table),
162
                   'confirm': self.get_confirm(record, table)
163
                  }
164

    
165
        # decide whether to return dict or a list of dicts in case we want to
166
        # display multiple actions within a cell.
167
        if self.extra_context:
168
            contexts = []
169
            extra_contexts = self.extra_context(record, table, self)
170
            if isinstance(extra_contexts, list):
171
                for extra_context in extra_contexts:
172
                    newcontext = dict(context)
173
                    newcontext.update(extra_context)
174
                    contexts.append(newcontext)
175
            else:
176
                context.update(extra_contexts)
177
                contexts = [context]
178
        else:
179
            contexts = [context]
180

    
181
        return contexts
182

    
183

    
184
def action_extra_context(application, table, self):
185
    user = table.user
186
    url, action, confirm, prompt = '', '', True, ''
187
    append_url = ''
188

    
189
    can_join = can_leave = can_cancel = False
190
    project = application.get_project()
191

    
192
    if project:
193
        try:
194
            join_project_checks(project)
195
            can_join = True
196
        except PermissionDenied, e:
197
            pass
198

    
199
        try:
200
            leave_project_checks(project)
201
            can_leave = True
202
        except PermissionDenied:
203
            pass
204

    
205
        try:
206
            cancel_membership_checks(project)
207
            can_cancel = True
208
        except PermissionDenied:
209
            pass
210

    
211
    membership = user.get_membership(project)
212
    if membership is not None:
213
        if can_leave and membership.can_leave():
214
            url = 'astakos.im.views.project_leave'
215
            action = _('Leave')
216
            confirm = True
217
            prompt = _('Are you sure you want to leave from the project?')
218
        elif can_cancel and membership.can_cancel():
219
            url = 'astakos.im.views.project_cancel'
220
            action = _('Cancel')
221
            confirm = True
222
            prompt = _('Are you sure you want to cancel the join request?')
223

    
224
    elif can_join:
225
        url = 'astakos.im.views.project_join'
226
        action = _('Join')
227
        confirm = True
228
        prompt = _('Are you sure you want to join this project?')
229
    else:
230
        action = ''
231
        confirm = False
232
        url = None
233

    
234
    url = reverse(url, args=(application.chain, )) + append_url if url else ''
235

    
236
    return {'action': action,
237
            'confirm': confirm,
238
            'url': url,
239
            'prompt': prompt}
240

    
241

    
242
class UserTable(tables.Table):
243

    
244
    def __init__(self, *args, **kwargs):
245
        self.user = None
246

    
247
        if 'request' in kwargs and kwargs.get('request').user:
248
            self.user = kwargs.get('request').user
249

    
250
        if 'user' in kwargs:
251
            self.user = kwargs.pop('user')
252

    
253
        super(UserTable, self).__init__(*args, **kwargs)
254

    
255
def project_name_append(application, column):
256
    if application.has_pending_modifications():
257
        return mark_safe("<br /><i class='tiny'>%s</i>" % \
258
                             _('modifications pending'))
259
    return u''
260

    
261
# Table classes
262
class UserProjectApplicationsTable(UserTable):
263
    caption = _('My projects')
264

    
265
    name = LinkColumn('astakos.im.views.project_detail',
266
                      coerce=lambda x: truncatename(x, 25),
267
                      append=project_name_append,
268
                      args=(A('chain'),))
269
    issue_date = tables.DateColumn(verbose_name=_('Application'), format=DEFAULT_DATE_FORMAT)
270
    start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
271
    end_date = tables.DateColumn(verbose_name=_('Expiration'), format=DEFAULT_DATE_FORMAT)
272
    members_count = tables.Column(verbose_name=_("Members"), default=0,
273
                                  orderable=False)
274
    membership_status = tables.Column(verbose_name=_("Status"), empty_values=(),
275
                                      orderable=False)
276
    project_action = RichLinkColumn(verbose_name=_('Action'),
277
                                    extra_context=action_extra_context,
278
                                    orderable=False)
279

    
280

    
281
    def render_membership_status(self, record, *args, **kwargs):
282
        if self.user.owns_application(record):
283
            return record.state_display()
284
        else:
285
            status = record.user_status(self.user)
286
            return record.user_status_display(self.user)
287

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

    
295
        c = project.count_pending_memberships()
296
        if c > 0:
297
            append = mark_safe("<i class='tiny'> - %d %s</i>"
298
                                % (c, _('pending')))
299

    
300
        return mark_safe(str(record.members_count()) + append)
301
        
302
    class Meta:
303
        model = ProjectApplication
304
        fields = ('name', 'membership_status', 'issue_date', 'end_date', 'members_count')
305
        attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
306
        template = "im/table_render.html"
307
        empty_text = _('No projects')
308
        exclude = ('start_date', )
309

    
310
class ProjectModificationApplicationsTable(UserProjectApplicationsTable):
311
    name = LinkColumn('astakos.im.views.project_detail',
312
                      verbose_name=_('Action'),
313
                      coerce= lambda x: 'review',
314
                      args=(A('pk'),))
315
    class Meta:
316
        attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
317
        fields = ('issue_date', 'membership_status')
318
        exclude = ('start_date', 'end_date', 'members_count', 'project_action')
319

    
320
def member_action_extra_context(membership, table, col):
321

    
322
    context = []
323
    urls, actions, prompts, confirms = [], [], [], []
324

    
325
    if membership.state == ProjectMembership.REQUESTED:
326
        urls = ['astakos.im.views.project_reject_member',
327
                'astakos.im.views.project_accept_member']
328
        actions = [_('Reject'), _('Accept')]
329
        prompts = [_('Are you sure you want to reject this member ?'),
330
                   _('Are you sure you want to accept this member ?')]
331
        confirms = [True, True]
332

    
333
    if membership.state == ProjectMembership.ACCEPTED:
334
        urls = ['astakos.im.views.project_remove_member']
335
        actions = [_('Remove')]
336
        if table.user == membership.person:
337
            actions = [_('Leave')]
338
        prompts = [_('Are you sure you want to remove this member ?')]
339
        confirms = [True, True]
340

    
341

    
342
    for i, url in enumerate(urls):
343
        context.append(dict(url=reverse(url, args=(table.project.pk,
344
                                                   membership.person.pk)),
345
                            action=actions[i], prompt=prompts[i],
346
                            confirm=confirms[i]))
347
    return context
348

    
349
class ProjectMembersTable(UserTable):
350
    name = tables.Column(accessor="person.last_name", verbose_name=_('Name'))
351
    status = tables.Column(accessor="state", verbose_name=_('Status'))
352
    project_action = RichLinkColumn(verbose_name=_('Action'),
353
                                    extra_context=member_action_extra_context,
354
                                    orderable=False)
355

    
356

    
357
    def __init__(self, project, *args, **kwargs):
358
        self.project = project
359
        super(ProjectMembersTable, self).__init__(*args, **kwargs)
360
        if not self.user.owns_project(self.project):
361
            self.exclude = ('project_action', )
362

    
363

    
364
    def render_name(self, value, record, *args, **kwargs):
365
        return record.person.realname
366

    
367
    def render_status(self, value, *args, **kwargs):
368
        return USER_STATUS_DISPLAY.get(value, 'Unknown')
369

    
370
    class Meta:
371
        template = "im/table_render.html"
372
        model = ProjectMembership
373
        fields = ('name', 'status')
374
        attrs = {'id': 'members-table', 'class': 'members-table alt-style'}
375
        empty_text = _('No members')
376