如果Djnago中的其他视图

时间:2019-05-27 06:55:57

标签: django django-models django-forms django-templates django-views

我正在制作 LMS 。 用户申请请假而管理员接受或拒绝请假的地方。 现在我陷入一个问题,我希望用户通过选择休假类型(休闲,生病,半薪等)通过Django表单申请休假,如果管理员接受了,则数据库中的默认值会更改或从Employee模型中扣除,并且当计数器达到0时,会生成一个错误,提示您没有任何请假,请联系管理员。

我无法理解如何为此做逻辑。

我尝试在视图甚至模型中应用Apply if else语句。

views.py

The function name is "Reject" as I am trying to make changes to the accept function.



def reject_leave(request, id): 
    # employee_item = Employee.objects.get(id=id)
    all_item = Leave.objects.get(id=id)
    all = Employee.objects.get(id=id)
    context = {'all': all,'all_item': all_item}

'''
if the leave_type (choice field of the leaves from the django form) equals to a leave type like Annual leave 

The the program deducts 1 or the amount entered from the total value of Annual leave from the Model Employee
'''

*Sorry for the poor writing I'm not experienced in Django*


***This code is saying that in an illogical way.***

 if leave_type.id is "Annual_leave":
                Annual_leave - 1
    else:
                subject = "Leave Rejected"  # email subject
                email_from = "settings.EMAIL_HOST_USER"  # email from
                to_email = ['someemail@something.com']  # email to

                with open(...) as f:
                    msgbody = f.read()
                    msg = EmailMultiAlternatives(
                    subject=subject, body=msgbody, from_email=email_from, to=to_email)
                    html_template = get_template(...).render()
                    msg.attach_alternative(html_template, "text/html")
                    msg.send()

    return render(request, 'projectfiles/rejectemail.html',context)

forms.py


class Leave_Form(forms.ModelForm):
    to_date = forms.DateField(
        widget=forms.DateInput(format=('%m/%d/%y'),
                               attrs={'class': 'form-control',
                                      'placeholder': ' Month/Date/Year'}))


    from_date = forms.DateField(
        widget=forms.DateInput(format=('%m/%d/%y'),
                               attrs={'class': 'form-control',
                               'placeholder':' Month/Date/Year'}))

    class Meta:
        model = Leave
        fields = ['leave_Type', 'description',
                  'from_date', 'to_date', 'leave_qty']
        exclude = ['employee_leaves', 'submit_date']
        leave_type_choice = (

            ("Annual_leave", "Annual leave"),
            ("Sick_leave", "Sick leave"),
            ("Casual_leave", "Casual leave"),
            ("Emergency_leave", "Emergency leave"),
            ("Half_pay","Half Pay")
    )

        widgets = {


            'leave_Type': forms.Select(choices = leave_type_choice, attrs={'class': 'form-control'}),
            'description': forms.Textarea(
                attrs={'class': 'form-control','placeholder': 'Enter description here', 'rows': 3, 'cols': 21})}

models.py



class Employee(models.Model):

    employee_name = models.OneToOneField(User, on_delete = models.CASCADE)
    employee_designation = models.CharField(max_length = 20)
    employee_department = models.CharField(max_length = 35)
    Annual_leave = models.PositiveSmallIntegerField(default=10)
    Sick_leave = models.PositiveSmallIntegerField(default=3)
    Casual_leave = models.PositiveSmallIntegerField(default=3)
    Half_pay = models.PositiveSmallIntegerField(default=4)
    Emergency_leave = models.PositiveSmallIntegerField(default=3)
    allowed = models.BooleanField(default=False)



    def __str__(self):
                return self.employee_name.username

    class Meta:
        verbose_name_plural = "Employee"





class Leave(models.Model):

    employee_leaves = models.ForeignKey(Employee, on_delete=models.CASCADE)
    leave_Type = models.CharField(max_length=25)
    leave_qty = models.PositiveSmallIntegerField(default=0)
    description = models.CharField(max_length=75, blank=True, null=True)
    submit_date = models.DateTimeField(auto_now_add=True)
    from_date = models.DateField(auto_now=False, auto_now_add=False)
    to_date = models.DateField(auto_now=False, auto_now_add=False)



    class Meta:
        verbose_name_plural = "Leave"

    def __str__(self):
        return self.leave_Type + "  by  " + str(self.employee_leaves)

当管理员接受(或在这种情况下拒绝)请假时。 例如 我希望从已分配的病假总数中扣除1病假或输入的病假数。

1 个答案:

答案 0 :(得分:2)

def reject_leave(request, id):  # overwriting built-in id is not a good idea
    all_item = Leave.objects.get(id=id)  # This is a single item, not all items
    all = Employee.objects.get(id=id)  # this again is a single item, not all items
    context = {'all': all, 'all_item': all_item}

    if leave_type.id is "Annual_leave":
    # What is leave_type?
        Annual_leave - 1 
        # What is Annual_leave? What does -1 supposed to do? 
    # Maybe you meant 
    # employee = Employee.objects.get(id=id)
    # leave = employee.employee_leaves_set.last() 
    # if leave.leave_Type == 'Annual_leave':
    #     employee.Annual_leave -= 1
    #     employee.save()
    else:
        subject = "Leave Rejected"
        email_from = "settings.EMAIL_HOST_USER"
        to_email = ['talhamurtaza@clickmail.info']

        with open('...') as f:
            msgbody = f.read()
            msg = EmailMultiAlternatives(
                subject=subject, body=msgbody, from_email=email_from, to=to_email)
            html_template = get_template(
                "...").render()
            msg.attach_alternative(html_template, "text/html")
            msg.send()

    return render(request, 'projectfiles/rejectemail.html', context)

这有很多问题,我可以肯定地说您还没有阅读过python教程,也没有阅读过pep-8或django教程。因此,请从第一个开始,逐步进行。