Commit 8a535a2f authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

Project members table refactoring

- Use django-tables2 to render table
parent c54b3b65
......@@ -31,16 +31,16 @@ class UserProjectApplicationsTable(tables.Table):
super(UserProjectApplicationsTable, self).__init__(*args, **kwargs)
name = tables.LinkColumn('astakos.im.views.project_detail', args=(A('pk'),))
issue_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
state = tables.Column(verbose_name="Status")
members_count = tables.Column(verbose_name="Enrolled", default=0,
members_count = tables.Column(verbose_name=_("Enrolled"), default=0,
sortable=False)
membership_status = tables.Column(verbose_name="My status", empty_values=(),
membership_status = tables.Column(verbose_name=_("My status"), empty_values=(),
orderable=False)
def render_membership_status(self, *args, **kwargs):
return MEMBER_STATUS_DISPLAY.get(kwargs.get('record').member_status(self.user))
......@@ -48,4 +48,25 @@ class UserProjectApplicationsTable(tables.Table):
model = ProjectApplication
fields = ('name', 'membership_status', 'issue_date', 'start_date',
'state', 'members_count')
attrs = {'id': 'projects-list', 'class': 'my-projects alt-style'}
caption = _('My projects')
template = "im/table_render.html"
class ProjectApplicationMembersTable(tables.Table):
name = tables.Column(accessor="person.last_name", verbose_name=_('Name'))
status = tables.Column(accessor="state", verbose_name=_('Status'))
def render_name(self, value, record, *args, **kwargs):
return record.person.last_name
def render_status(self, value, *args, **kwargs):
return MEMBER_STATUS_DISPLAY.get(value, 'Unknown')
class Meta:
template = "im/table_render.html"
model = ProjectMembership
fields = ('name', 'status')
attrs = {'id': 'members-table', 'class': 'members-table alt-style'}
{% extends "im/account_base.html" %}
{% load filters %}
{% load astakos_tags filters django_tables2 %}
{% block page.body %}
{% with object.project.approved_members as approved_members %}
......@@ -127,70 +127,10 @@
</div>
{% if object.project.is_alive %}
<div class="full-dotted">
{% with page|concat:sorting as args %}
{% with object.project.projectmembership_set.select_related.all|paginate:args as membership %}
{% if membership %}
<table class="alt-style" id="members-table">
<caption>MEMBERS:</caption>
<thead>
<tr>
{%if user.is_superuser or user == object.owner %}<th>User Email</th>{% endif %}
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for m in membership.object_list %}
<tr>
{%if user.is_superuser or user == object.owner %}<td>{{m.person.email}}</td>{% endif %}
<td>{{m.person.realname}}</td>
{% if m.person == object.owner %}
<td>Owner</td>
{% else %}
{% if m.acceptance_date %}
<td>Approved {% if m.leave_request_date%}(User has requested to leave the project on:{{m.leave_request_date}}){% endif %}
{% if user == object.owner and user != m.person %}
<a href="{% url project_remove_member object.id m.person.id %}?{% if page %}page={{ page }}{% endif %}{% if sorting %}&sorting={{sorting}}{% endif %}">Remove</a>
{% endif %}
</td>
{% else %}
<td>Pending
{% if user == object.owner %}
<a href="{% url project_accept_member object.id m.person.id %}?{% if page %}page={{ page }}{% endif %}{% if sorting %}&sorting={{sorting}}{% endif %}">Accept</a>
<a href="{% url project_reject_member object.id m.person.id %}?{% if page %}page={{ page }}{% endif %}{% if sorting %}&sorting={{sorting}}{% endif %}">Reject</a>
{% endif %}
</td>
{% endif %}
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
<div class="pagination">
<p class="next-prev">
{% if membership.has_previous %}
<a href="?page={{ membership.previous_page_number }}{% if sorting %}&sorting={{sorting}}{% endif %}">< previous</a>
{% else %}
<a href="javascript:void()" class="disabled">< previous</a>
{% endif %}
{% if membership.has_next %}
<a href="?page={{ membership.next_page_number }}{% if sorting %}&sorting={{sorting}}{% endif %}">next ></a>
{% else %}
<a href="javascript:void()" class="disabled">next ></a>
{% endif %}
</p>
<p class="nums">
<span class="current">
Page {{ membership.number }} of {{ membership.paginator.num_pages }}
</span>
</p>
</div>
{% else %}
<p>No members yet!</p>
{% endif %}
{% endwith %}
{% endwith %}
<h3>MEMBERS</h3>
{% if members_table %}
{% render_table members_table %}
{% endif %}
</div>
......
{% extends "im/account_base.html" %}
{% load astakos_tags %}
{% load filters %}
{% load django_tables2 %}
{% load astakos_tags filters django_tables2 %}
{% block page.body %}
<div class="maincol {% block innerpage.class %}{% endblock %}">
......
{% spaceless %}
{% load django_tables2 %}
{% load i18n %}
{% if table.page %}
<div class="table-container">
{% endif %}
{% block table %}
<table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
{% if table.attrs.caption %}
<caption>{{ table.attrs.caption }}</caption>
{% endif %}
{% nospaceless %}
{% block table.thead %}
<thead>
<tr>
{% for column in table.columns %}
{% if column.orderable %}
<th {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}#{{ table.attrs.id }}">{{ column.header }}</a></th>
{% else %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
{% endblock table.thead %}
{% block table.tbody %}
<tbody>
{% for row in table.page.object_list|default:table.rows %} {# support pagination #}
{% block table.tbody.row %}
<tr class="{% cycle "odd" "even" %}">
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>{{ cell }}</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
<tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
{% endblock table.tbody %}
{% block table.tfoot %}
<tfoot></tfoot>
{% endblock table.tfoot %}
{% endnospaceless %}
</table>
{% endblock table %}
{% if table.page %}
{% with table.page.paginator.count as total %}
{% with table.page.object_list|length as count %}
{% block pagination %}
<ul class="pagination">
{% if table.page.has_previous %}
{% nospaceless %}{% block pagination.previous %}<li class="previous"><a href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}#{{ table.attrs.id }}">{% trans "Previous" %}</a></li>{% endblock pagination.previous %}{% endnospaceless %}
{% endif %}
{% if table.page.has_previous or table.page.has_next %}
{% nospaceless %}{% block pagination.current %}<li class="current">{% blocktrans with table.page.number as current and table.paginator.num_pages as total %}Page {{ current }} of {{ total }}{% endblocktrans %}</li>{% endblock pagination.current %}{% endnospaceless %}
{% endif %}
{% if table.page.has_next %}
{% nospaceless %}{% block pagination.next %}<li class="next"><a href="{% querystring table.prefixed_page_field=table.page.next_page_number %}#{{ table.attrs.id }}">{% trans "Next" %}</a></li>{% endblock pagination.next %}{% endnospaceless %}
{% endif %}
{% nospaceless %}{% block pagination.cardinality %}<li class="cardinality">{% if total != count %}{% blocktrans %}{{ count }} of {{ total }}{% endblocktrans %}{% else %}{{ total }}{% endif %} {% if total == 1 %}{{ table.data.verbose_name }}{% else %}{{ table.data.verbose_name_plural }}{% endif %}</li>{% endblock pagination.cardinality %}{% endnospaceless %}
</ul>
{% endblock pagination %}
{% endwith %}
{% endwith %}
</div>
{% endif %}
{% endspaceless %}
......@@ -1096,7 +1096,9 @@ def project_add(request):
)
else:
resource_catalog = result.data
extra_context = {'resource_catalog':resource_catalog, 'show_form':True, 'details_fields':details_fields, 'membership_fields':membership_fields}
extra_context = {'resource_catalog':resource_catalog, 'show_form':True,
'details_fields':details_fields,
'membership_fields':membership_fields}
return _create_object(request, template_name='im/projects/projectapplication_form.html',
extra_context=extra_context, post_save_redirect=reverse('project_list'),
form_class=ProjectApplicationForm)
......@@ -1107,7 +1109,8 @@ def project_add(request):
@login_required
def project_list(request):
projects = ProjectApplication.objects.user_projects(request.user).select_related()
table = tables.UserProjectApplicationsTable(projects, user=request.user, prefix="my_projects")
table = tables.UserProjectApplicationsTable(projects, user=request.user,
prefix="my_projects_")
RequestConfig(request).configure(table)
return object_list(
......@@ -1184,13 +1187,14 @@ def project_detail(request, application_id):
transaction.commit()
addmembers_form = AddProjectMembersForm()
# validate sorting
sorting = 'person__email'
form = ProjectMembersSortForm(request.GET or request.POST)
if form.is_valid():
sorting = form.cleaned_data.get('sorting')
rollback = False
application = get_object_or_404(ProjectApplication, pk=application_id)
members = application.project.projectmembership_set.select_related()
members_table = tables.ProjectApplicationMembersTable(members,
prefix="members_")
RequestConfig(request).configure(members_table)
try:
return object_detail(
request,
......@@ -1199,11 +1203,10 @@ def project_detail(request, application_id):
template_name='im/projects/project_detail.html',
extra_context={
'resource_catalog':resource_catalog,
'sorting':sorting,
'addmembers_form':addmembers_form
}
)
except:
'addmembers_form':addmembers_form,
'members_table': members_table
})
except Exception, e:
rollback = True
finally:
if rollback == True:
......@@ -1233,7 +1236,8 @@ def project_search(request):
projects = ProjectApplication.objects.search_by_name(q)
projects = projects.filter(~Q(project__last_approval_date__isnull=True))
table = tables.UserProjectApplicationsTable(projects, user=request.user, prefix="my_projects")
table = tables.UserProjectApplicationsTable(projects, user=request.user,
prefix="my_projects_")
RequestConfig(request).configure(table)
return object_list(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment