Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / tables.py @ 61edd5cd

History | View | Annotate | Download (14.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
                                  can_leave_request,
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 and project.is_approved():
193
        try:
194
            join_project_checks(project)
195
            can_join = True
196
        except PermissionDenied, e:
197
            pass
198

    
199
        try:
200
            can_leave = can_leave_request(project, user)
201
        except PermissionDenied:
202
            pass
203

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

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

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

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

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

    
240

    
241
class UserTable(tables.Table):
242

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

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

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

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

    
254
def project_name_append(application, column):
255
    if application.has_pending_modifications():
256
        return mark_safe("<br /><i class='tiny'>%s</i>" % \
257
                             _('modifications pending'))
258
    return u''
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'), format=DEFAULT_DATE_FORMAT)
269
    start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
270
    end_date = tables.DateColumn(verbose_name=_('Expiration'), format=DEFAULT_DATE_FORMAT)
271
    members_count = tables.Column(verbose_name=_("Members"), default=0,
272
                                  orderable=False)
273
    membership_status = tables.Column(verbose_name=_("Status"), empty_values=(),
274
                                      orderable=False)
275
    project_action = RichLinkColumn(verbose_name=_('Action'),
276
                                    extra_context=action_extra_context,
277
                                    orderable=False)
278

    
279

    
280
    def render_membership_status(self, record, *args, **kwargs):
281
        if self.user.owns_application(record) or self.user.is_project_admin():
282
            return record.project_state_display()
283
        else:
284
            try:
285
                project = record.project
286
                return self.user.membership_display(project)
287
            except Project.DoesNotExist:
288
                return _("Unknown")
289

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

    
297
        c = project.count_pending_memberships()
298
        if c > 0:
299
            pending_members_url = reverse('project_pending_members', 
300
                kwargs={'chain_id': application.chain})
301

    
302
            pending_members = "<i class='tiny'> - %d %s</i>" % (c, _('pending'))
303
            if self.user.owns_application(record) or self.user.is_project_admin():
304
                pending_members = "<i class='tiny'>"+" - <a href='%s'>%d %s</a></i>" % (
305
                    pending_members_url,c, _('pending'))
306
            append = mark_safe(pending_members)
307
        members_url = reverse('project_members', 
308
            kwargs={'chain_id': application.chain})
309
        members_count = record.members_count()
310
        if self.user.owns_application(record) or self.user.is_project_admin():
311
            members_count = '<a href="%s">%d</a>' % (members_url,
312
                members_count)
313
        return mark_safe(str(members_count) + append)
314
        
315
    class Meta:
316
        model = ProjectApplication
317
        fields = ('name', 'membership_status', 'issue_date', 'end_date', 
318
                  'members_count')
319
        attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
320
        template = "im/table_render.html"
321
        empty_text = _('No projects')
322
        exclude = ('start_date', )
323

    
324
class ProjectModificationApplicationsTable(UserProjectApplicationsTable):
325
    name = LinkColumn('astakos.im.views.project_detail',
326
                      verbose_name=_('Action'),
327
                      coerce= lambda x: 'review',
328
                      args=(A('pk'),))
329
    class Meta:
330
        attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
331
        fields = ('issue_date', 'membership_status')
332
        exclude = ('start_date', 'end_date', 'members_count', 'project_action')
333

    
334
def member_action_extra_context(membership, table, col):
335

    
336
    context = []
337
    urls, actions, prompts, confirms = [], [], [], []
338

    
339
    if membership.project.is_deactivated():
340
        return context
341

    
342
    if membership.state == ProjectMembership.REQUESTED:
343
        urls = ['astakos.im.views.project_reject_member',
344
                'astakos.im.views.project_accept_member']
345
        actions = [_('Reject'), _('Accept')]
346
        prompts = [_('Are you sure you want to reject this member?'),
347
                   _('Are you sure you want to accept this member?')]
348
        confirms = [True, True]
349

    
350
    if membership.state in ProjectMembership.ACTUALLY_ACCEPTED:
351
        urls = ['astakos.im.views.project_remove_member']
352
        actions = [_('Remove')]
353
        prompts = [_('Are you sure you want to remove this member?')]
354
        confirms = [True, True]
355

    
356

    
357
    for i, url in enumerate(urls):
358
        context.append(dict(url=reverse(url, args=(table.project.pk,
359
                                                   membership.person.pk)),
360
                            action=actions[i], prompt=prompts[i],
361
                            confirm=confirms[i]))
362
    return context
363

    
364
class ProjectMembersTable(UserTable):
365
    input = "<input type='checkbox' name='all-none'/>"
366
    check = tables.Column(accessor="person.id",verbose_name =mark_safe(input), orderable=False)
367
    email = tables.Column(accessor="person.email", verbose_name=_('Email'))    
368
    status = tables.Column(accessor="state", verbose_name=_('Status'))
369
    project_action = RichLinkColumn(verbose_name=_('Action'),
370
                                    extra_context=member_action_extra_context,
371
                                    orderable=False)
372

    
373

    
374
    def __init__(self, project, *args, **kwargs):
375
        self.project = project
376
        super(ProjectMembersTable, self).__init__(*args, **kwargs)
377
        if not self.user.owns_project(self.project):
378
            self.exclude = ('project_action', )
379

    
380
    def render_check(self, value, record, *args, **kwargs):
381
        checkbox = "<input type='checkbox' value='%d' name ='actions'>" % record.id
382
        return  mark_safe(checkbox)
383

    
384
    def render_status(self, value, record, *args, **kwargs):
385
        return record.state_display()
386

    
387
    class Meta:
388
        template = "im/table_render.html"
389
        attrs = {'id': 'members-table', 'class': 'members-table alt-style'}
390
        empty_text = _('No members')