116 lines
3.1 KiB
Python
116 lines
3.1 KiB
Python
from django.views.generic import CreateView, ListView, UpdateView, DetailView, DeleteView, FormView, TemplateView
|
|
from django.contrib import messages
|
|
from django.shortcuts import render, redirect, reverse
|
|
from django.conf import settings
|
|
from django.http import HttpResponseRedirect,HttpResponse, JsonResponse
|
|
from .models import MainStatistic
|
|
from django.contrib.auth.models import User
|
|
from chat.models import ChatMessage
|
|
from users.models import Agency, AgencyBills
|
|
from standards.models import Standards
|
|
|
|
'''
|
|
Prüfung, ob angemeldeter User Mitarbeiterstatus hat. IMMER PER DISPATCH EINBAUEN!
|
|
'''
|
|
def checkForStuffUser(request):
|
|
if request.user.is_staff:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
'''
|
|
Hauptansicht Statisik
|
|
'''
|
|
class AdmMain(TemplateView):
|
|
template_name = "adm/adm_main.html"
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
if(checkForStuffUser(self.request)):
|
|
return super().dispatch(*args, **kwargs)
|
|
else:
|
|
messages.warning(self.request, f'Sie benötigen einen Mitarbeiter-Account, um diese Seiten aufzurufen!')
|
|
return redirect("login")
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({'active_link' : "adm-statistic"})
|
|
|
|
context.update({'statistik' : MainStatistic.objects.all().order_by('staticdate')[:180] })
|
|
|
|
return context
|
|
|
|
'''
|
|
Gesamtansicht der Agenturen
|
|
'''
|
|
class AdmAgencys(TemplateView):
|
|
template_name = "adm/adm_agencys.html"
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
if(checkForStuffUser(self.request)):
|
|
return super().dispatch(*args, **kwargs)
|
|
else:
|
|
messages.warning(self.request, f'Sie benötigen einen Mitarbeiter-Account, um diese Seiten aufzurufen!')
|
|
return redirect("login")
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({'active_link' : "adm-agencys"})
|
|
|
|
context.update({'agencys' : Agency.objects.all()})
|
|
|
|
return context
|
|
|
|
'''
|
|
Einzelansicht der Agenturen
|
|
'''
|
|
class AdmAgencySingle(TemplateView):
|
|
template_name = "adm/adm_agency_single.html"
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
if(checkForStuffUser(self.request)):
|
|
return super().dispatch(*args, **kwargs)
|
|
else:
|
|
messages.warning(self.request, f'Sie benötigen einen Mitarbeiter-Account, um diese Seiten aufzurufen!')
|
|
return redirect("login")
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context.update({'active_link' : "adm-agencys"})
|
|
context.update({'agency' : Agency.objects.get(pk=kwargs['agpk'])})
|
|
context.update({'bills' : AgencyBills.objects.filter(agency=Agency.objects.get(pk=kwargs['agpk'])).order_by('-billdate')[:3]})
|
|
|
|
return context
|
|
|
|
|
|
# CRONJOB, um die Statistik zu füllen!
|
|
def statisticCronJob(request, code):
|
|
data = {}
|
|
if(code == settings.CRONAPIKEY_STATSTIC):
|
|
print("STATISTIC is running...")
|
|
newMainS = MainStatistic(agencys=len(Agency.objects.all()),users=len(User.objects.all().exclude(is_staff=True, is_superuser=True)),standards=len(Standards.objects.all()),chatmessages=len(ChatMessage.objects.all()))
|
|
newMainS.save()
|
|
data.update({"status" : "success"})
|
|
else:
|
|
print("API STATISTIC CODE FAILED")
|
|
data.update({"status" : "failed"})
|
|
return JsonResponse(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|