118 lines
4.4 KiB
Python
118 lines
4.4 KiB
Python
from django.shortcuts import render
|
|
from django.contrib.auth.decorators import login_required
|
|
import json
|
|
from hr_module.models import Employee, Plan, TimeLog
|
|
from django.contrib.auth.models import User
|
|
from django.http import HttpResponse, JsonResponse, Http404
|
|
import datetime
|
|
from django.urls import reverse, reverse_lazy
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
|
|
|
|
login_url = reverse_lazy('login')
|
|
# Create your views here.
|
|
@login_required(login_url=login_url)
|
|
def homepage(request):
|
|
template_name = 'employee_module_home.html'
|
|
return render(request, template_name)
|
|
|
|
@login_required(login_url=login_url)
|
|
def time_tracking(request):
|
|
|
|
username = request.user.username
|
|
employee = Employee.objects.get(username=username)
|
|
try:
|
|
timelog_record = TimeLog.objects.filter(username=username, activity_type='work', end_time=None).latest('date')
|
|
except Exception as e:
|
|
print(e)
|
|
timelog_record = None
|
|
|
|
plan_records = Plan.objects.filter(username=username, date=datetime.date.today())
|
|
|
|
if request.method == 'POST':
|
|
if 'start_work' in request.POST:
|
|
new_record = TimeLog(username=employee, date=datetime.date.today(), begin_time=datetime.datetime.now().time(), activity_type='work')
|
|
new_record.save()
|
|
return HttpResponseRedirect(reverse('employee_module:time_tracking'))
|
|
|
|
if 'stop_work' in request.POST:
|
|
timelog_record.end_time = datetime.datetime.now().time()
|
|
timelog_record.save()
|
|
return HttpResponseRedirect(reverse('employee_module:time_tracking'))
|
|
|
|
template_name = 'employee_module_time_tracking.html'
|
|
if not timelog_record:
|
|
context = {'activity_in_progress': False, 'schedule': plan_records, 'work_started_at': None, }
|
|
else:
|
|
context = {'activity_in_progress': True, 'schedule': plan_records, 'work_started_at': timelog_record.begin_time}
|
|
return render(request=request, context=context, template_name=template_name)
|
|
|
|
@login_required(login_url=login_url)
|
|
def show_employee_data(request):
|
|
template_name = 'employee_module_show_employee_data.html'
|
|
return render(request, template_name)
|
|
|
|
def show_employee_data_api(request):
|
|
if request.method == 'POST':
|
|
if request.user.is_authenticated:
|
|
username = request.user.username
|
|
empl = User.objects.select_related('employee').get(username=username)
|
|
record_employee = empl.employee.__dict__
|
|
record_user = empl.__dict__
|
|
response_dict = dict(record_user, **record_employee)
|
|
for i in ['_state', 'password']:
|
|
response_dict.pop(i)
|
|
print(response_dict)
|
|
return JsonResponse(response_dict, safe=False)
|
|
else:
|
|
return JsonResponse({'error': 'not_authenticated'})
|
|
|
|
|
|
@login_required(login_url=login_url)
|
|
def show_schedule(request):
|
|
template = 'employee_module_show_schedule.html'
|
|
return render(request, template)
|
|
|
|
|
|
@login_required(login_url=login_url)
|
|
def show_timelog(request):
|
|
template = 'employee_module_show_timelog.html'
|
|
return render(request, template)
|
|
|
|
|
|
def show_employee_plan_api(request):
|
|
if request.method == 'POST':
|
|
body = json.loads(request.body)
|
|
username = request.user.username
|
|
start_date = body['start_date'].split('T')[0]
|
|
end_date = body['end_date'].split('T')[0]
|
|
|
|
start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
|
|
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
|
|
|
|
print(username, start_date, end_date)
|
|
|
|
user_obj = Employee.objects.get(pk=username)
|
|
|
|
query_result = Plan.objects.filter(username=user_obj, date__range= [start_date, end_date])
|
|
response = list(query_result.values())
|
|
return JsonResponse(response, safe=False)
|
|
|
|
|
|
def show_employee_timelog_api(request):
|
|
if request.method == 'POST':
|
|
body = json.loads(request.body)
|
|
username = request.user.username
|
|
start_date = body['start_date'].split('T')[0]
|
|
end_date = body['end_date'].split('T')[0]
|
|
|
|
start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
|
|
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
|
|
|
|
print(username, start_date, end_date)
|
|
|
|
user_obj = Employee.objects.get(pk=username)
|
|
|
|
query_result = TimeLog.objects.filter(username=user_obj, date__range= [start_date, end_date])
|
|
response = list(query_result.values())
|
|
return JsonResponse(response, safe=False) |