简体   繁体   中英

Verify a Django model field inside a Django model

I have a Django model called Attendance that has the clock in and clock in times of an employee along with the status of that entry, to see whether it's authorized or not. I then, am making another model called Payroll. I want this to check inside the Attendance entries to see all the Authorized entries and then do some action on them. How do I check all the status fields for all the entries in Attendance?

EDIT: Updated to better elaborate my question.

To better elaborate my question, this is how I've setup my Attendance model:

class CWorkAttendance(models.Model):
    AUTO_ATT = "AU"
    MANUAL_ATT = "MA"
    WORK_ENTRY_TYPES = (
        (AUTO_ATT, "Auto-Attendance"),
        (MANUAL_ATT, "Manual-Attendance"),
    )
    AUTHORIZED = "AU"
    UNAUTHORIZED = "UA"
    WORK_ENTRY_STATUSES = (
        (AUTHORIZED, "Athorized"),
        (UNAUTHORIZED, "Un-Authorized"),
    )
    #Thank you motatoes
    def face_locations_in(self, instance):
        now = datetime.datetime.now()
        return "attendance/{}/{}/in".format(instance.work_employee, now.strftime("%Y/%m/%d"))

    def face_locations_out(self, instance):
        now = datetime.datetime.now()
        return "attendance/{}/{}/out".format(instance.work_employee, now.strftime("%Y/%m/%d"))

    work_employee = models.ForeignKey('CEmployees', on_delete=models.CASCADE,)
    work_start_time = models.DateTimeField()
    work_end_time = models.DateTimeField(null=True)
    work_duration = models.IntegerField(null=True)
    work_entry_type = models.CharField(max_length=2,choices=WORK_ENTRY_TYPES)
    work_entry_status = models.CharField(max_length=2, choices=WORK_ENTRY_STATUSES, default=WORK_ENTRY_STATUSES[1][0])
    employee_face_captured_in = models.ImageField(upload_to=face_locations_in,)#////////
    employee_face_captured_out = models.ImageField(upload_to=face_locations_out,)

If you look closely at the work_entry_status , it's a choice CharField that will contain the status of the entry (UNAUTHORIZED by default).

I want to create a Payroll model that will check for all the rows in the CWorkAttendance model and check their work_entry_status fields to see if they are Authorized, which is what I want to learn how to do.

If those fields are authorized, I want the grab the row's work_employee , work_duration and also some details from the original CEmployees row for the employee.

This is what I want my Payslip/Payroll model to look like:

class Payslip(models.Model):
    GENERATED = "GEN"
    CONFIRMED = "CON" 
    PAYSLIP_STATUS = (
        (GENERATED, "Generated-UNSAVED"),
        (CONFIRMED, "Confirmed-SAVED"),
    )

    payslip_number = models.IntegerField()#MM/YY/AUTO_GENERATED_NUMBER(AUTO_INCREMENT)
    payslip_employee = models.ForeignKey('CEmployees', on_delete=models.CASCADE,)#Choose the employee from the master table CEmployees
    payslip_generation_date = models.DateTimeField(default=datetime.datetime.now())#Date of the payroll generation
    payslip_total_hours = models.IntegerField()#Total hours that the employee worked
    payslip_from_date = models.DateField()"""The date from when the payslip will be made. The payslip will be manual for now, so generate it after choosing a a date to generate from."""
    payslip_total_basic_seconds = models.IntegerField()#Total seconds the employee worked
    payslip_total_ot_seconds = models.IntegerField()#Total overtime seconds the employee worked
    payslip_basic_hourly_rate = models.IntegerField()#The basic hourly rate of the employee mentioned here. Take from the master employees table.
    payslip_basic_ot_rate = models.IntegerField()#Taking the basic overtime rate from the master table
    payslip_total_amount = models.FloatField()#The total amount of the payslip
    payslip_entry_status = models.CharField(max_length=3, default=GENERATED)#The status of the pay slip.

Thanks,

Not sure if I understand your requirements well, so let me know if I misunderstood.

# `employee` is the work_employee in question

# if you don't want to filter by employee, remove `work_employee=employee`
attendances = CWorkAttendance.objects.filter(work_entry_status=CWorkAttendance.AUTHORIZED, work_employee=employee)

for attendances in attendances:
    # do things with this attendance record
    attendance.work_duration 
    attendance.employee
    # ....

Update

Since you would like to do it manually, I would suggest having a separate view to generate the Payslip. The important thing is to know the date_from and the date_to for this payslip. I imagine that it is the managers who would have access to this view, so you would need the proper access controls set for it. I also think you need to have a payslip_to_date even if you are going to generate it until the current date, which will be useful for record keeping. I assume you have that column in the code below.

views.py:

from django.views import View


class GeneratePayslip(View):
    """
     make sure you have the right access controls set to this view
    """
    def post(self, request, **kwargs):
       employee_id = kwags.POST.get("employee_id")
       date_from = kwargs.POST.get("from_date")
       date_to = kwargs.POST.get("to_date")

       # we fetch all the objects within range
       attendances = CWorkAttendance.objects.filter( \
                work_entry_status=CWorkAttendance.AUTHORIZED, \
                work_employee_id=employee_id, \
                work_start_time__gte=date_from, \
                work_end_time__lte=date_to \
       )

       hours = 0
       for attendance in attendances:
           # perform calculations to compute total sum and rate
           pass

       # create the payslip object here ..

       # redirect to a success page and return

If you wanted to do it automatically later on, you may want to generate payslips automatically, once a month. For that you could use something like Celery to have periodic tasks that run in the background, for each employee. If this is the case you could move the above code to a file such as utils.py . you can create a method which takes employee_id, from_date, to_date, and then generate the payslip object, returning the payslip_id to the calling method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM