def add_work_time_view(request):
    if request.method == 'POST':
        if request.user.is_authenticated:
            worker = Worker.objects.filter(user=request.user).first()
            if worker:
                worked_today = WorkTimestamp.objects.filter(
                    user=request.user,
                    working_after=True,
                    timestamp__date=timezone.localdate()).exists()
                if not worker.working and worked_today:
                    time = request.POST.get('work_additional_time').split(':')
                    minutes = int(time[0]) * 60 + int(time[1])
                    funcs.add_work_time(worker, minutes)
                    if request.user.is_staff or request.user.is_superuser:
                        return redirect(router_routes['cs_admin'])
                    return redirect('back:auth:logout')
                return AuthError.session_expired(request)
            raise PermissionDenied
        raise PermissionDenied
    return HttpResponseBadRequest
def finish_work_view(request):
    if request.method == 'POST':
        if request.user.is_authenticated:
            worker = Worker.objects.filter(user=request.user).first()
            if worker:
                if worker.working:
                    worker.working = False
                    timestamp = WorkTimestamp.objects.create(
                        user=request.user,
                        working_after=False,
                        timestamp=parse_datetime(
                            request.POST.get('work_finish')))
                    timestamp.save()
                    worker.save()
                    if request.user.is_staff or request.user.is_superuser:
                        return redirect(router_routes['cs_admin'])
                    return redirect('back:auth:logout')
                return AuthError.session_expired(request)
            raise PermissionDenied
        raise PermissionDenied
    return HttpResponseBadRequest
def start_work_view(request):
    if request.method == 'POST':
        if request.user.is_authenticated:
            worker = Worker.objects.filter(user=request.user).first()
            if worker:
                worked_today = WorkTimestamp.objects.filter(
                    user=request.user,
                    working_after=True,
                    timestamp__date=timezone.localdate()).exists()
                if not (worker.working or worked_today):
                    worker.working = True
                    timestamp = WorkTimestamp.objects.create(
                        user=request.user,
                        working_after=True,
                        location=request.POST.get('location'))
                    timestamp.save()
                    worker.save()
                    if request.user.is_staff or request.user.is_superuser:
                        return redirect(router_routes['cs_admin'])
                    return redirect('back:auth:logout')
                return AuthError.session_expired(request)
            raise PermissionDenied
        raise PermissionDenied
    return HttpResponseBadRequest