initial
This commit is contained in:
44
lib/utils.py
Normal file
44
lib/utils.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import time
|
||||
import logging
|
||||
import functools
|
||||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||
from django_middleware_global_request import get_request
|
||||
from django_proxmox_mikrotik import settings
|
||||
|
||||
|
||||
def measure_time(func):
|
||||
"""Decorator, der die Ausführungszeit einer Funktion misst und mit logging.debug ausgibt."""
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
start_time = time.time()
|
||||
result = func(*args, **kwargs)
|
||||
end_time = time.time()
|
||||
execution_time = end_time - start_time
|
||||
logging.debug(f"Funktion '{func.__name__}' wurde in {execution_time:.4f} Sekunden ausgeführt")
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def paginator(queryset, page: int = 1, per_page: int = 18, request=None):
|
||||
request = request or get_request()
|
||||
per_page = request.GET.get('per_page',
|
||||
request.POST.get('per_page', getattr(settings, 'PAGINATOR_PER_PAGE', per_page)))
|
||||
paginator = Paginator(queryset, per_page)
|
||||
page = request.GET.get('page', request.POST.get('page', page))
|
||||
try:
|
||||
return paginator.page(page)
|
||||
except PageNotAnInteger:
|
||||
return paginator.page(1)
|
||||
except EmptyPage:
|
||||
return paginator.page(paginator.num_pages)
|
||||
|
||||
|
||||
class PaginatedModel:
|
||||
def __init__(self, model):
|
||||
self.model = model
|
||||
|
||||
def paginate(self, domainfilter, page: int = 1, per_page: int = 18, request=None):
|
||||
queryset = self.model.objects.filter(domainfilter)
|
||||
return paginator(queryset, page, per_page, request)
|
||||
Reference in New Issue
Block a user