def delete_salary_record(request, employee_id, salary_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) salary = Salary.objects.filter(id=salary_id) if salary: salary = salary[0] try: Salary.update_previous_later_salary_dates_when_delete( employee_id, salary_id) salary.delete() return render(request, 'delete/salary_success.html', {'employee_id': employee_id}) except IntegrityError as e: messages.error( request, "This salary record is referenced through restricted foreign keys." ) else: messages.error(request, "No such salary record.") salary_history = Salary.find_salary_history_by_id(employee_id) employee = Employee.find_employee_by_id(employee_id) return render(request, 'delete/salary.html', { 'salary_history': salary_history, 'employee': employee })
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 delete_title_record(request, employee_id, title_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) title = JobTitle.objects.filter(id=title_id) if title: title = title[0] try: JobTitle.update_previous_later_title_dates_when_delete( employee_id, title_id) title.delete() return render(request, 'delete/title_success.html', {'employee_id': employee_id}) except IntegrityError: messages.error( request, "This title record is referenced through restricted foreign keys." ) else: messages.error(request, "No such title record.") title_history = JobTitle.find_title_history_by_id(employee_id) employee = Employee.find_employee_by_id(employee_id) return render(request, 'delete/title.html', { 'title_history': title_history, 'employee': employee })
def update_department(request, department_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) department = Department.objects.filter(id=department_id) if department: department = department[0] else: return render(request, 'update/department.html', { 'department': None, 'id': None }) department_form = UpdateDepartment(initial={'name': department.name}) if request.method == 'POST': department_form = UpdateDepartment(request.POST, instance=department) if department_form.has_changed(): if department_form.is_valid(): department_form.save() return render(request, 'update/department_success.html') else: messages.error(request, NO_CHANGE) return render(request, 'update/department.html', { 'department': department_form, 'id': department_id })
def delete_assignment_record(request, employee_id, assignment_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) assignment = EmployeeWorksAt.objects.filter(id=assignment_id) if assignment: assignment = assignment[0] try: EmployeeWorksAt.update_previous_later_assignment_dates_when_delete( employee_id, assignment_id) assignment.delete() return render(request, 'delete/assignment_success.html', {'employee_id': employee_id}) except IntegrityError: messages.error( request, "This assignment record is referenced through restricted foreign keys." ) else: messages.error(request, "No such assignment record.") assignment_history = EmployeeWorksAt.find_assignment_history_by_id( employee_id) employee = Employee.find_employee_by_id(employee_id) return render(request, 'delete/assignment.html', { 'assignment_history': assignment_history, 'employee': employee })
def update_user_record(request, user_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) user = User.objects.filter(id=user_id) if user: user = user[0] else: messages.error(request, "No such user.") if request.method == 'POST': system_user = SystemUser.objects.filter(user_id=user_id) if system_user: system_user_form = SystemUserForm(request.POST, instance=system_user[0]) system_user_form.save() return render(request, 'update/user_success.html') else: messages.error(request, "No such user.") system_user_form = SystemUserForm() return render(request, 'update/user.html', { 'system_user': system_user_form, 'username': user.username })
def delete_order(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) orders = Order.find_all_orders() return render(request, 'delete/order.html', {'orders': orders})
def delete_user(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) users = SystemUser.find_all_users() return render(request, 'delete/user.html', {'users': users})
def update_employee(request, employee_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) employee = Employee.objects.filter(id=employee_id) if not employee: return render(request, 'read/show_employee_result.html', {'employee': None}) else: employee = employee[0] initial_data = { 'id': employee_id, 'first_name': employee.first_name, 'last_name': employee.last_name, 'birth_date': employee.birth_date, 'gender': employee.gender, 'hire_date': employee.hire_date } forms = UpdateEmployee(initial=initial_data) if request.method == 'POST': forms = UpdateEmployee(request.POST, instance=employee) if forms.has_changed(): if forms.is_valid(): forms.save() return render(request, 'update/employee_success.html') else: messages.error(request, NO_CHANGE) return render(request, 'update/employee.html', { 'employee': forms, 'employee_id': initial_data['id'] })
def delete_salary(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) employee = EmployeeIdentifier() if request.method == 'POST': employee = EmployeeIdentifier(request.POST) if employee.is_valid(): cleaned_data = employee.cleaned_data if cleaned_data.get('id'): target_employee = Employee.find_employee_all_info_by_id( cleaned_data.get('id')) if target_employee: return render(request, 'delete/salary_employee.html', {'employee': target_employee}) if cleaned_data.get('first_name') and cleaned_data.get( 'last_name'): target_employee = Employee.find_employee_by_name( cleaned_data.get('first_name'), cleaned_data.get('last_name')) target_employee = Employee.find_employee_all_info_by_id( target_employee['employee_id']) if target_employee: return render(request, 'delete/salary_employee.html', {'employee': target_employee}) messages.error(request, "No such employee.") return render(request, 'delete/before_search.html', {'employee': employee})
def delete_department(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) all_departments = Department.find_all_departments() return render(request, 'delete/department.html', {'departments': all_departments})
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_title_history(request, employee_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) title_history = JobTitle.find_title_history_by_id(employee_id) employee = Employee.find_employee_by_id(employee_id) return render(request, 'delete/title.html', { 'title_history': title_history, 'employee': employee })
def delete_salary_history(request, employee_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) salary_history = Salary.find_salary_history_by_id(employee_id) employee = Employee.find_employee_by_id(employee_id) return render(request, 'delete/salary.html', { 'salary_history': salary_history, 'employee': employee })
def delete_assignment_history(request, employee_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) assignment_history = EmployeeWorksAt.find_assignment_history_by_id( employee_id) employee = Employee.find_employee_by_id(employee_id) return render(request, 'delete/assignment.html', { 'assignment_history': assignment_history, 'employee': employee })
def create_employee(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) new_employee = NewEmployee() if request.method == 'POST': new_employee = NewEmployee(request.POST) if new_employee.is_valid(): new_employee.save() return render(request, 'create/employee_success.html') return render(request, 'create/employee.html', {'new_employee': new_employee})
def create_department(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) new_department = NewDepartment() if request.method == 'POST': new_department = NewDepartment(request.POST) if new_department.is_valid(): new_department.save() return render(request, 'create/department_success.html') return render(request, 'create/department.html', {'new_department': new_department})
def create_order(request): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) order = NewOrder() sale_staff = SaleStaff.find_all_sale_staff() if not sale_staff: messages.error( request, "No sale staff in the system. Please assign one first.") else: if request.method == 'POST': order = NewOrder(request.POST) if order.is_valid(): order.save() return render(request, 'create/order_success.html') return render(request, 'create/order.html', {'order': order})
def delete_order_record(request, order_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) order = Order.objects.filter(id=order_id) if order: order = order[0] try: order.delete() return render(request, 'delete/order_success.html') except IntegrityError: messages.error( request, "This order record is referenced through restricted foreign keys." ) else: messages.error(request, "No such order.") orders = Order.find_all_orders() return render(request, 'delete/order.html', {'orders': orders})
def update_assignment(request, employee_id, assignment_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) employee = Employee.find_employee_by_id(employee_id) current_assignment = EmployeeWorksAt.objects.filter(id=assignment_id) assignment = UpdateAssignment() if current_assignment: current_assignment = current_assignment[0] initial_data = { 'department': current_assignment.department, 'employee': current_assignment.employee, 'from_date': current_assignment.from_date, 'to_date': current_assignment.to_date } assignment = UpdateAssignment(initial=initial_data) else: messages.error(request, "No such assignment record.") if request.method == 'POST': assignment = UpdateAssignment(request.POST, instance=current_assignment) if assignment.is_valid(): cleaned_data = assignment.cleaned_data current_assignment.department = cleaned_data['department'] current_assignment.employee = cleaned_data['employee'] current_assignment.from_date = cleaned_data['from_date'] current_assignment.to_date = cleaned_data['to_date'] EmployeeWorksAt.update_previous_later_assignment_dates( employee_id, assignment_id, cleaned_data['from_date'], cleaned_data['to_date']) current_assignment.save() return render(request, 'update/assignment_success.html', {'employee_id': employee_id}) return render(request, 'update/assignment.html', { 'assignment': assignment, 'employee': employee })
def update_order_record(request, order_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) order_object = Order.objects.filter(id=order_id) if order_object: order_object = order_object[0] else: return render(request, 'read/show_orders.html') order = OrderForm(instance=order_object) if request.method == 'POST': order = OrderForm(request.POST, instance=order_object) if order.has_changed(): if order.is_valid(): order.save() return render(request, 'update/order_success.html') else: messages.error(request, NO_CHANGE) return render(request, 'update/order.html', {'order': order})
def update_title(request, employee_id, title_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) employee = Employee.find_employee_by_id(employee_id) current_title = JobTitle.objects.filter(id=title_id) title = UpdateTitle() if current_title: current_title = current_title[0] initial_data = { 'title': current_title.title, 'employee': current_title.employee, 'from_date': current_title.from_date, 'to_date': current_title.to_date } title = UpdateTitle(initial=initial_data) else: messages.error(request, "No such title record.") if request.method == 'POST': title = UpdateTitle(request.POST, instance=current_title) if title.is_valid(): cleaned_data = title.cleaned_data current_title.title = cleaned_data['title'] current_title.from_date = cleaned_data['from_date'] current_title.to_date = cleaned_data['to_date'] JobTitle.update_previous_later_title_dates( employee_id, title_id, cleaned_data['from_date'], cleaned_data['to_date']) current_title.save() return render(request, 'update/title_success.html', {'employee_id': employee_id}) return render(request, 'update/title.html', { 'title': title, 'title_id': title, 'employee': employee })
def update_salary(request, employee_id, salary_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) employee = Employee.find_employee_by_id(employee_id) current_salary = Salary.objects.filter(id=salary_id) salary = UpdateSalary() if current_salary: current_salary = current_salary[0] initial_data = { 'amount': current_salary.amount, 'from_date': current_salary.from_date, 'to_date': current_salary.to_date, 'employee': current_salary.employee } salary = UpdateSalary(initial=initial_data) else: messages.error(request, "No such salary record.") if request.method == 'POST': salary = UpdateSalary(request.POST, instance=current_salary) if salary.is_valid(): cleaned_data = salary.cleaned_data current_salary.amount = cleaned_data['amount'] current_salary.from_date = cleaned_data['from_date'] current_salary.to_date = cleaned_data['to_date'] Salary.update_previous_later_salary_dates( employee_id, salary_id, cleaned_data['from_date'], cleaned_data['to_date']) current_salary.save() return render(request, 'update/salary_success.html', {'employee_id': employee_id}) return render(request, 'update/salary.html', { 'salary': salary, 'salary_id': salary_id, 'employee': employee })
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 delete_user_record(request, user_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) user = User.objects.filter(id=user_id) if user: user = user[0] if user.is_superuser: messages.error(request, "You cannot delete a super user.") users = SystemUser.find_all_users() return render(request, 'delete/user.html', {'users': users}) try: user.delete() return render(request, 'delete/user_success.html') except IntegrityError: messages.error( request, "User {} is referenced through restricted foreign keys.". format(user.username)) else: messages.error(request, "No such user.") users = SystemUser.find_all_users() return render(request, 'delete/user.html', {'users': users})
def delete_department_record(request, department_id): if not has_admin_access(request): return HttpResponseRedirect(reverse('guest')) all_departments = Department.find_all_departments() department = Department.objects.filter(id=department_id) if department: department = department[0] else: messages.error(request, "No such department.") return render(request, 'delete/department.html', {'departments': all_departments}) try: department.delete() return render(request, 'delete/department_success.html') except IntegrityError: messages.error( request, "Department '{}' is referenced through restricted foreign keys.". format(department.name)) return render(request, 'delete/department.html', {'departments': all_departments})