diff --git a/opentech/apply/dashboard/tables.py b/opentech/apply/dashboard/tables.py index 0d568c685b916f50396a52a131d6a47bc059661c..4d3b529af3ecc1924ae3b1bc01a8f15ec40d64ba 100644 --- a/opentech/apply/dashboard/tables.py +++ b/opentech/apply/dashboard/tables.py @@ -9,3 +9,4 @@ class DashboardTable(tables.Table): class Meta: model = ApplicationSubmission fields = ('title', 'page', 'submit_time') + template = "dashboard/tables/table.html" diff --git a/opentech/apply/dashboard/templates/dashboard/tables/table.html b/opentech/apply/dashboard/templates/dashboard/tables/table.html new file mode 100644 index 0000000000000000000000000000000000000000..49e9c41a1f8093cebb452a68e321a11eb78d72aa --- /dev/null +++ b/opentech/apply/dashboard/templates/dashboard/tables/table.html @@ -0,0 +1,6 @@ +{% extends 'django_tables2/table.html' %} + +{# example of how to extend the table template #} +{% block table.tbody.row %} + {{ block.super }} +{% endblock %} diff --git a/opentech/apply/dashboard/views.py b/opentech/apply/dashboard/views.py index 8a14705f4c9beeb443b1e58e3fd384505632b750..b86e281c466be79136ff303496990da608fd46c3 100644 --- a/opentech/apply/dashboard/views.py +++ b/opentech/apply/dashboard/views.py @@ -1,4 +1,5 @@ from django.views.generic import ListView +from django_tables2 import RequestConfig from opentech.apply.funds.models import ApplicationSubmission @@ -12,4 +13,5 @@ class DashboardView(ListView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['object_list'] = DashboardTable(context['object_list']) + RequestConfig(self.request).configure(context['object_list']) return context diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py index 9c0b275009dcb2932e085afe214326b9f5ed420d..528c72e50aacb2f7be4d0b43759855d7b1c0036e 100644 --- a/opentech/apply/funds/models.py +++ b/opentech/apply/funds/models.py @@ -1,6 +1,8 @@ from datetime import date from django.core.exceptions import ValidationError +from django.db import models +from django.db.models.expressions import RawSQL, OrderBy from django.contrib.postgres.fields import JSONField from django.db import models from django.db.models import Q @@ -196,9 +198,28 @@ class Round(AbstractStreamForm): raise Http404() +class JSONOrderable(models.QuerySet): + def order_by(self, *field_names): + def build_json_order_by(field): + if field.replace('-', '') not in REQUIRED_BLOCK_NAMES: + return field + + if field[0] == '-': + descending = True + field = field[1:] + else: + descending = False + return OrderBy(RawSQL("LOWER(form_data->>%s)", (field,)), descending=descending) + + field_ordering = [build_json_order_by(field) for field in field_names] + return super().order_by(*field_ordering) + + class ApplicationSubmission(AbstractFormSubmission): form_data = JSONField() + objects = JSONOrderable.as_manager() + def get_data(self): # Updated for JSONField form_data = self.form_data