Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature(Conversations): Suppression des données de prise de contacts après 6 mois #1027

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions clevercloud/conversations_delete_outdated.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -l

# delete outdated conversations

# Do not run if this env var is not set:
if [[ -z "$CRON_CONVERSATIONS_DELETE_OUTDATED" ]]; then
echo "CRON_CONVERSATIONS_DELETE_OUTDATED not set. Exiting..."
exit 0
fi

# About clever cloud cronjobs:
# https://www.clever-cloud.com/doc/tools/crons/

if [[ "$INSTANCE_NUMBER" != "0" ]]; then
echo "Instance number is ${INSTANCE_NUMBER}. Stop here."
exit 0
fi

# $APP_HOME is set by default by clever cloud.
cd $APP_HOME

django-admin delete_outdated_conversations
3 changes: 2 additions & 1 deletion clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"15 0 * * * $ROOT/clevercloud/stats_export_user_download_list_to_file.sh",
"30 0 * * * $ROOT/clevercloud/stats_export_user_search_list_to_file.sh",
"0 1 * * * $ROOT/clevercloud/tenders_update_count_fields.sh",
"0 6 * * * $ROOT/clevercloud/conversations_delete_outdated.sh",
"0 7 * * 1 $ROOT/clevercloud/siaes_sync_with_emplois_inclusion.sh",
"5 7 * * 1 $ROOT/clevercloud/siaes_sync_c2_c4.sh",
"10 7 * * 1 $ROOT/clevercloud/siaes_update_api_entreprise_fields.sh",
Expand All @@ -18,4 +19,4 @@
"10 9 * * * $ROOT/clevercloud/tenders_send_siae_interested_reminder_emails.sh",
"20 9 * * * $ROOT/clevercloud/tenders_send_author_incremental.sh",
"*/5 8-15 * * 1-5 $ROOT/clevercloud/tenders_send_validated.sh"
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from lemarche.conversations.models import Conversation
from lemarche.utils.commands import BaseCommand


class Command(BaseCommand):
"""
Command to delete outdated conversations

Note: run via a CRON every day
Usage: python manage.py delete_outdated_conversations
"""

def handle(self, *args, **options):
self.stdout_info("Delete script of outdated conversations")
conversations_outdated = Conversation.objects.outdated()
self.stdout_info(f"Found {conversations_outdated.count()} outdated conversation(s) to delete")
deleted_count, _ = conversations_outdated.delete()
self.stdout_info(f"Deleted {deleted_count} outdated conversation(s)")
9 changes: 9 additions & 0 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
from uuid import uuid4

from django.conf import settings
Expand Down Expand Up @@ -33,6 +34,14 @@ def get_conv_from_uuid(self, conv_uuid: str, version=1):
else:
return self.get(Q(sender_encoded__endswith=conv_uuid) | Q(siae_encoded__endswith=conv_uuid))

def outdated(self):
"""the conversations must be deleted after six month
So we get all conversations outdated with this method
"""
# we use shortcut of 30 days x 6 month because timedelta doesn't accept months
six_months_ago = timezone.now() - timedelta(days=30 * 6)
return self.filter(created_at__lte=six_months_ago)


class Conversation(models.Model):
KIND_SEARCH = "SEARCH"
Expand Down
11 changes: 11 additions & 0 deletions lemarche/conversations/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import timedelta

from django.test import TestCase
from django.utils import timezone

from lemarche.conversations import constants as conversation_constants
from lemarche.conversations.factories import ConversationFactory
Expand Down Expand Up @@ -66,6 +69,14 @@ def test_with_answer_stats(self):
self.assertEqual(conversation_queryset.get(id=self.conversation.id).answer_count_annotated, 0)
self.assertEqual(conversation_queryset.get(id=self.conversation_with_answer.id).answer_count_annotated, 1)

def test_outdated(self):
one_year_ago = timezone.now() - timedelta(days=365)
ConversationFactory(created_at=one_year_ago)
five_weeks_ago = timezone.now() - timedelta(weeks=5)
ConversationFactory(created_at=five_weeks_ago)
self.assertEqual(Conversation.objects.all().count(), 2 + 2)
self.assertEqual(Conversation.objects.outdated().count(), 1)


class TemplateTransactionalModelTest(TestCase):
@classmethod
Expand Down