Skip to content

Commit

Permalink
Address review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysle committed Nov 28, 2024
1 parent 5aa5c42 commit 81eeddf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
9 changes: 7 additions & 2 deletions inyoka/portal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,24 @@ def delete_list(cls, user_id, ids):
@classmethod
@transaction.atomic
def clean_private_message_folders(cls):
"""
Remove all messages in private message folders (except 'archive')
after end of cache duration according to settings
has been reached.
"""
sent = PRIVMSG_FOLDERS['sent'][0]
inbox = PRIVMSG_FOLDERS['inbox'][0]
trash = PRIVMSG_FOLDERS['trash'][0]
privmsgs_trash = PrivateMessageEntry.objects.filter(
folder=trash,
message__pub_date__lte=datetime.now() - timedelta(
days=settings.PRIVATE_MESSAGE_TRASH_DURATION),
).exclude(user__groups__name__iexact=settings.INYOKA_TEAM_GROUP_NAME)
).exclude(user__groups__name__exact=settings.INYOKA_TEAM_GROUP_NAME)
privmsgs_inbox_sent = PrivateMessageEntry.objects.filter(
folder__in=[inbox, sent],
message__pub_date__lte=datetime.now() - timedelta(
days=settings.PRIVATE_MESSAGE_INBOX_SENT_DURATION),
).exclude(user__groups__name__iexact=settings.INYOKA_TEAM_GROUP_NAME)
).exclude(user__groups__name__exact=settings.INYOKA_TEAM_GROUP_NAME)
privmsgs_trash.delete()
privmsgs_inbox_sent.delete()

Expand Down
6 changes: 1 addition & 5 deletions inyoka/portal/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ def query_counter_task(cache_key, sql):

@shared_task
def clean_privmsg_folders():
"""
Remove all messages in private message folders (except 'archive')
after end of cache duration according to settings
has been reached.
"""
"""Clean private message folders."""
logger.info("Deleting private messages after end of cache duration")
PrivateMessageEntry.clean_private_message_folders()
13 changes: 9 additions & 4 deletions tests/apps/portal/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from django.db import IntegrityError
from django.test import TestCase

from inyoka.portal.models import Linkmap, PrivateMessage, PrivateMessageEntry
from inyoka.portal.models import Linkmap, PrivateMessage, PrivateMessageEntry, PRIVMSG_FOLDERS
from inyoka.portal.user import User
from inyoka.utils.urls import href

Expand Down Expand Up @@ -126,16 +126,21 @@ def setUp(self):
timedelta(days=settings.PRIVATE_MESSAGE_INBOX_SENT_DURATION))
pm.send([self.other_user])

self.trash = PRIVMSG_FOLDERS["trash"][0]
self.inbox = PRIVMSG_FOLDERS["inbox"][0]
self.sent = PRIVMSG_FOLDERS["sent"][0]

self.privmsgentry = PrivateMessageEntry.objects.get(message=pm, user=self.other_user)

def test_delete_messages(self):
self.assertEqual(self.privmsgentry.message.subject, 'Expired message')
PrivateMessageEntry.clean_private_message_folders()
self.assertFalse(PrivateMessageEntry.objects.filter(folder=1).exists())
self.assertTrue(PrivateMessageEntry.objects.filter(folder=0).exists())
self.assertFalse(PrivateMessageEntry.objects.filter(folder=self.inbox).exists())
# Team members are spared
self.assertTrue(PrivateMessageEntry.objects.filter(folder=self.sent).exists())

def test_delete_archived_messages(self):
self.privmsgentry.archive()
self.assertTrue(self.privmsgentry.in_archive)
PrivateMessageEntry.clean_private_message_folders()
self.assertTrue(PrivateMessageEntry.objects.filter(folder=3, user=self.other_user).exists())
self.assertTrue(PrivateMessageEntry.objects.filter(folder=self.trash, user=self.other_user).exists())

0 comments on commit 81eeddf

Please sign in to comment.