File: views.py

package info (click to toggle)
django-tables 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,752 kB
  • sloc: python: 7,120; makefile: 132; sh: 74
file content (192 lines) | stat: -rw-r--r-- 6,213 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from random import choice

from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils.lorem_ipsum import words
from django.views.generic.base import TemplateView
from django_filters.views import FilterView

from django_tables2 import MultiTableMixin, RequestConfig, SingleTableMixin, SingleTableView
from django_tables2.export.views import ExportMixin

from .data import COUNTRIES
from .filters import PersonFilter
from .models import Country, Person
from .tables import (
    Bootstrap4Table,
    Bootstrap5Table,
    BootstrapTable,
    BootstrapTablePinnedRows,
    CheckboxTable,
    CountryTable,
    PersonTable,
    SemanticTable,
    ThemedCountryTable,
)


def create_fake_data():
    # create some fake data to make sure we need to paginate
    if Country.objects.all().count() < 50:
        for country in COUNTRIES.splitlines():
            name, population = country.split(";")
            Country.objects.create(name=name, visits=0, population=int(population))

    if Person.objects.all().count() < 500:
        countries = list(Country.objects.all()) + [None]
        Person.objects.bulk_create(
            [Person(name=words(3, common=False), country=choice(countries)) for i in range(50)]
        )


def index(request):
    create_fake_data()
    table = PersonTable(Person.objects.all())
    RequestConfig(request, paginate={"per_page": 5}).configure(table)

    return render(
        request,
        "index.html",
        {
            "table": table,
            "urls": (
                (reverse("tutorial"), "Tutorial"),
                (reverse("multiple"), "Multiple tables"),
                (reverse("filtertableview"), "Filtered tables (with export)"),
                (reverse("singletableview"), "Using SingleTableMixin"),
                (reverse("multitableview"), "Using MultiTableMixin"),
                (
                    reverse("template_example", args=["bootstrap3"]),
                    "template: Bootstrap 3 (bootstrap.html)",
                ),
                (
                    reverse("template_example", args=["bootstrap4"]),
                    "template: Bootstrap 4 (bootstrap4.html)",
                ),
                (
                    reverse("template_example", args=["bootstrap5"]),
                    "template: Bootstrap 5 (bootstrap5.html)",
                ),
                (
                    reverse("template_example", args=["semantic"]),
                    "template: Semantic UI (semantic.html)",
                ),
            ),
        },
    )


def multiple(request):
    qs = Country.objects.all()

    example1 = CountryTable(qs, prefix="1-")
    RequestConfig(request, paginate=False).configure(example1)

    example2 = CountryTable(qs, prefix="2-")
    RequestConfig(request, paginate={"per_page": 2}).configure(example2)

    example3 = ThemedCountryTable(qs, prefix="3-")
    RequestConfig(request, paginate={"per_page": 3}).configure(example3)

    example4 = ThemedCountryTable(qs, prefix="4-")
    RequestConfig(request, paginate={"per_page": 3}).configure(example4)

    example5 = ThemedCountryTable(qs, prefix="5-")
    example5.template = "extended_table.html"
    RequestConfig(request, paginate={"per_page": 3}).configure(example5)

    return render(
        request,
        "multiple.html",
        {
            "example1": example1,
            "example2": example2,
            "example3": example3,
            "example4": example4,
            "example5": example5,
        },
    )


def checkbox(request):
    create_fake_data()
    table = CheckboxTable(Country.objects.all(), order_by="name")
    RequestConfig(request, paginate={"per_page": 15}).configure(table)

    return render(request, "checkbox_example.html", {"table": table})


def template_example(request, version):
    """Demonstrate the use of the bootstrap template"""

    versions = {
        "bootstrap3": (BootstrapTable, "bootstrap_template.html"),
        "bootstrap4": (Bootstrap4Table, "bootstrap4_template.html"),
        "bootstrap5": (Bootstrap5Table, "bootstrap5_template.html"),
        "semantic": (SemanticTable, "semantic_template.html"),
    }
    table_class, template_name = versions[version]

    create_fake_data()
    table = table_class(Person.objects.all().select_related("country"), order_by="-name")
    RequestConfig(request, paginate={"per_page": 10}).configure(table)

    return render(request, template_name, {"table": table})


class ClassBased(SingleTableView):
    table_class = ThemedCountryTable
    queryset = Country.objects.all()
    template_name = "class_based.html"


class MultipleTables(MultiTableMixin, TemplateView):
    template_name = "multiTable.html"

    table_pagination = {"per_page": 10}

    def get_tables(self):
        qs = Person.objects.all()
        return [
            PersonTable(qs),
            PersonTable(qs, exclude=("country",)),
            BootstrapTablePinnedRows(qs),
        ]


def tutorial(request):
    table = PersonTable(
        Person.objects.all(), attrs={"class": "paleblue"}, template_name="django_tables2/table.html"
    )
    RequestConfig(request, paginate={"per_page": 10}).configure(table)
    return render(request, "tutorial.html", {"table": table})


class FilteredPersonListView(ExportMixin, SingleTableMixin, FilterView):
    table_class = PersonTable
    model = Person
    template_name = "bootstrap_template.html"

    filterset_class = PersonFilter

    export_formats = ("csv", "xls")

    def get_queryset(self):
        return super().get_queryset().select_related("country")

    def get_table_kwargs(self):
        return {"template_name": "django_tables2/bootstrap.html"}


def country_detail(request, pk):
    country = get_object_or_404(Country, pk=pk)

    # hide the country column, as it is not very interesting for a list of persons for a country.
    table = PersonTable(country.person_set.all(), extra_columns=(("country", None),))
    return render(request, "country_detail.html", {"country": country, "table": table})


def person_detail(request, pk):
    person = get_object_or_404(Person, pk=pk)

    return render(request, "person_detail.html", {"person": person})