SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 8a7aa9a8 authored by George Katsikas's avatar George Katsikas :goat:
Browse files

add command to delete expired sessions

fix #182
parent 2290156f
No related branches found
No related tags found
No related merge requests found
......@@ -20,4 +20,7 @@ python manage.py affiliatejournal_update_publications_from_Crossref --settings=S
python manage.py update_submission_fellowships --settings=SciPost_v1.settings.production_do1
# Update the users' groups
python manage.py update_user_permission_groups --settings=SciPost_v1.settings.production_do1
\ No newline at end of file
python manage.py update_user_permission_groups --settings=SciPost_v1.settings.production_do1
# Delete expired sessions (today's date implied)
python manage.py delete_expired_sessions --settings=SciPost_v1.settings.production_do1
\ No newline at end of file
......@@ -6,3 +6,7 @@ cd /home/scipost/SciPost/scipost_django
source ../venv3.11/bin/activate
python manage.py remind_fellows_to_submit_report
# Delete expired sessions (today's date implied)
python manage.py delete_expired_sessions --settings=SciPost_v1.settings.staging_do1
\ No newline at end of file
__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"
import re
from django.core.management.base import BaseCommand
from django.utils.timezone import datetime, now
class Command(BaseCommand):
help = "Delete expired sessions."
def add_arguments(self, parser):
parser.add_argument(
"--expiration_date",
type=str,
required=False,
default=now().strftime("%Y-%m-%d"),
help="Expiration date before which to delete sessions (format: YYYY-MM-DD)",
)
def handle(self, *args, **kwargs):
if re.match(r"^\d{4}-\d{2}-\d{2}$", kwargs["expiration_date"]):
expiration_date = datetime.strptime(
kwargs["expiration_date"], "%Y-%m-%d"
).astimezone()
self.delete_expired_sessions(expiration_date)
else:
raise ValueError(
"Invalid expiration date format. Please use the format YYYY-MM-DD."
)
def delete_expired_sessions(self, expiration_date: datetime):
from django.contrib.sessions.models import Session
sessions_to_delete = Session.objects.filter(expire_date__lt=expiration_date)
sessions_count = sessions_to_delete.count()
sessions_to_delete.delete()
self.stdout.write(
self.style.SUCCESS(
f"Successfully deleted {sessions_count} "
f"expired sessions before {expiration_date.date()}."
)
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment