digitaleagenturnc/timemanagement/views.py

79 lines
2.3 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
@login_required
def TimeManagement(request):
context = {
"active_link" : "timemanagement"
}
return render(request, 'timemanagement/timemanagement_management.html', context)
@login_required
def TimeAjax(request):
data = {}
if request.method == "GET":
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
}
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,
}
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
}
else:
data = {
"success" : False
}
return JsonResponse(data)