""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: michel.peltriaux@sgdnord.rlp.de Created on: 25.11.20 """ from django.template.loader import render_to_string from django.urls import reverse from django.utils.html import format_html from konova.utils.tables import BaseTable, ChoicesColumnForm import django_tables2 as tables from django.utils.translation import gettext_lazy as _ from process.enums import ProcessStateEnum from process.models import Process from process.settings import PROCESS_STATE_STRINGS class ProcessTable(BaseTable): id = tables.Column( verbose_name=_("Intervention identifier"), orderable=True, accessor="intervention.identifier", ) t = tables.Column( verbose_name=_("Title"), orderable=True, accessor="intervention.title", ) """ THESE COLUMNS MIGHT NOT BE OF INTEREST. TO REDUCE TABLE WIDTH THEY CAN BE REMOVED dila = tables.Column( verbose_name=_("Licensing authority document identifier"), orderable=True, accessor="licensing_authority_document_identifier", ) diro = tables.Column( verbose_name=_("Registration office document identifier"), orderable=True, accessor="registration_office_document_identifier", ) """ s = tables.Column( verbose_name=_("Status"), orderable=True, accessor="state", ) d = tables.Column( verbose_name=_("Created on"), orderable=True, accessor="created_on", ) ac = tables.Column( verbose_name=_("Actions"), orderable=False, empty_values=[], attrs={"td": {"class": "action-col"}} ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.title = _("Processes") self.add_new_url = reverse("process:new") def render_id(self, value, record: Process): """ Renders the id column for an intervention Args: value (str): The identifier value record (Process): The process record Returns: """ html = "" html += self.render_link( tooltip=_("Open {}").format(_("Process")), href=reverse("process:open", args=(record.id,)), txt=value, new_tab=False, ) return format_html(html) def render_s(self, value, record, column) -> str: """ Translates the record state to a desired language Args: value (str): The value of state inside record record (Process): The whole record itself Returns: str """ state = record.state is_owner = record.created_by == self.request.user choices = ProcessStateEnum.as_next_role_choices(self.request.user, state, is_owner) valid_choices = [choice[0] for choice in choices] if state in valid_choices: form = ChoicesColumnForm( action_url=reverse("process:edit-status", args=(record.id,)), choices=choices, initial={"select": state}, ) rendered = render_to_string("konova/choiceColumnForm.html", context={"form": form}, request=self.request) else: rendered = PROCESS_STATE_STRINGS.get(state) return rendered def render_ac(self, value, record): """ Renders possible actions for this record, such as delete. """ process = _("Process") html = "" html += self.render_open_btn( _("Open {}").format(process), reverse("process:open", args=(record.id,)), ) html += self.render_edit_btn( _("Edit {}").format(process), reverse("process:edit", args=(record.id,)), ) html += self.render_delete_btn( _("Delete {}").format(process), reverse("process:remove", args=(record.id,)), ) return format_html(html)