| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/erp/asset/ |
Upload File : |
"""
scheduler.py
This module is used to register scheduled tasks
"""
import sys
from datetime import date, timedelta
from apscheduler.schedulers.background import BackgroundScheduler
from django.urls import reverse
from notifications.signals import notify
def notify_expiring_assets():
"""
Finds all Expiring Assets and send a notification on the notify_before date.
"""
from django.contrib.auth.models import User
from asset.models import Asset
today = date.today()
assets = Asset.objects.all()
bot = User.objects.filter(username="Horilla Bot").first()
for asset in assets:
if asset.expiry_date:
expiry_date = asset.expiry_date
notify_date = expiry_date - timedelta(days=asset.notify_before)
if notify_date == today:
notify.send(
bot,
recipient=asset.owner.employee_user_id,
verb=f"The Asset ' {asset.asset_name} ' expires in {asset.notify_before} days",
verb_ar=f"تنتهي صلاحية الأصل ' {asset.asset_name} ' خلال {asset.notify_before}\
من الأيام",
verb_de=f"Das Asset {asset.asset_name} läuft in {asset.notify_before} Tagen\
ab.",
verb_es=f"El activo {asset.asset_name} caduca en {asset.notify_before} días.",
verb_fr=f"L'actif {asset.asset_name} expire dans {asset.notify_before} jours.",
redirect=reverse("asset-category-view"),
label="System",
icon="information",
)
def notify_expiring_documents():
"""
Finds all Expiring Documents and send a notification on the notify_before date.
"""
from django.contrib.auth.models import User
from horilla_documents.models import Document
today = date.today()
documents = Document.objects.all()
bot = User.objects.filter(username="Horilla Bot").first()
for document in documents:
if document.expiry_date:
expiry_date = document.expiry_date
notify_date = expiry_date - timedelta(days=document.notify_before)
if notify_date == today:
notify.send(
bot,
recipient=document.employee_id.employee_user_id,
verb=f"The document ' {document.title} ' expires in {document.notify_before}\
days",
verb_ar=f"تنتهي صلاحية المستند '{document.title}' خلال {document.notify_before}\
يوم",
verb_de=f"Das Dokument '{document.title}' läuft in {document.notify_before}\
Tagen ab.",
verb_es=f"El documento '{document.title}' caduca en {document.notify_before}\
días",
verb_fr=f"Le document '{document.title}' expire dans {document.notify_before}\
jours",
redirect=reverse("asset-category-view"),
label="System",
icon="information",
)
if today >= expiry_date:
document.is_active = False
if not any(
cmd in sys.argv
for cmd in ["makemigrations", "migrate", "compilemessages", "flush", "shell"]
):
scheduler = BackgroundScheduler()
scheduler.add_job(notify_expiring_assets, "interval", hours=4)
scheduler.add_job(notify_expiring_documents, "interval", hours=4)
scheduler.start()