main
emile 1 year ago
parent 49ee9a3c58
commit 61c71b1a6a

BIN
.DS_Store vendored

Binary file not shown.

BIN
osinaweb/.DS_Store vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -36,4 +36,5 @@ admin.site.register(Point)
admin.site.register(DailyReport)
admin.site.register(BusinessType)
admin.site.register(PointActivity)
admin.site.register(Connection)

@ -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),
),
]

@ -311,3 +311,14 @@ class DailyReport(models.Model):
date = models.CharField(max_length=40)
time = models.CharField(max_length=40)
staff = models.ForeignKey(StaffProfile, on_delete=models.CASCADE, null=True,blank=True, related_name='dailyreport_staff')
class Connection(models.Model):
STATUS_CHOICES = (
('Online', 'Online'),
('Offline', 'Offline'),
)
status = models.CharField(max_length=200, choices=STATUS_CHOICES, null=True)
date = models.DateTimeField(null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)

@ -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.

@ -498,4 +498,6 @@ def customer_invoices(request, *args, **kwargs):
}
return render(request, 'customer_dashboard/listing_pages/customer-invoices.html', context)
return render(request, 'customer_dashboard/listing_pages/customer-invoices.html', context)

@ -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

@ -17,6 +17,10 @@ import os
BASE_DIR = Path(__file__).resolve().parent.parent
# settings.py
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
@ -56,6 +60,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'osinaweb.middleware.OnlineConnectionMiddleware',
]
ROOT_URLCONF = 'osinaweb.urls'

Loading…
Cancel
Save