Employee Leave types conditions in DJango rest framework

Question:

I’m working on Hrms application in django rest framework. I’ve created employee details module now next part is leave management system. Actually my company policy has different leave policies like cl,sl,ml,compo off, and permissions.I’m unable to understand how to make the logic for it and and don’t know where to write the logic in serializers or views? Since a newbee i find somewhat difficult. Also when a employee apply a leave it should be requested to T.L. and then should be visible to hr and manager. T.l would give permission and it all should be in application and also in email process. How to make request and approval in rest api also how to send mail using django rest api? Can anyone guide me. If employee select Cl, he got total 12 cl and he can avail monthly once and cl can carryforward upto 3 leaves after that it will expire, then sl means quarterly 2 available, then half day leaves, what will be the logic here and how should i progress?

class LeaveType(models.Model):

 Leave_type = (
    ('CL', 'Casual Leave'),
   ('SL', 'Sick Leave'),
   ('ML', 'Medical Leave'),
    ('Comp Off', 'Compensation'),
    ('L.O.P', 'Loss of Pay')
 )


Leave_Choice = (
    ('Full Day', 'Full Day Leave'),
    ('Fore Noon', 'Fore Noon Only'),
   ('After Noon', 'After Noon Only'),

Status_choices = (
   ('Approved', 'Approved'),
     ('Rejected', 'Rejected'),
    ('Pending', 'Pending'),

leave_type = models.CharField(max_length=50, choices=Leave_type)
status = models.CharField(max_length=50, choices=Status_choices, default='Pending')
leave_choice = models.CharField(max_length=50, choices=Leave_Choice, default='Full Day')

 if leave_type == 'CL':

   total_leave_per_year = 12
   monthly_leave_applicable = 1
   carry_forawrd_monthly_leave = 3

elif leave_type == 'SL':

   Quarterly_days_applicable = 2
   annual_leave_applicable = 8
Asked By: janezio

||

Answers:

Make a separate app for leaves, linked as foreign key field to the main employee.
Then declare leave models, and go with the conditions in the serializer, to allow or not allow employee to apply for leave.
With permissions, you can control the read only or the read/write part of the view.
For sending email, take a look at the documentation:
https://docs.djangoproject.com/en/4.1/topics/email/

Answered By: Maz

So If you want Logic Here is how you can proceed

Make your first API where the user asks for leave allows a New record to be created in the database and the status must be pending also you should allow some user type to be assigned like Team lead or hr to be mentioned.

so firstly we will have our code as

You will need user as foreign key in your table.

LeaveType.objects.create(leave_type="Full_day",status="pending", leave_choice="Cl", assogned_to="some user from foreign key")

Now on second step Is our case where the leave can be only seen by some specific people.

For this you need to look after django permissions
suppose we have 3 user type in your system like HR, Team Lead and Project Manager in your user model.

So you need to write your custom permissons class .

You can do this way
from rest_framework import permissions

class CanViewLeavePermission(permissions.BasePermission):

    def has_permission(self, request, view):
        # check if user is team lead or not 
        return bool(request.user.is_team_lead)

Now we can encorporate our above class along with permission class list in our view

class UpdateLeaveRequestView(UpdateAPIView):
     permisson_class =[IsAuthenticated,CanViewLeavePermission]

after this you just need user id, I mean we will create logic to update our api as the id will be taken as slug from url and we can just query the LeaveType Model with primary key from url and do send email and update the status and can assign to next person and allow some level of validation if pending leave is already updated.

Hope your understood!

Answered By: lord stock