Files
ihrm/attendance/scheduler.py
Horilla 07db36a777 Dev/v2.0 (#965)
* [FIX] BASE: Fixed the permissions in the settings page and updated the payroll permission

* [FIX] DOCKER: Issue with creating migration file for auth user model

* [FIX] EMPLOYEE : Document request view toggle issue fixes

* [FIX] ATTENDANCE: Fixed union of distinct querysets in attendance request

* [FIX] PAYROLL: Fixed reimbursement form excluded field issue

* [UPDT] WHATSAPP: Updated flow json version

* [UPDT] WHATSAPP: Updated meta_token from charfield to text field

* [UPDT] WHATSAPP: Updated whatsapp credential view url to CBV template view

* [FIX] WHATSAPP: Fixed issues in sending messages and added exception message if request failed

* [UPDT] RECRUITMENT: Formatted recruitment view page

* [UPDT] RECRUITMENT: Updated linkedin to check for integration enabled

* [FIX] SETTINGS: Fixed outlook issue on settings page

* [FIX] SETTINGS: Fixed outlook error in settings page

* [RMV] GENERAL: Remove unwanted console log messages

* [UPDT] HORILLA_THEME: Updated permission for ldap and Gmeet in settings page

* [RMV] GENERAL: Remove unwanted console log messages

* [UPDT] BASE: Index page updation

* [UPDT] WHATSAPP: Add whatsapp app to the installed_apps list

* [FIX] BASE: Fixed the permission issue in the general settings section

* [FIX] WHATSAPP: Fixed decorator issue and test message exception issue

* [RMV] GENERAL: Remove unwanted print statements

* [FIX] SETTINGS: Fixed typo in permission for Outlook

* [IMP] Remove formatting of migrations file from the pre-commit hooks

* [RMV] BASE: Unwanted variable from the chart
2025-10-30 15:00:03 +05:30

70 lines
1.9 KiB
Python

import datetime
import sys
import pytz
from apscheduler.schedulers.background import BackgroundScheduler
from django.conf import settings
from base.backends import logger
def create_work_record():
from attendance.models import WorkRecords
from employee.models import Employee
date = datetime.datetime.today()
work_records = WorkRecords.objects.filter(date=date).values_list(
"employee_id", flat=True
)
employees = Employee.objects.exclude(id__in=work_records)
records_to_create = []
for employee in employees:
try:
shift_schedule = employee.get_shift_schedule()
if shift_schedule is None:
continue
shift = employee.get_shift()
record = WorkRecords(
employee_id=employee,
date=date,
work_record_type="DFT",
shift_id=shift,
message="",
)
records_to_create.append(record)
except Exception as e:
logger.error(f"Error preparing work record for {employee}: {e}")
if records_to_create:
try:
WorkRecords.objects.bulk_create(records_to_create)
except Exception as e:
logger.error(f"Failed to bulk create work records: {e}")
if not any(
cmd in sys.argv
for cmd in ["makemigrations", "migrate", "compilemessages", "flush", "shell"]
):
"""
Initializes and starts background tasks using APScheduler when the server is running.
"""
scheduler = BackgroundScheduler(timezone=pytz.timezone(settings.TIME_ZONE))
scheduler.add_job(
create_work_record, "interval", minutes=30, misfire_grace_time=3600 * 3
)
scheduler.add_job(
create_work_record,
"cron",
hour=0,
minute=30,
misfire_grace_time=3600 * 9,
id="create_daily_work_record",
replace_existing=True,
)
scheduler.start()