Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysle committed Nov 23, 2024
1 parent c5b93c9 commit 6ca5158
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
21 changes: 21 additions & 0 deletions inyoka/portal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import gzip
import hashlib
import os
from datetime import datetime, timedelta

from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
Expand Down Expand Up @@ -189,6 +190,26 @@ def delete_list(cls, user_id, ids):
message.read = True if message.folder == trash else message.read
message.save()

@classmethod
@transaction.atomic
def clean_private_message_folders(cls):
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)
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)
privmsgs_trash.delete()
privmsgs_inbox_sent.delete()


def delete(self):
if self.folder == PRIVMSG_FOLDERS['trash'][0]:
self.folder = None
Expand Down
13 changes: 1 addition & 12 deletions inyoka/portal/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,5 @@ def clean_privmsg_folders():
after end of cache duration according to settings
has been reached.
"""
privmsgs_trash = PrivateMessageEntry.objects.filter(
folder="2",
message__pub_date__lte=datetime.now() - timedelta(
days=settings.PRIVATE_MESSAGE_TRASH_DURATION),
)
privmsgs_inbox_sent = PrivateMessageEntry.objects.filter(
folder__in=["0", "1"],
message__pub_date__lte=datetime.now() - timedelta(
days=settings.PRIVATE_MESSAGE_INBOX_SENT_DURATION),
)
logger.info("Deleting private messages after end of cache duration")
privmsgs_trash.delete()
privmsgs_inbox_sent.delete()
PrivateMessageEntry.clean_private_message_folders()
36 changes: 35 additions & 1 deletion tests/apps/portal/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
:license: BSD, see LICENSE for more details.
"""
import gzip
from datetime import datetime, timedelta
from os import path

from django.conf import settings
from django.contrib.auth.models import Group
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.test import TestCase

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


Expand Down Expand Up @@ -105,3 +108,34 @@ def test_generate_css__deletes_old_files(self):

self.assertFalse(path.exists(self.full_path))
self.assertTrue(path.exists(path.join(settings.MEDIA_ROOT, 'linkmap', self.css_file)))


class TestPrivateMessageEntry(TestCase):

def setUp(self):
super().setUp()

user = User.objects.register_user('testing', '[email protected]',
'pwd', False)
user.groups.add(Group.objects.get(name=settings.INYOKA_TEAM_GROUP_NAME))
self.other_user = User.objects.register_user(
'other_user',
'[email protected]',
'pwd', False)
pm = PrivateMessage(author=user, subject="Expired message", pub_date=datetime.now() -
timedelta(days=settings.PRIVATE_MESSAGE_INBOX_SENT_DURATION))
pm.send([self.other_user])

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())

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())

0 comments on commit 6ca5158

Please sign in to comment.