Django休假管理系统

时间:2019-05-15 09:48:58

标签: python sql django database django-models

我正在尝试创建一个Django休假管理系统,每个用户都可以在该系统中登录并申请休假,并接收有关其批准或拒绝的电子邮件通知。

管理员可以选择更改个人资料,接受和拒绝,并可以阻止某人申请休假。

我希望你们能仔细阅读我的代码,并告诉我是否走了正确的道路,并为我提供了一些模型和代码。 同样在请求离开中,我想通过表单将请求与当前登录的用户一起传递。以及如何使用电子邮件选项发送和接收电子邮件。

预先感谢

Views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from .forms import Leave_Form, RegistrationForm, userReg_Form  # Form Name
from . models import Employee, Leave # Class Name
from django.shortcuts import render, redirect, render_to_response
from django.contrib import messages
from django.contrib.auth import login, logout, authenticate, update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm, UserCreationForm
from django.http import HttpResponseRedirect, JsonResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _


def home(request):  # To view contents of the home page
    return render(request, "projectfiles/HomePage.html")



@login_required(login_url='LoginPage')
def request_leave(request):
    form = Leave_Form(request.POST or None)
    if form.is_valid():
        # abc = form.save(commit=False)
        # abc.employee_leaves = Employee
        # abc.save()
        form.save()

    context = {'form': form}
    # messages.success(request, ('Submitted for review!'))
    # return HttpResponseRedirect('Home-Page')
    return render(request, 'projectfiles/reqleave.html', context)








#User registration with dept
def reg_user(request):
    form = RegistrationForm(request.POST)
    form2 = userReg_Form(request.POST)
    if request.method == 'POST':


        if form.is_valid() and form2.is_valid():
            user = form.save()

            profile = form2.save(commit=False)

            profile.employee_name = user

            profile.save()

            return redirect('Home-Page')

        else:
            form = RegistrationForm()
            form2 = userReg_Form()

    context = {'form': form, 'form2': form2}
    return render(request, 'projectfiles/reg.html', context)






@login_required(login_url='LoginPage')
def showusers(request):
    users = User.objects.all()
    context={'users':users}
    return render(request, 'projectfiles/showusers.html', context)







@login_required(login_url='LoginPage')
def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, user)
            messages.success(request,('Password updated!'))
            return redirect('Home-Page')
        else:
            messages.error(request,('Please correct the error below.'))
    else:
        form = PasswordChangeForm(request.user)
    return render(request, 'projectfiles/change_password.html', {'form': form})

Models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User


class Employee(models.Model):

    employee_name = models.OneToOneField(User, on_delete = models.CASCADE)
    employee_designation = models.CharField(max_length = 10)
    employee_department = models.CharField(max_length = 35)


    def __str__(self):
        return self.employee_department + " " + str(self.employee_name.username)

    class Meta:
        verbose_name_plural = "Employee"







class Leave(models.Model):

    employee_leaves = models.ForeignKey(Employee, on_delete=models.CASCADE, null=True, blank=True)
    leave_Type = models.CharField(max_length=25)

    class Meta:
        verbose_name_plural = "Leave"

    def __str__(self):
        return self.leave_Type

forms.py

# -*- coding: utf-8 -*-
from .models import Employee, Leave
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm


class Leave_Form(forms.ModelForm):

        class Meta:
            model = Leave
            fields = ['leave_Type']
            exclude = ['employee_leaves']
            leave_type_choice = (
            ("Annual leave", "Annual leave"),
            ("Sick leave", "Sick leave"),
            ("Casual leave", "Casual leave"),
            ("Emergency leave", "Emergency leave"), )

            widgets = {
                'leave_Type': forms.Select(choices=leave_type_choice, attrs={'class': 'form-control'}), }





class RegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2'
        ]

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']

        if commit:
            user.save()
        return user





class userReg_Form(forms.ModelForm):

    class Meta:
        model = Employee
        exclude = ['employee_name']
        fields = [
             'employee_designation', 'employee_department',  ]


        employee_department_type = (
            ("Admin", "Admin"),
            ("Finance", "Finance"),
            ("General Staff", "General Staff"),
            ("H.R", "H.R"),
            ("I.T", "I.T"),
            ("Software Development", "Software Development"), )

        widgets={
            'employee_department': forms.Select(choices=employee_department_type, attrs={'class': 'form-control'}), }


urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views


urlpatterns = [

    url(r'^(?i)Home/', views.home, name='Home-Page'),
    url(r'^(?i)request/', views.request_leave, name='Leave-Page'),
    # url(r'^(?i)Home/', views.Home, name='Home-Page'),
    url(r'^password/$', views.change_password, name='change_password'),
    url(r'^showprofile/$', views.showusers, name='Users-Page'),


    # url(r'^(?i)delete/(?P<To_DO_Fun_id>\d+)/$', views.remove, name='Del-Page'),



    #=========login,logout & User registration URLS=========
    url(r'^(?i)login/$', auth_views.LoginView.as_view(template_name='projectfiles/login.html'), name='LoginPage'),
    url(r'^(?i)logout/$', auth_views.LogoutView.as_view(template_name='projectfiles/logout.html'), name='LogoutPage'),
    url(r'^(?i)registraion/$', views.reg_user, name='RegPage'),
    #=========End=========

]

showusers.html

{% include 'projectfiles/base2.html' %}
<title>Users</title>


{% block asd %}


<br><br>
<center>ADD <strong>DELETE</strong> AND <strong>EDIT</strong> PROFILE BUTTONS AND VIEWS
<br><br>
    <a href=#>Add User</a>
</center>
<br><br>

{% if users %}
<div>
    <br>
    <br>
    <center>
    {% for some in users %}
<table style="width:30%" border="18">
        <tr>
        <td>{{some.id}}</td>
        <td>{{some.username}}</td>
        <td><a href=#>Delete</a></td>
        <td><a href=#>Edit</a></td>

    </tr>
    </table>
        {% endfor %}</center>


</div>
{% endif %}

{% endblock %}

reqleave.html

{% include 'projectfiles/base.html' %}
<title>Request Leave</title>

{% block asd %}
<br><br>

<div class="container" align="center">
<form method="POST" style="width:190px">
    {% csrf_token %}
    {{form.as_p}}

<button class="btn btn-success my-2 my-sm-0" type="submit">Apply</button>

</form></div>

{% endblock %}

我期望的结果是看到用户登录并申请请假。 在获得批准的休假后,该休假将从他所准予的全部休假中扣除,例如如果他申请3个年假,将从10个总年假中扣除,并在主屏幕上显示为剩余的7个年假。

,如果他用完了所有叶子,就会产生一个错误。 管理员还可以如何阻止他即使请假也可以申请休假。

0 个答案:

没有答案