Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / tables.py @ 6795eb09

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

    
41
from django_tables2 import A
42
import django_tables2 as tables
43

    
44
from astakos.im.models import *
45
from astakos.im.templatetags.filters import truncatename
46

    
47
DEFAULT_DATE_FORMAT = "d/m/Y"
48

    
49

    
50
MEMBER_STATUS_DISPLAY = {
51
    100: _('Owner'),
52
      0: _('Requested'),
53
      1: _('Pending'),
54
      2: _('Accepted'),
55
      3: _('Removing'),
56
      4: _('Removed'),
57
     -1: _('Unregistered'),
58
}
59

    
60
class TruncatedLinkColumn(tables.LinkColumn):
61

    
62
    def __init__(self, *args, **kwargs):
63
        self.truncate_chars = kwargs.pop('truncate_chars', 10)
64
        super(TruncatedLinkColumn, self).__init__(*args, **kwargs)
65

    
66

    
67
    def render_link(self, uri, text, attrs=None):
68
        text = truncatename(text, self.truncate_chars)
69
        return super(TruncatedLinkColumn, self).render_link(uri, text, attrs)
70

    
71

    
72
# Helper columns
73
class RichLinkColumn(tables.TemplateColumn):
74

    
75
    method = 'POST'
76

    
77
    confirm_prompt = _('Yes')
78
    cancel_prompt = _('No')
79
    confirm = True
80

    
81
    prompt = _('Confirm action ?')
82

    
83
    action_tpl = None
84
    action = _('Action')
85
    extra_context = lambda record, table, column: {}
86

    
87
    url = None
88
    url_args = ()
89
    resolve_func = None
90

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

    
100
        super(RichLinkColumn, self).__init__(*args, **kwargs)
101

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

    
119
        return mark_safe(content)
120

    
121
    def get_confirm(self, record, table):
122
        if callable(self.confirm):
123
            return self.confirm(record, table)
124
        return self.confirm
125

    
126
    def resolved_url(self, record, table):
127
        if callable(self.url):
128
            return self.url(record, table)
129

    
130
        if not self.url:
131
            return '#'
132

    
133
        args = list(self.url_args)
134
        for index, arg in enumerate(args):
135
            if isinstance(arg, A):
136
                args[index] = arg.resolve(record)
137
        return reverse(self.url, args=args)
138

    
139
    def get_action(self, record, table):
140
        if callable(self.action):
141
            return self.action(record, table)
142
        return self.action
143

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

    
149
    def get_template_context(self, record, table, value, bound_column, **kwargs):
150
        context = {'default': bound_column.default,
151
                   'record': record,
152
                   'value': value,
153
                   'col': self,
154
                   'url': self.resolved_url(record, table),
155
                   'prompt': self.get_prompt(record, table),
156
                   'action': self.get_action(record, table),
157
                   'confirm': self.get_confirm(record, table)
158
                  }
159

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

    
176
        return contexts
177

    
178

    
179
def action_extra_context(project, table, self):
180
    user = table.user
181
    url, action, confirm, prompt = '', '', True, ''
182
    append_url = ''
183

    
184
    if user.owns_project(project):
185
        url = 'astakos.im.views.project_update'
186
        action = _('Update')
187
        confirm = False
188
        prompt = ''
189
    elif user.is_project_accepted_member(project):
190
        url = 'astakos.im.views.project_leave'
191
        action = _('- Leave')
192
        confirm = True
193
        prompt = _('Are you sure you want to leave from the project ?')
194
    elif not user.is_project_member(project):
195
        url = 'astakos.im.views.project_join'
196
        action = _('+ Join')
197
        confirm = True
198
        prompt = _('Are you sure you want to join this project ?')
199
    else:
200
        action = ''
201
        confirm = False
202
        url = None
203

    
204
    url = reverse(url, args=(project.pk, )) + append_url if url else ''
205
    return {'action': action,
206
            'confirm': confirm,
207
            'url': url,
208
            'prompt': prompt}
209

    
210

    
211
class UserTable(tables.Table):
212

    
213
    def __init__(self, *args, **kwargs):
214
        self.user = None
215

    
216
        if 'request' in kwargs and kwargs.get('request').user:
217
            self.user = kwargs.get('request').user
218

    
219
        if 'user' in kwargs:
220
            self.user = kwargs.pop('user')
221

    
222
        super(UserTable, self).__init__(*args, **kwargs)
223

    
224

    
225
# Table classes
226
class UserProjectApplicationsTable(UserTable):
227
    caption = _('My projects')
228

    
229
    name = TruncatedLinkColumn('astakos.im.views.project_detail',
230
                                                truncate_chars=25,
231
                                                args=(A('pk'),))
232
    issue_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
233
    start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
234
    end_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
235
    members_count = tables.Column(verbose_name=_("Enrolled"), default=0,
236
                                  sortable=False)
237
    membership_status = tables.Column(verbose_name=_("Status"), empty_values=(),
238
                                      sortable=False)
239
    project_action = RichLinkColumn(verbose_name=_('Action'),
240
                                    extra_context=action_extra_context,
241
                                    sortable=False)
242

    
243

    
244
    def render_membership_status(self, record, *args, **kwargs):
245
        status = record.user_status(self.user)
246
        if status == 100:
247
            return record.state
248
        else:
249
            return MEMBER_STATUS_DISPLAY.get(status, 'Unknown')
250

    
251
    class Meta:
252
        model = ProjectApplication
253
        fields = ('name', 'membership_status', 'issue_date', 'start_date','end_date', 'members_count')
254
        attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
255
        template = "im/table_render.html"
256
        empty_text = _('No projects')
257

    
258

    
259
def member_action_extra_context(membership, table, col):
260

    
261
    context = []
262
    urls, actions, prompts, confirms = [], [], [], []
263

    
264
    if membership.state == ProjectMembership.REQUESTED:
265
        urls = ['astakos.im.views.project_reject_member',
266
                'astakos.im.views.project_accept_member']
267
        actions = [_('Reject'), _('Approve')]
268
        prompts = [_('Are you sure you want to reject this member ?'),
269
                   _('Are you sure you want to approve this member ?')]
270
        confirms = [True, True]
271

    
272
    if membership.state == ProjectMembership.ACCEPTED:
273
        urls = ['astakos.im.views.project_remove_member']
274
        actions = [_('Remove')]
275
        prompts = [_('Are you sure you want to remove this member ?')]
276
        confirms = [True, True]
277

    
278

    
279
    for i, url in enumerate(urls):
280
        context.append(dict(url=reverse(url, args=(table.project.pk,
281
                                                   membership.person.pk)),
282
                            action=actions[i], prompt=prompts[i],
283
                            confirm=confirms[i]))
284
    return context
285

    
286
class ProjectApplicationMembersTable(UserTable):
287
    name = tables.Column(accessor="person.last_name", verbose_name=_('Name'))
288
    status = tables.Column(accessor="state", verbose_name=_('Status'))
289
    project_action = RichLinkColumn(verbose_name=_('Action'),
290
                                    extra_context=member_action_extra_context,
291
                                    sortable=False)
292

    
293

    
294
    def __init__(self, project, *args, **kwargs):
295
        self.project = project
296
        super(ProjectApplicationMembersTable, self).__init__(*args, **kwargs)
297
        if not self.user.owns_project(self.project):
298
            self.exclude = ('project_action', )
299

    
300

    
301
    def render_name(self, value, record, *args, **kwargs):
302
        return record.person.realname
303

    
304
    def render_status(self, value, *args, **kwargs):
305
        return MEMBER_STATUS_DISPLAY.get(value, 'Unknown')
306

    
307
    class Meta:
308
        template = "im/table_render.html"
309
        model = ProjectMembership
310
        fields = ('name', 'status')
311
        attrs = {'id': 'members-table', 'class': 'members-table alt-style'}
312
        empty_text = _('No members')
313