-
Notifications
You must be signed in to change notification settings - Fork 0
/
columns.py
84 lines (52 loc) · 2.36 KB
/
columns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from django.template.loader import render_to_string
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
import django_tables2 as tables
from .utils import get_language_field_name
class LockedColumn(tables.Column):
def render(self, value, record):
"""renders a toggle button for the locked state"""
template_name = "catalogs_table/columns/locked.html"
context = {
"locked": value,
"pk": str(record.pk),
}
return format_html(render_to_string(template_name, context))
class AvailableColumn(tables.Column):
_template_name = "catalogs_table/columns/available.html"
def render(self, value, record):
"""renders a toggle button for the available state"""
context = {
"available": value,
"pk": str(record.pk),
}
rendered = render_to_string(self._template_name, context)
return format_html(rendered)
class TitleColumn(tables.Column):
_template_name = "catalogs_table/columns/title.html"
def render(self, value, record):
"""renders the title as a link to the catalog detail view"""
context = {
"title": value,
"record": record,
}
return format_html(render_to_string(self._template_name, context))
def order(self, queryset, is_descending):
"""orders the title by the field of current language"""
title_lang_field = get_language_field_name("title")
queryset = queryset.order_by(("-" if is_descending else "") + title_lang_field)
return (queryset, True)
class SitesColumn(tables.Column):
_template_name = "catalogs_table/columns/sites.html"
def render(self, value, record):
"""render the sites column with a str join of values in the sites attribute"""
context = {"pk": str(record.pk)}
return format_html(render_to_string(self._template_name, context))
def order(self, queryset, is_descending):
"""order the sites column with sites_count"""
queryset = queryset.order_by(("-" if is_descending else "") + "sites_count")
return (queryset, True)
class OrderColumn(tables.Column):
_template_name = "catalogs_table/columns/order.html"
def render(self, value, record):
return format_html(render_to_string(self._template_name, {"order": value}))