digitaleagenturnc/timemanagement/views.py

136 lines
3.6 KiB
Python

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from .models import Workday, Breaks
from django.utils import timezone
import requests, csv, os
from django.templatetags.static import static
from django.conf import settings
from datetime import date
def loadingFreeDays(plz):
# Getting land
file_path = os.path.join(settings.STATIC_ROOT, 'users/extra/plz_short.csv')
land = False
with open(file_path, 'rt') as csvfile:
filecsv = csv.reader(csvfile, delimiter=';')
for row in filecsv:
if row[1] == plz:
land = row[6]
break;
if(land != False):
# CALCULATE FREEDAYS AS JSON
year = today = date.today().year
URL = "https://feiertage-api.de/api/"
PARAMS = {'jahr':year,'nur_land':land}
r = requests.get(url = URL, params = PARAMS)
return r.json()
else:
return False
@login_required
def AbsenceManagmenet(request):
context = {
"active_link" : "abscence",
"freedays" : loadingFreeDays(request.user.profile.agency.plz)
}
return render(request, 'timemanagement/tm_ab_management.html', context)
@login_required
def TimeManagement(request):
context = {
"active_link" : "timemanagement",
"workdays" : Workday.objects.filter(agency=request.user.profile.agency, user=request.user).order_by("-start")
}
return render(request, 'timemanagement/timemanagement_management.html', context)
@login_required
def TimeAjax(request):
data = {}
if request.method == "GET":
# START WORKDAY
if request.GET["action"] == "start_day":
wd = Workday(user=request.user, agency=request.user.profile.agency, start=timezone.now())
wd.save()
data = {
"success" : True,
"wd_starttime" : wd.start.strftime("%H:%M:%S"),
"wd_starttime_complete" : wd.start
}
# END DAY
elif request.GET["action"] == "end_day":
wd = list(Workday.objects.filter(user=request.user, agency=request.user.profile.agency, end=None))[0]
# END ALL BREAKS
for b in wd.breaks.all():
if b.end == None:
b.end = timezone.now()
b.save()
wd.end = timezone.now()
wd.save()
breaksum = 0
for b in wd.breaks.all():
if(b.end != None):
breaksum += (b.end - b.start).seconds
data = {
"success" : True,
"wd_endtime" : wd.end.strftime("%H:%M:%S"),
"actualbreaktime" : breaksum*1000
}
# START A BREAK
elif request.GET["action"] == "start_break":
wd = list(Workday.objects.filter(user=request.user, agency=request.user.profile.agency, end=None))[0]
newbreak = Breaks(workday=wd, user=request.user, agency=request.user.profile.agency, start=timezone.now())
newbreak.save()
wd.breaks.add(newbreak)
data = {
"success" : True,
"break_starttime" : newbreak.start,
}
# END BREAK
elif request.GET["action"] == "end_break":
wd = list(Workday.objects.filter(user=request.user, agency=request.user.profile.agency, end=None))[0]
toendbreak = list(wd.breaks.filter(end=None))[0]
toendbreak.end = timezone.now()
toendbreak.save()
wd = list(Workday.objects.filter(user=request.user, agency=request.user.profile.agency, end=None))[0]
breaksum = 0
for b in wd.breaks.all():
if(b.end != None):
breaksum += (b.end - b.start).seconds
data = {
"success" : True,
"actualbreaktime" : breaksum*1000
}
# REMOVE WORKDAY
elif request.GET["action"] == "remove_workday":
wd = Workday.objects.get(pk=request.GET.get("workday"))
if(wd.user == request.user and wd.agency == request.user.profile.agency):
wd.delete()
data = {
"success" : True
}
else:
data = { "success" : False}
else:
data = {
"success" : False
}
return JsonResponse(data)