261 lines
6.6 KiB
Python
261 lines
6.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, FreeDays
|
|
from django.utils import timezone
|
|
import requests, csv, os
|
|
from django.templatetags.static import static
|
|
from django.conf import settings
|
|
from datetime import date
|
|
from django.contrib.auth.models import User
|
|
from calendar import monthrange
|
|
import datetime
|
|
import calendar
|
|
from .forms import AddAbsence
|
|
|
|
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
|
|
|
|
def get_datetime_range(year, month):
|
|
nb_days = monthrange(year, month)[1]
|
|
return [datetime.date(year, month, day) for day in range(1, nb_days+1)]
|
|
|
|
|
|
@login_required
|
|
def AbsenceManagmenet(request, activemonth=False, activeyear=False):
|
|
prevmonth = ""
|
|
nextmonth = ""
|
|
|
|
|
|
#MONTH
|
|
if(not activemonth or activemonth > 12 or activemonth < 1):
|
|
activemonth = int(activemonth)
|
|
#Active month
|
|
activemonth=date.today().month
|
|
|
|
if(activemonth == 1):
|
|
prevmonth = 12
|
|
else:
|
|
prevmonth = activemonth-1
|
|
|
|
if(activemonth == 12):
|
|
nextmonth = 1
|
|
else:
|
|
nextmonth = activemonth + 1
|
|
else:
|
|
|
|
if(activemonth == 1):
|
|
prevmonth = 12
|
|
else:
|
|
prevmonth = activemonth-1
|
|
|
|
if(activemonth == 12):
|
|
nextmonth = 1
|
|
else:
|
|
nextmonth = activemonth + 1
|
|
|
|
#YEAR
|
|
nextyear = date.today().year
|
|
prevyear = date.today().year
|
|
|
|
if(not activeyear):
|
|
activeyear = date.today().year
|
|
else:
|
|
if(nextmonth == 1):
|
|
nextyear = activeyear + 1
|
|
else:
|
|
nextyear = activeyear
|
|
if(prevmonth == 12):
|
|
prevyear = activeyear - 1
|
|
else:
|
|
prevyear = activeyear
|
|
|
|
context = {
|
|
"active_link" : "abscence",
|
|
"usersofagency" : User.objects.filter(profile__agency=request.user.profile.agency).order_by("-last_name"),
|
|
"days_this_month" : get_datetime_range(activeyear,activemonth),
|
|
"nextmonth" : nextmonth,
|
|
"prevmonth" : prevmonth,
|
|
"nextyear" : nextyear,
|
|
"prevyear" : prevyear,
|
|
"activemonth" : activemonth,
|
|
"activeyear" : activeyear,
|
|
"abscenceform" : AddAbsence()
|
|
}
|
|
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}
|
|
# REMOVE WORKDAY
|
|
elif request.GET["action"] == "testdjango":
|
|
prevmonth = ""
|
|
nextmonth = ""
|
|
activemonth = int(request.GET["activemonth"])
|
|
activeyear = int(request.GET["activeyear"])
|
|
|
|
#MONTH
|
|
if(not activemonth or activemonth > 12 or activemonth < 1):
|
|
activemonth = int(activemonth)
|
|
#Active month
|
|
activemonth=date.today().month
|
|
|
|
if(activemonth == 1):
|
|
prevmonth = 12
|
|
else:
|
|
prevmonth = activemonth-1
|
|
|
|
if(activemonth == 12):
|
|
nextmonth = 1
|
|
else:
|
|
nextmonth = activemonth + 1
|
|
else:
|
|
|
|
if(activemonth == 1):
|
|
prevmonth = 12
|
|
else:
|
|
prevmonth = activemonth-1
|
|
|
|
if(activemonth == 12):
|
|
nextmonth = 1
|
|
else:
|
|
nextmonth = activemonth + 1
|
|
|
|
#YEAR
|
|
nextyear = date.today().year
|
|
prevyear = date.today().year
|
|
|
|
if(not activeyear):
|
|
activeyear = date.today().year
|
|
else:
|
|
if(nextmonth == 1):
|
|
nextyear = activeyear + 1
|
|
else:
|
|
nextyear = activeyear
|
|
if(prevmonth == 12):
|
|
prevyear = activeyear - 1
|
|
else:
|
|
prevyear = activeyear
|
|
|
|
context = {
|
|
"active_link" : "abscence",
|
|
"usersofagency" : User.objects.filter(profile__agency=request.user.profile.agency).order_by("-last_name"),
|
|
"days_this_month" : get_datetime_range(activeyear,activemonth),
|
|
"activemonth" : activemonth,
|
|
"activeyear" : activeyear,
|
|
"nextmonth" : nextmonth,
|
|
"prevmonth" : prevmonth,
|
|
"nextyear" : nextyear,
|
|
"prevyear" : prevyear
|
|
}
|
|
|
|
|
|
return render(request, "timemanagement/rendered_table.html", context)
|
|
else:
|
|
data = {
|
|
"success" : False
|
|
}
|
|
|
|
return JsonResponse(data)
|
|
|