def create_title(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) # check if there are employee records all_employees = Employee.find_all_employees() if not all_employees: messages.error(request, 'No employee record found in the system!') new_title = NewTitle() if request.method == 'POST': new_title = NewTitle(request.POST) if new_title.is_valid(): cleaned_data = new_title.cleaned_data e_id = cleaned_data['current_employee'] new_title_record = JobTitle() new_title_record.employee = Employee.objects.get(pk=e_id) new_title_record.from_date = cleaned_data['from_date'] new_title_record.to_date = cleaned_data['to_date'] new_title_record.title = cleaned_data['title'] # update the old to_date JobTitle.update_title_to_date(e_id, cleaned_data['from_date']) new_title_record.save() return render(request, 'create/title_success.html') return render(request, 'create/title.html', {'new_title': new_title})
def create_assignment(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) # check if there are employee records all_employees = Employee.find_all_employees() if not all_employees: messages.error(request, 'No employee record found in the system!') # check if there are employee records all_departments = Department.find_all_departments() if not all_departments: messages.error(request, 'No department record found in the system!') new_assignment = NewAssignment() if request.method == 'POST': new_assignment = NewAssignment(request.POST) if new_assignment.is_valid(): cleaned_data = new_assignment.cleaned_data e_id = cleaned_data['employee_'] d_id = cleaned_data['department_'] EmployeeWorksAt.update_assignment_to_date( e_id, cleaned_data['from_date']) new_assignment_record = EmployeeWorksAt() new_assignment_record.employee = Employee.objects.get(pk=e_id) new_assignment_record.department = Department.objects.get(pk=d_id) new_assignment_record.from_date = cleaned_data['from_date'] new_assignment_record.to_date = cleaned_data['to_date'] # update the old to_date new_assignment_record.save() return render(request, 'create/assignment_success.html.html') return render(request, 'create/assignment.html', {'new_assignment': new_assignment})
def get_all_employees(): all_employees = Employee.find_all_employees() if not all_employees: employees = [('', '')] else: employees = [(i['employee_id'], i['first_name'] + " " + i['last_name']) for i in all_employees] return employees
def delete_employee(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) all_employees = Employee.find_all_employees() return render(request, 'delete/employee.html', {'employees': all_employees})
def delete_employee_record(request, employee_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) all_employees = Employee.find_all_employees() employee = Employee.objects.filter(id=employee_id) if employee: employee = employee[0] else: messages.error(request, "No such employee.") return render(request, 'delete/employee.html', {'employees': all_employees}) try: employee.delete() return render(request, 'delete/employee_success.html') except IntegrityError: messages.error( request, "Employee '{} {}' is referenced through restricted foreign keys.". format(employee.first_name, employee.last_name)) return render(request, 'delete/employee.html', {'employees': all_employees})
def read_employee_all(request): employees = Employee.find_all_employees() return render(request, 'read/show_all_employees.html', {'employees': employees})