You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
# tasks.py
|
|
from celery import shared_task
|
|
from osinacore.models import *
|
|
from django.db.models import Q
|
|
from datetime import datetime, timedelta
|
|
import pytz
|
|
|
|
@shared_task
|
|
def set_offline():
|
|
all_staff_profiles = StaffProfile.objects.all()
|
|
for staff_profile in all_staff_profiles:
|
|
last_point_activity = PointActivity.objects.filter(
|
|
point__task__assigned_to=staff_profile
|
|
).last()
|
|
if last_point_activity and last_point_activity.end_time:
|
|
beirut_timezone = pytz.timezone('Asia/Beirut')
|
|
current_time = datetime.now(beirut_timezone)
|
|
if current_time - last_point_activity.end_time > timedelta(minutes=10):
|
|
user = staff_profile.user
|
|
last_connection = Connection.objects.filter(user=user).order_by('-date').first()
|
|
if last_connection and last_connection.status != 'Offline':
|
|
Connection.objects.create(
|
|
status='Offline',
|
|
date=datetime.now(),
|
|
user=user
|
|
)
|
|
Status.objects.create(
|
|
text='Went offline!',
|
|
date=datetime.now().date(),
|
|
time=datetime.now().strftime('%I:%M %p'),
|
|
staff=staff_profile
|
|
)
|
|
else:
|
|
user = staff_profile.user
|
|
last_connection = Connection.objects.filter(user=user).order_by('-date').first()
|
|
if last_connection and last_connection.status != 'Offline':
|
|
Connection.objects.create(
|
|
status='Offline',
|
|
date=datetime.now(),
|
|
user=user
|
|
)
|
|
Status.objects.create(
|
|
text='I am now offline!',
|
|
date=datetime.now().date(),
|
|
time=datetime.now().strftime('%I:%M %p'),
|
|
staff=staff_profile
|
|
)
|
|
|