Skip to content

Commit

Permalink
project listing filter "posted in or after YEAR" (posted since...)
Browse files Browse the repository at this point in the history
not super-useful as long as can't change sorting, and default is to
sort by posting date anyway; but, a start!

resolves #36
  • Loading branch information
jesteria committed Sep 3, 2020
1 parent 5712268 commit 57b63a3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/marketplace/domain/proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def list_public_projects(projname=None,
orgname=None,
skills=None,
social_cause=None,
posted_since=None,
project_status=None):
# We could also add the projects that are non-public but that also belong
# to the organizations that the user is member of. Should that be added
Expand All @@ -60,6 +61,12 @@ def list_public_projects(projname=None,
if orgname:
projects = projects.filter(organization__name__icontains=orgname)

if isinstance(posted_since, int):
projects = projects.filter(creation_date__year__gte=posted_since)
elif posted_since:
# must be datetime-like value
projects = projects.filter(creation_date__gte=posted_since)

if skills:
for skill in re.split(r'[,\s]+', skills):
projects = projects.filter(projecttask__projecttaskrequirement__skill__name__icontains=skill)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@



<div class="form-check">
<label class="form-check-label" for="{{ field_name }}{{ field_value }}">
<input class="form-check-input"
type="checkbox"
type="{{ field_type|default:'checkbox' }}"
id="{{ field_name }}{{ field_value }}"
name="{{ field_name }}"
value="{{ field_value }}"
Expand Down
8 changes: 8 additions & 0 deletions src/marketplace/templates/marketplace/proj_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ <h4>Filter results</h4>
{% include 'marketplace/components/filter_checkbox.html' with field_name='projectstatus' field_value='completed' field_text='Completed' is_checked=checked_project_fields.completed %}
</fieldset>

<fieldset class="mt-3" name="since">
<legend>Posted in or after</legend>
{# if we make form auto-submit on change, then these should perhaps be anchors. however, as currently implemented, radios perhaps make most sense. #}
{% for project_year, is_checked in project_years %}
{% include 'marketplace/components/filter_checkbox.html' with field_name='postedsince' field_value=project_year field_text=project_year field_type='radio' is_checked=is_checked %}
{% endfor %}
</fieldset>

<button type="submit"
form="projlist"
formmethod="get"
Expand Down
33 changes: 30 additions & 3 deletions src/marketplace/views/proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.contrib.messages.views import SuccessMessageMixin
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.db.models import Max, Min
from django.db.models.functions import ExtractYear
from django.forms import CharField, ModelForm, Textarea
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import redirect, render
Expand Down Expand Up @@ -135,15 +137,36 @@ def get_project_role(request, proj_pk, role_pk):

@require_GET
def project_list_view(request):
# year of posting filter gets slight special handling
(latest_year, oldest_year) = (
Project.objects
.annotate(
creation_year=ExtractYear('creation_date'),
).aggregate(
Max('creation_year'),
Min('creation_year'),
).values()
)
project_years = range(latest_year, (oldest_year - 1), -1)

try:
filter_year = int(request.GET.get('postedsince', ''))
except ValueError:
filter_year = None
selected_year = filter_year if filter_year in project_years else oldest_year

search_config = {'posted_since': selected_year}

# the rest are handled in bulk
search_values = (
request.GET.get(key.replace('_', ''))
for key in PROJECT_SEARCH_KEYS
)
search_config = {
key: value
search_config.update(
(key, value)
for (key, value) in zip(PROJECT_SEARCH_KEYS, search_values)
if value
}
)

filter_projname = request.GET.get('projname', '')
filter_orgname = request.GET.get('orgname', '')
Expand Down Expand Up @@ -186,6 +209,10 @@ def project_list_view(request):
'user_is_any_organization_member': any_org_member,
'single_org_membership': single_org_membership,
'organization_memberships': organization_memberships,
'project_years': [
(project_year, project_year == selected_year)
for project_year in project_years
],
})


Expand Down

0 comments on commit 57b63a3

Please sign in to comment.