From 0f19f0c15c0d81b19aee7f9bb841214cde33ef9b Mon Sep 17 00:00:00 2001 From: i701 Date: Thu, 24 Apr 2025 13:05:20 +0500 Subject: [PATCH] Add Celery periodic task for deactivating expired devices and update requirements --- apibase/celery.py | 10 ++++++++++ apibase/settings.py | 19 ++++++++++--------- billing/views.py | 1 - pyrightconfig.json | 2 ++ requirements.txt | 4 ++++ 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/apibase/celery.py b/apibase/celery.py index 3531d90..70445b1 100644 --- a/apibase/celery.py +++ b/apibase/celery.py @@ -1,6 +1,7 @@ import os from celery import Celery +from celery.schedules import crontab # Set the default Django settings module for the 'celery' program. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apibase.settings") @@ -17,6 +18,15 @@ app.config_from_object("django.conf:settings", namespace="CELERY") app.autodiscover_tasks(["api", "devices"]) +# Add periodic task scheduler +app.conf.beat_schedule = { + "deactivate-expired-devices-every-day": { + "task": "api.tasks.deactivate_expired_devices", + "schedule": crontab(hour=0, minute=0), # Runs daily at midnight + }, +} + + @app.task(bind=True, ignore_result=True) def debug_task(self): print(f"Request: {self.request!r}") diff --git a/apibase/settings.py b/apibase/settings.py index ef80b65..05a38a5 100644 --- a/apibase/settings.py +++ b/apibase/settings.py @@ -28,9 +28,9 @@ env.read_env(os.path.join(BASE_DIR, ".env")) # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = env("SECRET_KEY", default=get_random_secret_key()) -DEBUG = env.bool("DJANGO_DEBUG", default=True) +DEBUG = env.bool("DJANGO_DEBUG", default=True) # type: ignore -ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[]) +ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[]) # type: ignore if DEBUG: @@ -68,6 +68,7 @@ INSTALLED_APPS = [ # third party "django_filters", "corsheaders", + "django_celery_beat", ] if DEBUG: @@ -123,10 +124,10 @@ if not DEBUG: SECURE_SSL_REDIRECT = env("DJANGO_SECURE_SSL_REDIRECT", cast=bool) SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True - SECURE_HSTS_SECONDS = env("SECURE_HSTS_SECONDS", default=3600, cast=int) + SECURE_HSTS_SECONDS = env("SECURE_HSTS_SECONDS", default=3600, cast=int) # type: ignore SECURE_HSTS_INCLUDE_SUBDOMAINS = True SECURE_HSTS_PRELOAD = True - CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS", default=[]) + CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS", default=[]) # type: ignore SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") SECURE_BROWSER_XSS_FILTER = True X_FRAME_OPTIONS = "DENY" @@ -162,7 +163,7 @@ CACHES = { else "django.core.cache.backends.locmem.LocMemCache" ), "LOCATION": ( - env("REDIS_URL", default="redis://redis:6379/") if not DEBUG else "" + env("REDIS_URL", default="redis://redis:6379/") if not DEBUG else "" # type: ignore ), "OPTIONS": ( { @@ -347,10 +348,10 @@ logging.config.dictConfig( EMAIL_BACKEND = ( "django.core.mail.backends.smtp.EmailBackend" # Replace with your preferred backend ) -EMAIL_HOST = env("EMAIL_HOSTNAME", default="") -EMAIL_PORT = env("EMAIL_PORT", cast=int, default=25) -EMAIL_HOST_USER = env("EMAIL_USERNAME", default="") -EMAIL_HOST_PASSWORD = env("EMAIL_PASSWORD", default="") +EMAIL_HOST = env("EMAIL_HOSTNAME", default="") # type: ignore +EMAIL_PORT = env("EMAIL_PORT", cast=int, default=25) # type: ignore +EMAIL_HOST_USER = env("EMAIL_USERNAME", default="") # type: ignore +EMAIL_HOST_PASSWORD = env("EMAIL_PASSWORD", default="") # type: ignore # DEFAULT_FROM_EMAIL = "noreply@sarlink.net" EMAIL_USE_TLS = True diff --git a/billing/views.py b/billing/views.py index e9bf102..646e796 100644 --- a/billing/views.py +++ b/billing/views.py @@ -31,7 +31,6 @@ if not PAYMENT_BASE_URL: raise ValueError( "PAYMENT_BASE_URL is not set. Please set it in your environment variables." ) -print("PAYMENT_BASE_URL from env:", PAYMENT_BASE_URL) class InsufficientFundsError(Exception): diff --git a/pyrightconfig.json b/pyrightconfig.json index b4b408d..6ee0185 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -4,6 +4,8 @@ "reportMissingImports": "error", "include": ["src"], "typeCheckingMode": "standard", + "reportArgumentType": "warning", + "reportUnusedVariable": "warning", "exclude": [ "council-api/**/migrations", "**/__pycache__", diff --git a/requirements.txt b/requirements.txt index df9eb3e..eea8574 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,10 +23,12 @@ click-didyoumean==0.3.1 click-plugins==1.1.1 click-repl==0.3.0 colorama==0.4.6 +cron-descriptor==1.4.5 cryptography==41.0.7 cssselect2==0.7.0 dj-database-url==2.1.0 django==5.2 +django-celery-beat==2.8.0 django-cors-headers==4.3.1 django-debug-toolbar==4.2.0 django-easy-audit==1.3.7 @@ -40,6 +42,7 @@ django-seed==0.3.1 django-storages==1.14.4 django-stubs==5.1.1 django-stubs-ext==5.1.1 +django-timezone-field==7.1 djangorestframework==3.14.0 djangorestframework-simplejwt==5.3.1 djangorestframework-stubs==3.15.1 @@ -82,6 +85,7 @@ pyotp==2.9.0 pypdf==4.0.2 pypng==0.20220715.0 python-bidi==0.4.2 +python-crontab==3.2.0 python-dateutil==2.9.0.post0 python-decouple==3.8 python-telegram-bot==22.0