Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18526

Issue with Updating Attendance Records in Django using MySQL

$
0
0

I am encountering an issue with updating attendance records in my Django application using MySQL. I have a handle_attendance_record function intended to update or create an AttendanceRecord for an employee based on check-in and check-out logs. However, the records are not updating as expected.

Here is the code for my function:

from datetime import datetimeclass AttendanceRecord(models.Model):    # Model definition    ...def handle_attendance_record(self, log_data, log_time):    user_id = str(log_data["UserId"]).zfill(4)    try:        employee = Employee.objects.get(employee_matricule=user_id)    except Employee.DoesNotExist:        print(f"Employee not found for user_id {user_id}")        return    log_date = log_time.date()    record, created = AttendanceRecord.objects.get_or_create(        employee=employee, date=log_date, defaults={'check_in': None, 'check_out': None})    if log_data["Type"] == "CheckOut" and record.check_out is None:        record.check_out = log_time.time()    elif log_data["Type"] == "CheckIn" and record.check_in is None:        record.check_in = log_time.time()    if record.check_in and record.check_out:        record.working_hours = datetime.combine(log_date, record.check_out) - datetime.combine(log_date, record.check_in)    record.save()

And here is a snippet of the data from my MySQL database:

mysql> select * from ap_attendancelog;+----+----------+------------+------------+| id | user_id  | log_type   | log_time   |+----+----------+------------+--------------------+|  1 | 0004     | CheckIn    | 2023-12-19 11:15:08||  2 | 0004     | CheckOut   | 2023-12-19 21:15:08|+----+----------+------------+--------------------+mysql> select * from ap_attendancerecord;+----+------------+-----------+-----------+| id | employee_id| check_in  | check_out |+----+------------+-----------+-----------+|  1 | 0004       | 11:15:08  | NULL      |+----+------------+-----------+-----------+

Viewing all articles
Browse latest Browse all 18526

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>