Hello
parent
49ee9a3c58
commit
61c71b1a6a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,25 @@
|
||||
# Generated by Django 4.2.5 on 2024-03-20 19:31
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('osinacore', '0061_alter_task_end_date_alter_task_start_date'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Connection',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('status', models.CharField(choices=[('Online', 'Online'), ('Offline', 'Offline')], max_length=200, null=True)),
|
||||
('date', models.DateField()),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.5 on 2024-03-20 19:36
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('osinacore', '0062_connection'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='connection',
|
||||
name='date',
|
||||
field=models.DateTimeField(null=True),
|
||||
),
|
||||
]
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,24 @@
|
||||
# tasks.py
|
||||
from celery import shared_task
|
||||
from osinacore.models import *
|
||||
from django.db.models import Q
|
||||
|
||||
@shared_task
|
||||
def set_offline():
|
||||
print('Hello')
|
||||
all_staff_profiles = StaffProfile.objects.all()
|
||||
|
||||
staff_without_working_points = all_staff_profiles.exclude(
|
||||
Q(task__point__status='Working On')
|
||||
)
|
||||
|
||||
users_without_working_points = [staff_profile.user for staff_profile in staff_without_working_points]
|
||||
print(users_without_working_points)
|
||||
|
||||
# Create a new connection record for each user with status set to 'Offline'
|
||||
for user in users_without_working_points:
|
||||
Connection.objects.create(
|
||||
status='Offline',
|
||||
date=timezone.now(),
|
||||
user=user
|
||||
)
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,23 @@
|
||||
|
||||
import os
|
||||
from celery import Celery
|
||||
from celery.schedules import crontab
|
||||
from datetime import timedelta
|
||||
# Set the default Django settings module for the 'celery' program.
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'osinaweb.settings')
|
||||
|
||||
# Create a Celery instance.
|
||||
celery_app = Celery('osinaweb', broker_url='redis://127.0.0.1:6379/0')
|
||||
|
||||
# Load task modules from all registered Django app configs.
|
||||
celery_app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||
|
||||
# Autodiscover tasks from all registered apps
|
||||
celery_app.autodiscover_tasks()
|
||||
|
||||
celery_app.conf.beat_schedule = {
|
||||
'set-offline-every-minute': {
|
||||
'task': 'osinacore.tasks.set_offline', # Assuming your task is in tasks.py in your_app
|
||||
'schedule': crontab(minute='*/1'), # Run every minute
|
||||
},
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
from datetime import datetime, timedelta
|
||||
from osinacore.models import Connection
|
||||
|
||||
class OnlineConnectionMiddleware:
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
# Call the view function or next middleware in the stack
|
||||
response = self.get_response(request)
|
||||
|
||||
# If user is authenticated, create or update the Connection instance
|
||||
if request.user.is_authenticated:
|
||||
current_datetime = datetime.now()
|
||||
|
||||
thirty_minutes_ago = current_datetime - timedelta(minutes=30)
|
||||
|
||||
# Check if an online Connection instance exists within the last 30 minutes
|
||||
existing_connection = Connection.objects.filter(
|
||||
user=request.user,
|
||||
date__gte=thirty_minutes_ago,
|
||||
status='Online'
|
||||
).first()
|
||||
|
||||
# If there is no online connection within the last 30 minutes, create a new one
|
||||
if not existing_connection:
|
||||
Connection.objects.create(user=request.user, status='Online', date=current_datetime)
|
||||
|
||||
return response
|
Loading…
Reference in New Issue