验证Django mod中的Django model字段

2024-06-09 18:43:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个叫出勤的Django模型,它有一个员工的打卡时间和打卡时间,以及该条目的状态,以查看它是否被授权。然后,我在做另一个模型叫工资单。我想这里面的考勤条目检查,看看所有授权的条目,然后对他们做一些行动。如何检查所有考勤条目的所有状态字段?你知道吗

编辑:更新以更好地阐述我的问题。

为了更好地阐述我的问题,以下是我如何设置考勤模型的:

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,)

如果仔细查看work_entry_status,它是一个choice CharField,它将包含条目的状态(默认情况下未授权)。你知道吗

我想创建一个Payroll模型,它将检查CWorkAttendance模型中的所有行,并检查它们的work_entry_status字段以查看它们是否被授权,这是我想学习的方法。你知道吗

如果这些字段被授权,我需要抓取行的work_employeework_duration,以及雇员的原始CEmployees行的一些细节。你知道吗

我希望我的工资单/工资单模型如下所示:

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.

谢谢你


Tags: thefrom模型datetimemodelsstatusemployeenow
1条回答
网友
1楼 · 发布于 2024-06-09 18:43:28

我不确定我是否理解你的要求,所以如果我误解了请告诉我。你知道吗

# `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
    # ....

更新

既然您想手动执行,我建议您使用一个单独的视图来生成工资单。重要的是要知道这个工资单的date_fromdate_to。我想是管理者可以访问这个视图,所以您需要为它设置适当的访问控制。我还认为您需要有一个payslip_to_date,即使您要在当前日期之前生成它,这将有助于保存记录。我假设你在下面的代码中有这个列。你知道吗

你知道吗视图.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

如果您希望以后自动生成工资单,您可能希望每月自动生成一次工资单。为此,您可以使用类似芹菜的东西为每个员工在后台运行定期任务。如果是这种情况,您可以将上面的代码移到utils.py这样的文件中。您可以创建一个方法,该方法采用employee\u id,从\u date到\u date,然后生成payslip对象,将payslip\u id返回给调用方法

相关问题 更多 >