Revision 3f0d6293 snf-astakos-app/astakos/im/tables.py

b/snf-astakos-app/astakos/im/tables.py
88 88
        # If the table is being rendered using `render_table`, it hackily
89 89
        # attaches the context to the table as a gift to `TemplateColumn`. If
90 90
        # the table is being rendered via `Table.as_html`, this won't exist.
91
        context = getattr(table, 'context', Context())
92
        context.update(self.get_template_context(record, table, value,
93
                                                 bound_column, **kwargs))
94
        try:
95
            if self.template_code:
96
                return Template(self.template_code).render(context)
97
            else:
98
                return render_to_string(self.template_name, context)
99
        finally:
100
            context.pop()
91
        content = ''
92
        for extra_context in self.get_template_context(record, table, value,
93
                                                       bound_column, **kwargs):
94
            context = getattr(table, 'context', Context())
95
            context.update(extra_context)
96
            try:
97
                if self.template_code:
98
                    content += Template(self.template_code).render(context)
99
                else:
100
                    content += render_to_string(self.template_name, context)
101
            finally:
102
                context.pop()
103

  
104
        return mark_safe(content)
101 105

  
102 106
    def get_confirm(self, record, table):
103 107
        if callable(self.confirm):
......
139 143
                  }
140 144

  
141 145
        if self.extra_context:
142
            context.update(self.extra_context(record, table, self))
146
            contexts = []
147
            extra_contexts = self.extra_context(record, table, self)
148
            if isinstance(extra_contexts, list):
149
                for extra_context in extra_contexts:
150
                    newcontext = dict(context)
151
                    newcontext.update(extra_context)
152
                    contexts.append(newcontext)
153
            else:
154
                context.update(extra_contexts)
155
        else:
156
            contexts = [context]
143 157

  
144
        return context
158
        return contexts
145 159

  
146 160

  
147 161
def action_extra_context(project, table, self):
......
165 179
        confirm = True
166 180
        prompt = _('Are you sure you want to join this project ?')
167 181
    else:
168
        action = _('Pending')
182
        action = ''
183
        confirm = False
184
        url = None
169 185

  
170 186
    url = reverse(url, args=(project.pk, )) + append_url if url else ''
171 187
    return {'action': action,
......
174 190
            'prompt': prompt}
175 191

  
176 192

  
177
# Table classes
178
class UserProjectApplicationsTable(tables.Table):
179
    caption = _('My projects')
193
class UserTable(tables.Table):
180 194

  
181 195
    def __init__(self, *args, **kwargs):
182 196
        self.user = None
......
187 201
        if 'user' in kwargs:
188 202
            self.user = kwargs.pop('user')
189 203

  
190
        super(UserProjectApplicationsTable, self).__init__(*args, **kwargs)
204
        super(UserTable, self).__init__(*args, **kwargs)
205

  
206

  
207
# Table classes
208
class UserProjectApplicationsTable(UserTable):
209
    caption = _('My projects')
191 210

  
192 211
    name = tables.LinkColumn('astakos.im.views.project_detail', args=(A('pk'),))
193 212
    issue_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
......
214 233
        fields = ('name', 'membership_status', 'issue_date', 'start_date','end_date', 'members_count')
215 234
        attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
216 235
        template = "im/table_render.html"
236
        empty_text = _('No projects')
237

  
238

  
239
def member_action_extra_context(membership, table, col):
240
    urls, actions, prompts, confirms = [], [], [], []
241

  
242
    if membership.state == ProjectMembership.REQUESTED:
243
        urls = ['astakos.im.views.project_reject_member',
244
                'astakos.im.views.project_accept_member']
245
        actions = [_('Reject'), _('Approve')]
246
        prompts = [_('Are you sure you want to reject this member ?'),
247
                   _('Are you sure you want to approve this member ?')]
248
        confirms = [True, True]
249

  
250
    if membership.state == ProjectMembership.ACCEPTED:
251
        urls = ['astakos.im.views.project_remove_member']
252
        actions = [_('Remove')]
253
        prompts = [_('Are you sure you want to remove this member ?')]
254
        confirms = [True, True]
217 255

  
218 256

  
219
class ProjectApplicationMembersTable(tables.Table):
257
class ProjectApplicationMembersTable(UserTable):
220 258
    name = tables.Column(accessor="person.last_name", verbose_name=_('Name'))
221 259
    status = tables.Column(accessor="state", verbose_name=_('Status'))
260
    project_action = RichLinkColumn(verbose_name=_('Action'),
261
                                    extra_context=member_action_extra_context,
262
                                    sortable=False)
263

  
264

  
265
    def __init__(self, project, *args, **kwargs):
266
        self.project = project
267
        super(ProjectApplicationMembersTable, self).__init__(*args, **kwargs)
268
        if not self.user.owns_project(self.project):
269
            self.exclude = ('project_action', )
222 270

  
223 271

  
224 272
    def render_name(self, value, record, *args, **kwargs):
......
232 280
        model = ProjectMembership
233 281
        fields = ('name', 'status')
234 282
        attrs = {'id': 'members-table', 'class': 'members-table alt-style'}
283
        empty_text = _('No members')
235 284

  

Also available in: Unified diff