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): ...@@ -1100,6 +1100,13 @@ class ProjectApplicationManager(models.Manager):
return self.filter(Q(owner=user) | Q(applicant=user) | \ return self.filter(Q(owner=user) | Q(applicant=user) | \
Q(project__in=user.projectmembership_set.filter())) 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): class ProjectApplication(models.Model):
PENDING, APPROVED, REPLACED, UNKNOWN = 'Pending', 'Approved', 'Replaced', 'Unknown' PENDING, APPROVED, REPLACED, UNKNOWN = 'Pending', 'Approved', 'Replaced', 'Unknown'
applicant = models.ForeignKey( applicant = models.ForeignKey(
...@@ -1153,7 +1160,7 @@ class ProjectApplication(models.Model): ...@@ -1153,7 +1160,7 @@ class ProjectApplication(models.Model):
try: try:
membership = self.project.projectmembership_set.get(person=user) membership = self.project.projectmembership_set.get(person=user)
status = membership.state status = membership.state
except Project.DoesNotExist: except ProjectMembership.DoesNotExist:
status = -1 status = -1
return status return status
......
...@@ -3,6 +3,7 @@ import django_tables2 as tables ...@@ -3,6 +3,7 @@ import django_tables2 as tables
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django_tables2 import A from django_tables2 import A
from astakos.im.models import * from astakos.im.models import *
from django.utils.safestring import mark_safe
DEFAULT_DATE_FORMAT = "d/m/Y" DEFAULT_DATE_FORMAT = "d/m/Y"
...@@ -35,7 +36,8 @@ class UserProjectApplicationsTable(tables.Table): ...@@ -35,7 +36,8 @@ class UserProjectApplicationsTable(tables.Table):
issue_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT) issue_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT) start_date = tables.DateColumn(format=DEFAULT_DATE_FORMAT)
state = tables.Column(verbose_name="Status") 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) orderable=False)
......
...@@ -9,12 +9,14 @@ ...@@ -9,12 +9,14 @@
<div class="projects"> <div class="projects">
<h2>PROJECTS</h2> <h2>PROJECTS</h2>
{% if form %} {% if form %}
{% include "im/projects_list.html" %} {% include "im/projects/search_form.html" %}
{% else %} {% else %}
{% include "im/projects_index.html" %} {% include "im/projects/intro.html" %}
</div> </div>
{% endif %} {% endif %}
{% render_table table %} {% if table %}
{% render_table table %}
{% endif %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}
<p>Search for existing Projects and join the ones you like. Please search by Project name. </p> <p>Search for existing Projects and join the ones you like. Please search by Project name. </p>
<form action="{% url project_search %}" method="post" class="withlabels signup submit-inline">{% csrf_token %} <form action="{% url project_search %}" method="post" class="withlabels signup submit-inline">{% csrf_token %}
{% include "im/form_render.html" %} {% include "im/form_render.html" %}
<div class="form-row submit"> <div class="form-row submit">
<input type="submit" class="submit altcol" value="SEARCH" /> <input type="submit" class="submit altcol" value="SEARCH" />
{% if q %}<a href="{% url project_search %}">clear</a>{% endif %} {% if q %}<a href="{% url project_search %}">clear</a>{% endif %}
</div> </div>
</form> </form>
{% 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 ...@@ -41,6 +41,8 @@ from urllib import quote
from functools import wraps from functools import wraps
from datetime import datetime from datetime import datetime
from django_tables2 import RequestConfig
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
...@@ -1086,7 +1088,7 @@ def _update_object(request, model=None, object_id=None, slug=None, ...@@ -1086,7 +1088,7 @@ def _update_object(request, model=None, object_id=None, slug=None,
def project_add(request): def project_add(request):
result = callpoint.list_resources() result = callpoint.list_resources()
details_fields = ["name", "homepage", "description","start_date","end_date", "comments"] details_fields = ["name", "homepage", "description","start_date","end_date", "comments"]
membership_fields =["member_join_policy", "member_leave_policy", "limit_on_members_number"] membership_fields =["member_join_policy", "member_leave_policy", "limit_on_members_number"]
if not result.is_success: if not result.is_success:
messages.error( messages.error(
request, request,
...@@ -1097,7 +1099,7 @@ def project_add(request): ...@@ -1097,7 +1099,7 @@ def project_add(request):
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', return _create_object(request, template_name='im/projects/projectapplication_form.html',
extra_context=extra_context, post_save_redirect=reverse('project_list'), extra_context=extra_context, post_save_redirect=reverse('project_list'),
form_class=ProjectApplicationForm) form_class=ProjectApplicationForm)
@require_http_methods(["GET"]) @require_http_methods(["GET"])
...@@ -1106,7 +1108,7 @@ def project_add(request): ...@@ -1106,7 +1108,7 @@ def project_add(request):
def project_list(request): def project_list(request):
projects = ProjectApplication.objects.user_projects(request.user).select_related() 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")
table.paginate(page=request.GET.get('my_projectspage', 1), per_page=PAGINATE_BY) RequestConfig(request).configure(table)
return object_list( return object_list(
request, request,
...@@ -1124,7 +1126,7 @@ def project_list(request): ...@@ -1124,7 +1126,7 @@ def project_list(request):
def project_update(request, application_id): def project_update(request, application_id):
result = callpoint.list_resources() result = callpoint.list_resources()
details_fields = ["name", "homepage", "description","start_date","end_date", "comments"] details_fields = ["name", "homepage", "description","start_date","end_date", "comments"]
membership_fields =["member_join_policy", "member_leave_policy", "limit_on_members_number"] membership_fields =["member_join_policy", "member_leave_policy", "limit_on_members_number"]
if not result.is_success: if not result.is_success:
messages.error( messages.error(
request, request,
...@@ -1132,7 +1134,9 @@ def project_update(request, application_id): ...@@ -1132,7 +1134,9 @@ def project_update(request, application_id):
) )
else: else:
resource_catalog = result.data 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( return _update_object(
request, request,
object_id=application_id, object_id=application_id,
...@@ -1213,43 +1217,35 @@ def project_detail(request, application_id): ...@@ -1213,43 +1217,35 @@ def project_detail(request, application_id):
@login_required @login_required
def project_search(request): def project_search(request):
q = request.GET.get('q', '') q = request.GET.get('q', '')
queryset = ProjectApplication.objects.filter() form = ProjectSearchForm()
q = q.strip()
if request.method == 'GET': if request.method == "POST":
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) form = ProjectSearchForm(request.POST)
if form.is_valid(): if form.is_valid():
q = form.cleaned_data['q'].strip() q = form.cleaned_data['q'].strip()
queryset = queryset.filter(~Q(project__last_approval_date__isnull=True))
queryset = queryset.filter(name__contains=q)
else: else:
queryset = queryset.none() q = None
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)
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)
return object_list( return object_list(
request, request,
queryset, projects,
paginate_by=PAGINATE_BY_ALL,
page=request.GET.get('page') or 1,
template_name='im/projects/project_list.html', template_name='im/projects/project_list.html',
extra_context=dict( extra_context={
form=form, 'form': form,
is_search=True, 'is_search': True,
sorting=sorting, 'q': q,
q=q, 'table': table
) })
)
@require_http_methods(["POST"]) @require_http_methods(["POST"])
@signed_terms_required @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