Commit c54b3b65 authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

Projects list/search refactoring

parent 7ad374bd
......@@ -1100,6 +1100,13 @@ class ProjectApplicationManager(models.Manager):
return self.filter(Q(owner=user) | Q(applicant=user) | \
Q(project__in=user.projectmembership_set.filter()))
def search_by_name(self, *search_strings):
q = Q()
for s in search_strings:
q = q | Q(name__icontains=s)
return self.filter(q)
class ProjectApplication(models.Model):
PENDING, APPROVED, REPLACED, UNKNOWN = 'Pending', 'Approved', 'Replaced', 'Unknown'
applicant = models.ForeignKey(
......@@ -1153,7 +1160,7 @@ class ProjectApplication(models.Model):
try:
membership = self.project.projectmembership_set.get(person=user)
status = membership.state
except Project.DoesNotExist:
except ProjectMembership.DoesNotExist:
status = -1
return status
......
......@@ -3,6 +3,7 @@ import django_tables2 as tables
from django.utils.translation import ugettext as _
from django_tables2 import A
from astakos.im.models import *
from django.utils.safestring import mark_safe
DEFAULT_DATE_FORMAT = "d/m/Y"
......@@ -35,7 +36,8 @@ class UserProjectApplicationsTable(tables.Table):
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=(),
orderable=False)
......
......@@ -9,12 +9,14 @@
<div class="projects">
<h2>PROJECTS</h2>
{% if form %}
{% include "im/projects_list.html" %}
{% include "im/projects/search_form.html" %}
{% else %}
{% include "im/projects_index.html" %}
{% include "im/projects/intro.html" %}
</div>
{% endif %}
{% if table %}
{% render_table table %}
{% endif %}
</div>
</div>
{% endblock %}
{% extends "im/account_base.html" %}
{% load astakos_tags %}
{% load filters %}
{% load django_tables2 %}
{% block page.body %}
<div class="maincol {% block innerpage.class %}{% endblock %}">
<div class="projects">
<h2>PROJECTS</h2>
{% if form %}
{% include "im/projects_list.html" %}
{% else %}
{% include "im/projects_index.html" %}
</div>
{% endif %}
{% render_table table %}
</div>
</div>
{% endblock %}
......@@ -41,6 +41,8 @@ from urllib import quote
from functools import wraps
from datetime import datetime
from django_tables2 import RequestConfig
from django.shortcuts import get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
......@@ -1106,7 +1108,7 @@ def project_add(request):
def project_list(request):
projects = ProjectApplication.objects.user_projects(request.user).select_related()
table = tables.UserProjectApplicationsTable(projects, user=request.user, prefix="my_projects")
table.paginate(page=request.GET.get('my_projectspage', 1), per_page=PAGINATE_BY)
RequestConfig(request).configure(table)
return object_list(
request,
......@@ -1132,7 +1134,9 @@ def project_update(request, application_id):
)
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 _update_object(
request,
object_id=application_id,
......@@ -1213,43 +1217,35 @@ def project_detail(request, application_id):
@login_required
def project_search(request):
q = request.GET.get('q', '')
queryset = ProjectApplication.objects.filter()
if request.method == 'GET':
form = ProjectSearchForm()
q = q.strip()
queryset = queryset.filter(~Q(project__last_approval_date__isnull=True))
queryset = queryset.filter(name__contains=q)
else:
form = ProjectSearchForm(request.POST)
if request.method == "POST":
form = ProjectSearchForm(request.POST)
if form.is_valid():
q = form.cleaned_data['q'].strip()
queryset = queryset.filter(~Q(project__last_approval_date__isnull=True))
queryset = queryset.filter(name__contains=q)
else:
queryset = queryset.none()
q = None
sorting = 'name'
# validate sorting
sort_form = ProjectSortForm(request.GET)
if sort_form.is_valid():
sorting = sort_form.cleaned_data.get('sorting')
queryset = queryset.order_by(sorting)
if q is None:
projects = ProjectApplication.objects.none()
else:
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")
RequestConfig(request).configure(table)
return object_list(
request,
queryset,
paginate_by=PAGINATE_BY_ALL,
page=request.GET.get('page') or 1,
projects,
template_name='im/projects/project_list.html',
extra_context=dict(
form=form,
is_search=True,
sorting=sorting,
q=q,
)
)
extra_context={
'form': form,
'is_search': True,
'q': q,
'table': table
})
@require_http_methods(["POST"])
@signed_terms_required
......
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