122 lines
4.0 KiB
Python
122 lines
4.0 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
Created on: 25.11.20
|
|
|
|
"""
|
|
import uuid
|
|
|
|
from django import forms
|
|
from django.core.paginator import PageNotAnInteger, EmptyPage
|
|
from django.http import HttpRequest
|
|
from django.utils.html import format_html
|
|
import django_tables2 as tables
|
|
|
|
from konova.forms import BaseForm
|
|
from konova.settings import PAGE_SIZE_DEFAULT, PAGE_PARAM, RESULTS_PER_PAGE_PARAM, PAGE_SIZE_OPTIONS
|
|
|
|
|
|
class BaseTable(tables.tables.Table):
|
|
results_per_page_choices = PAGE_SIZE_OPTIONS
|
|
results_per_page_chosen = None
|
|
results_per_page_parameter = RESULTS_PER_PAGE_PARAM
|
|
add_new_entries = True
|
|
add_new_url = None
|
|
title = None
|
|
|
|
def __init__(self, request: HttpRequest = None, filter_set=None, queryset=None, *args, **kwargs):
|
|
self.user = request.user or None
|
|
if filter_set is not None:
|
|
queryset = filter_set.qs
|
|
kwargs["data"] = queryset
|
|
kwargs["request"] = request
|
|
super().__init__(*args, **kwargs)
|
|
self.results_per_page_chosen = int(request.GET.get(RESULTS_PER_PAGE_PARAM, PAGE_SIZE_DEFAULT))
|
|
try:
|
|
self.paginate(
|
|
page=request.GET.get(PAGE_PARAM, 1),
|
|
per_page=self.results_per_page_chosen,
|
|
)
|
|
except (PageNotAnInteger, EmptyPage) as e:
|
|
self.paginate(
|
|
page=1,
|
|
per_page=self.results_per_page_chosen,
|
|
)
|
|
|
|
def render_link(self, tooltip: str, href: str, txt: str, new_tab: bool = False):
|
|
"""
|
|
Returns an <a> html element using given parameters
|
|
"""
|
|
new_tab = "_blank" if new_tab else "_self"
|
|
return format_html(
|
|
"<a href={} target='{}' title='{}'>{}</a>",
|
|
href,
|
|
new_tab,
|
|
tooltip,
|
|
txt,
|
|
)
|
|
|
|
def render_delete_btn(self, tooltip: str = None, href: str = None):
|
|
"""
|
|
Returns a remover icon with <a> support as html element using given parameters
|
|
"""
|
|
return format_html(
|
|
"<a href={} title='{}'><button class='button small'><em class='fas fa-trash-alt'></em></button></a>",
|
|
href,
|
|
tooltip,
|
|
)
|
|
|
|
def render_edit_btn(self, tooltip: str = None, href: str = None):
|
|
"""
|
|
Returns a remover icon with <a> support as html element using given parameters
|
|
"""
|
|
return format_html(
|
|
"<a href={} title='{}'><button class='button small'><em class='fas fa-edit'></em></button></a>",
|
|
href,
|
|
tooltip,
|
|
)
|
|
|
|
def render_open_btn(self, tooltip: str = None, href: str = None, new_tab: bool = False):
|
|
"""
|
|
Returns a remover icon with <a> support as html element using given parameters
|
|
"""
|
|
return format_html(
|
|
"<a href={} title='{}' target='{}'><button class='button small'><em class='fas fa-sign-in-alt'></em></button></a>",
|
|
href,
|
|
tooltip,
|
|
"_blank" if new_tab else ""
|
|
)
|
|
|
|
def render_boolean(self, tooltip: str = None, val: bool = False):
|
|
"""
|
|
Returns a remover icon with <a> support as html element using given parameters
|
|
"""
|
|
icon = "fas fa-check-circle true" if val else "fas fa-times-circle false"
|
|
return format_html(
|
|
"<em title='{}' class='{}'></em>",
|
|
tooltip,
|
|
icon
|
|
)
|
|
|
|
|
|
class ChoicesColumnForm(BaseForm):
|
|
select = forms.ChoiceField(
|
|
choices=[],
|
|
label="",
|
|
label_suffix="",
|
|
widget=forms.Select(
|
|
attrs={
|
|
"onchange": "submit();",
|
|
}
|
|
)
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.action_url = kwargs.pop("action_url", None)
|
|
self.choices = kwargs.pop("choices", [])
|
|
super().__init__(*args, **kwargs)
|
|
self.auto_id += "_" + str(uuid.uuid4())
|
|
if len(self.choices) > 0:
|
|
self.fields["select"].choices = self.choices
|