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
Loading
...@@ -9,3 +9,4 @@ class DashboardTable(tables.Table): ...@@ -9,3 +9,4 @@ class DashboardTable(tables.Table):
class Meta: class Meta:
model = ApplicationSubmission model = ApplicationSubmission
fields = ('title', 'page', 'submit_time') 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.views.generic import ListView
from django_tables2 import RequestConfig
from opentech.apply.funds.models import ApplicationSubmission from opentech.apply.funds.models import ApplicationSubmission
...@@ -12,4 +13,5 @@ class DashboardView(ListView): ...@@ -12,4 +13,5 @@ class DashboardView(ListView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['object_list'] = DashboardTable(context['object_list']) context['object_list'] = DashboardTable(context['object_list'])
RequestConfig(self.request).configure(context['object_list'])
return context return context
from datetime import date from datetime import date
from django.core.exceptions import ValidationError 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.contrib.postgres.fields import JSONField
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
...@@ -196,9 +198,28 @@ class Round(AbstractStreamForm): ...@@ -196,9 +198,28 @@ class Round(AbstractStreamForm):
raise Http404() 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): class ApplicationSubmission(AbstractFormSubmission):
form_data = JSONField() form_data = JSONField()
objects = JSONOrderable.as_manager()
def get_data(self): def get_data(self):
# Updated for JSONField # Updated for JSONField
form_data = self.form_data 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