Initial
This commit is contained in:
22
kspneo/utils/generators.py
Normal file
22
kspneo/utils/generators.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 09.11.20
|
||||
|
||||
"""
|
||||
import random
|
||||
import string
|
||||
|
||||
|
||||
def generate_random_string(length: int, only_numbers: bool = False) -> str:
|
||||
"""
|
||||
Generates a random string of variable length
|
||||
"""
|
||||
if only_numbers:
|
||||
elements = string.digits
|
||||
else:
|
||||
elements = string.ascii_letters
|
||||
|
||||
ret_val = "".join(random.choice(elements) for i in range(length))
|
||||
return ret_val
|
||||
52
kspneo/utils/mailer.py
Normal file
52
kspneo/utils/mailer.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 09.11.20
|
||||
|
||||
"""
|
||||
import logging
|
||||
|
||||
from django.core.mail import send_mail
|
||||
|
||||
from konova.sub_settings.django_settings import DEFAULT_FROM_EMAIL
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Mailer:
|
||||
"""
|
||||
A wrapper for the django internal mailing functionality
|
||||
"""
|
||||
from_mail = None
|
||||
to_mail = []
|
||||
fail_silently = False
|
||||
|
||||
# Optional. Can be changed using the constructor to authenticate on the smtp server using other credentials
|
||||
auth_user = None
|
||||
auth_password = None
|
||||
|
||||
def __init__(self, to_mail: list, from_mail: str = DEFAULT_FROM_EMAIL, auth_user: str = None, auth_password: str = None, fail_silently: bool = False):
|
||||
# Make sure given to_mail parameter is a list
|
||||
if isinstance(to_mail, str):
|
||||
to_mail = [to_mail]
|
||||
|
||||
self.from_mail = from_mail
|
||||
self.to_mail = to_mail
|
||||
self.fail_silently = fail_silently
|
||||
self.auth_user = auth_user
|
||||
self.auth_password = auth_password
|
||||
|
||||
def send(self, subject: str, msg: str):
|
||||
"""
|
||||
Sends a mail with subject and message
|
||||
"""
|
||||
return send_mail(
|
||||
subject=subject,
|
||||
message=msg,
|
||||
from_email=self.from_mail,
|
||||
recipient_list=self.to_mail,
|
||||
fail_silently=self.fail_silently,
|
||||
auth_user=self.auth_user,
|
||||
auth_password=self.auth_password
|
||||
)
|
||||
45
kspneo/utils/session.py
Normal file
45
kspneo/utils/session.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 09.12.20
|
||||
|
||||
"""
|
||||
from django.http import HttpRequest
|
||||
from idna import unicode
|
||||
|
||||
from konova.models import RoleGroup
|
||||
from organisation.settings import ROLE_TYPE_STRINGS
|
||||
|
||||
CURRENT_ROLE_ID = "current_role"
|
||||
|
||||
|
||||
def set_session_user_role(request: HttpRequest, role_group: RoleGroup) -> dict:
|
||||
""" Set the user session to an active role
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The user request
|
||||
role_group (RoleGroup): The selected role group
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
current_role = {}
|
||||
if role_group is not None:
|
||||
current_role["type"] = unicode(ROLE_TYPE_STRINGS.get(role_group.role.type))
|
||||
current_role["org"] = role_group.organisation.__str__()
|
||||
current_role["id"] = role_group.id
|
||||
request.session[CURRENT_ROLE_ID] = current_role
|
||||
return current_role
|
||||
|
||||
|
||||
def get_session_user_role(request: HttpRequest) -> dict:
|
||||
""" Returns the current role chosen by a user for this session
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The used request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return request.session.get(CURRENT_ROLE_ID, {})
|
||||
121
kspneo/utils/tables.py
Normal file
121
kspneo/utils/tables.py
Normal file
@@ -0,0 +1,121 @@
|
||||
"""
|
||||
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
|
||||
Reference in New Issue
Block a user