Beispiel #1
0
def add_employee(request):
    context = RequestContext(request)
    context_dict = {}
    hr_user = request.user.groups.filter(name="Human Resources").exists()
    mgr_user = request.user.groups.filter(name="Shift Manager").exists()
    employee_list = get_employee_list()

    if request.method == "POST":
        form = EmployeeForm(request.POST)
        profile = UserProfile()

        if form.is_valid():
            
            employee = form.save(commit=False)
            # Create login credentials.
            username = employee.first_name + "_" + employee.last_name
            new_user = User.objects.create_user(username=username, password="******")
            new_user.first_name = employee.first_name
            new_user.last_name = employee.last_name
            new_user.save()

            employee.user = new_user
            profile.user = new_user
            profile.save()

            default_schedule = Schedule()
            default_schedule.save()
            employee.schedule = default_schedule

            # Create/assign user groups based on employee department.
            assigned_group, _ = Group.objects.get_or_create(name=employee.department)
            assigned_group.user_set.add(employee.user)

            employee.save()

            return index(request)
        else:
            print form.errors
    else:
        form = EmployeeForm()

    context_dict['form'] = form
    context_dict['employee_list'] = employee_list
    context_dict['hr_user'] = hr_user
    context_dict['mgr_user'] = mgr_user

    return render_to_response('hris_system/add_employee.html', context_dict, context)
Beispiel #2
0
def edit_employee_page(request, employee_url):
    context = RequestContext(request)
    context_dict = {}
    hr_user = request.user.groups.filter(name="Human Resources").exists()
    mgr_user = request.user.groups.filter(name="Shift Manager").exists()
    employee_list = get_employee_list()
    on_pto = False

    employee = Employee.objects.get(id=employee_url)
    if employee.status == "Personal Time Off":
        on_pto = True

    form = EmployeeForm(request.POST or None, instance=employee)
    if form.is_valid():
        employee_changes = form.save(commit=False)
        # Update the employee's user groups with their new department -- if they should no longer have access to their
        # previous department's group, the employee will need this user group removed in the admin interface.
        employee_group, c = Group.objects.get_or_create(name=employee.department)
        employee_group.user_set.add(employee.user)

        # This handles the scenario when an employee is on PTO and manually changed off.
        if on_pto is True and employee_changes.status != "Personal Time Off":
            pto_to_modify = modify_pto(employee)
            for i in pto_to_modify:
                original_end = i.date_end
                i.date_end = date.today() - timedelta(1)
                i.comments = "PTO end date changed from {0} to {1}.".format(original_end, i.date_end)
                i.save()

        employee_changes.save()

        return HttpResponseRedirect('/hris/')
    
    context_dict['form'] = form
    context_dict['employee'] = employee
    context_dict['employee_list'] = employee_list
    context_dict['hr_user'] = hr_user
    context_dict['mgr_user'] = mgr_user

    return render_to_response('hris_system/edit_employee.html', context_dict, context)