diff --git a/.DS_Store b/.DS_Store index bb919ea2..ed253b7b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/osinaweb/.DS_Store b/osinaweb/.DS_Store index ccb02c8a..477d3a9c 100644 Binary files a/osinaweb/.DS_Store and b/osinaweb/.DS_Store differ diff --git a/osinaweb/billing/__pycache__/tasks.cpython-310.pyc b/osinaweb/billing/__pycache__/tasks.cpython-310.pyc new file mode 100644 index 00000000..f39edd2e Binary files /dev/null and b/osinaweb/billing/__pycache__/tasks.cpython-310.pyc differ diff --git a/osinaweb/celerybeat-schedule.db b/osinaweb/celerybeat-schedule.db new file mode 100644 index 00000000..939508de Binary files /dev/null and b/osinaweb/celerybeat-schedule.db differ diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 05c51d67..cdee30b2 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/dump.rdb b/osinaweb/dump.rdb new file mode 100644 index 00000000..3d52c954 Binary files /dev/null and b/osinaweb/dump.rdb differ diff --git a/osinaweb/osinacore/.DS_Store b/osinaweb/osinacore/.DS_Store index 44d53759..5f6ea1ec 100644 Binary files a/osinaweb/osinacore/.DS_Store and b/osinaweb/osinacore/.DS_Store differ diff --git a/osinaweb/osinacore/__pycache__/admin.cpython-310.pyc b/osinaweb/osinacore/__pycache__/admin.cpython-310.pyc index 91fda7cc..e78d25ac 100644 Binary files a/osinaweb/osinacore/__pycache__/admin.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/admin.cpython-310.pyc differ diff --git a/osinaweb/osinacore/__pycache__/models.cpython-310.pyc b/osinaweb/osinacore/__pycache__/models.cpython-310.pyc index cd2053f1..8a016ea2 100644 Binary files a/osinaweb/osinacore/__pycache__/models.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/osinacore/__pycache__/tasks.cpython-310.pyc b/osinaweb/osinacore/__pycache__/tasks.cpython-310.pyc new file mode 100644 index 00000000..ba4d670b Binary files /dev/null and b/osinaweb/osinacore/__pycache__/tasks.cpython-310.pyc differ diff --git a/osinaweb/osinacore/__pycache__/urls.cpython-310.pyc b/osinaweb/osinacore/__pycache__/urls.cpython-310.pyc index 44e60238..793d29bf 100644 Binary files a/osinaweb/osinacore/__pycache__/urls.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/urls.cpython-310.pyc differ diff --git a/osinaweb/osinacore/__pycache__/views.cpython-310.pyc b/osinaweb/osinacore/__pycache__/views.cpython-310.pyc index af612537..2e483c29 100644 Binary files a/osinaweb/osinacore/__pycache__/views.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osinacore/admin.py b/osinaweb/osinacore/admin.py index 8d79560a..14e6abc1 100644 --- a/osinaweb/osinacore/admin.py +++ b/osinaweb/osinacore/admin.py @@ -36,4 +36,5 @@ admin.site.register(Point) admin.site.register(DailyReport) admin.site.register(BusinessType) admin.site.register(PointActivity) +admin.site.register(Connection) diff --git a/osinaweb/osinacore/migrations/0062_connection.py b/osinaweb/osinacore/migrations/0062_connection.py new file mode 100644 index 00000000..679f004c --- /dev/null +++ b/osinaweb/osinacore/migrations/0062_connection.py @@ -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)), + ], + ), + ] diff --git a/osinaweb/osinacore/migrations/0063_alter_connection_date.py b/osinaweb/osinacore/migrations/0063_alter_connection_date.py new file mode 100644 index 00000000..360d7281 --- /dev/null +++ b/osinaweb/osinacore/migrations/0063_alter_connection_date.py @@ -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), + ), + ] diff --git a/osinaweb/osinacore/migrations/__pycache__/0062_connection.cpython-310.pyc b/osinaweb/osinacore/migrations/__pycache__/0062_connection.cpython-310.pyc new file mode 100644 index 00000000..b90da865 Binary files /dev/null and b/osinaweb/osinacore/migrations/__pycache__/0062_connection.cpython-310.pyc differ diff --git a/osinaweb/osinacore/migrations/__pycache__/0063_alter_connection_date.cpython-310.pyc b/osinaweb/osinacore/migrations/__pycache__/0063_alter_connection_date.cpython-310.pyc new file mode 100644 index 00000000..f3236dc6 Binary files /dev/null and b/osinaweb/osinacore/migrations/__pycache__/0063_alter_connection_date.cpython-310.pyc differ diff --git a/osinaweb/osinacore/models.py b/osinaweb/osinacore/models.py index 4a4cb71e..a593b39d 100644 --- a/osinaweb/osinacore/models.py +++ b/osinaweb/osinacore/models.py @@ -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) \ No newline at end of file diff --git a/osinaweb/osinacore/tasks.py b/osinaweb/osinacore/tasks.py new file mode 100644 index 00000000..c623df84 --- /dev/null +++ b/osinaweb/osinacore/tasks.py @@ -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 + ) \ No newline at end of file diff --git a/osinaweb/osinacore/templates/.DS_Store b/osinaweb/osinacore/templates/.DS_Store index 2c3a5855..a1d4d95f 100644 Binary files a/osinaweb/osinacore/templates/.DS_Store and b/osinaweb/osinacore/templates/.DS_Store differ diff --git a/osinaweb/osinacore/views.py b/osinaweb/osinacore/views.py index be7dbf97..bc20f416 100644 --- a/osinaweb/osinacore/views.py +++ b/osinaweb/osinacore/views.py @@ -498,4 +498,6 @@ def customer_invoices(request, *args, **kwargs): } - return render(request, 'customer_dashboard/listing_pages/customer-invoices.html', context) \ No newline at end of file + return render(request, 'customer_dashboard/listing_pages/customer-invoices.html', context) + + diff --git a/osinaweb/osinaweb/__pycache__/celery.cpython-310.pyc b/osinaweb/osinaweb/__pycache__/celery.cpython-310.pyc new file mode 100644 index 00000000..50db7250 Binary files /dev/null and b/osinaweb/osinaweb/__pycache__/celery.cpython-310.pyc differ diff --git a/osinaweb/osinaweb/__pycache__/middleware.cpython-310.pyc b/osinaweb/osinaweb/__pycache__/middleware.cpython-310.pyc new file mode 100644 index 00000000..4e8ee908 Binary files /dev/null and b/osinaweb/osinaweb/__pycache__/middleware.cpython-310.pyc differ diff --git a/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc b/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc index 3545a689..a2ef73de 100644 Binary files a/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc and b/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc differ diff --git a/osinaweb/osinaweb/celery.py b/osinaweb/osinaweb/celery.py new file mode 100644 index 00000000..3c37056b --- /dev/null +++ b/osinaweb/osinaweb/celery.py @@ -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 + }, +} diff --git a/osinaweb/osinaweb/middleware.py b/osinaweb/osinaweb/middleware.py new file mode 100644 index 00000000..45f628eb --- /dev/null +++ b/osinaweb/osinaweb/middleware.py @@ -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 diff --git a/osinaweb/osinaweb/settings.py b/osinaweb/osinaweb/settings.py index 109622dc..5875a970 100644 --- a/osinaweb/osinaweb/settings.py +++ b/osinaweb/osinaweb/settings.py @@ -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'