add loop to check every hour if a user need to be deleted after not logging in for 2 years

This commit is contained in:
2024-10-01 01:48:55 +02:00
parent 54da3fb923
commit 8aaa7deec2
4 changed files with 38 additions and 3 deletions

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/13 16:20:58 by tomoron #+# #+# #
# Updated: 2024/09/30 19:55:24 by tomoron ### ########.fr #
# Updated: 2024/10/01 00:46:17 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -461,7 +461,7 @@ class Game:
def saveResults(self):
try:
if(self.winner == None):
print("unkown winner, settings to 1")
print("unkown winner, setting to 1")
self.winner = 1
print("saving results")
p1DbUser = User.objects.get(id=self.p1.id)

View File

@ -0,0 +1,11 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# __init__.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/01 00:41:32 by tomoron #+# #+# #
# Updated: 2024/10/01 01:05:30 by tomoron ### ########.fr #
# #
# **************************************************************************** #

View File

@ -13,11 +13,27 @@ from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from django.core.asgi import get_asgi_application
from channels.sessions import SessionMiddlewareStack
from django.utils import timezone
from datetime import timedelta
from django.db import transaction
import threading
import time
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')
from .websocket import WebsocketHandler
def deleteLoop():
while(True):
from .models import User
time.sleep(60 * 60)
limit = timezone.now() - timedelta(days=2 * 365)
with transaction.atomic():
User.objects.using('second').filter(last_login__lt=limit).delete()
threading.Thread(target=deleteLoop, daemon=True).start()
django = get_asgi_application()
application = ProtocolTypeRouter({

View File

@ -81,6 +81,14 @@ DATABASES = {
"NAME":"VAR_DB_NAME",
"USER":"VAR_DB_USERNAME",
"PASSWORD":"VAR_DB_PASSWORD"
},
"second": {
"ENGINE": "django.db.backends.postgresql",
"HOST":"VAR_DB_HOST",
"PORT":5432,
"NAME":"VAR_DB_NAME",
"USER":"VAR_DB_USERNAME",
"PASSWORD":"VAR_DB_PASSWORD"
}
}
@ -132,7 +140,7 @@ STATIC_ROOT = BASE_DIR / 'staticfiles'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
ASGI_APPLICATION = 'server.asgi.applicatio'
ASGI_APPLICATION = 'server.asgi.application'
SESSION_SAVE_EVERY_REQUEST = True
SESSION_COOKIE_NAME = 'sessionid'
SESSION_COOKIE_SECURE = False