Coverage for apis_core/history/views.py: 73%
26 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-10-30 12:03 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-10-30 12:03 +0000
1from django.forms import Form
2from django.views.generic.detail import BaseDetailView, DetailView
3from django.views.generic.edit import FormView
4from django_tables2 import SingleTableMixin
5from django_tables2.tables import table_factory
7from apis_core.generic.helpers import first_member_match, module_paths
8from apis_core.generic.views import (
9 GenericModelMixin,
10 GenericModelPermissionRequiredMixin,
11)
13from .tables import HistoryGenericTable
16class HistoryView(
17 GenericModelMixin, GenericModelPermissionRequiredMixin, SingleTableMixin, DetailView
18):
19 """
20 This view lists the historical data of one object.
21 The `GenericModel` this view acts upon is NOT the historical model, but the base model.
22 I.e. it is not the `VersionPerson`, but the `Person`. Therefore the permission required
23 to access this view refers to the `Person` model, NOT the `VersionPerson` model!
24 """
26 template_name = "history/history.html"
27 permission_action_required = "view"
29 def get_table_class(self):
30 table_modules = module_paths(self.model, path="tables", suffix="HistoryTable")
31 table_class = first_member_match(table_modules, HistoryGenericTable)
32 return table_factory(self.model, table_class)
34 def get_table_data(self):
35 return self.get_object().get_history_data()
38class HistoryReset(
39 GenericModelMixin, GenericModelPermissionRequiredMixin, BaseDetailView, FormView
40):
41 """
42 This view allows to reset a model instance to an earlier version.
43 The `GenericModel` this view acts upon IS the historical model, so the permission
44 required to access the view refers to the versioned model. For example for the
45 `Person` model, this view acts upon the `VersionPerson` model and therefore the
46 permission required is the `.versionperson_view` permission.
47 """
49 form_class = Form
50 template_name = "history/reset.html"
51 permission_action_required = "change"
53 def form_valid(self, form):
54 self.get_object().instance.save()
55 return super().form_valid(form)
57 def get_success_url(self):
58 return self.get_object().instance.get_history_url()