Skip to content
Snippets Groups Projects
Commit 6891da6a authored by Todd Dembrey's avatar Todd Dembrey
Browse files

enable ordering on the table including for JSON structures

parent f9581d0c
No related branches found
No related tags found
No related merge requests found
......@@ -9,3 +9,4 @@ class DashboardTable(tables.Table):
class Meta:
model = ApplicationSubmission
fields = ('title', 'page', 'submit_time')
template = "dashboard/tables/table.html"
{% extends 'django_tables2/table.html' %}
{# example of how to extend the table template #}
{% block table.tbody.row %}
{{ block.super }}
{% endblock %}
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
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment