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

Remove private messages after specific cache duration #1333

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions inyoka/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
:license: BSD, see LICENSE for more details.
"""
from collections import OrderedDict

import jinja2
from datetime import timedelta
from os.path import dirname, join

from celery.schedules import crontab

import inyoka
import jinja2

gettext_noop = lambda x: x

Expand Down Expand Up @@ -159,6 +158,11 @@
# days to describe an inactive user
USER_INACTIVE_DAYS = 365

# days after private messages
# in specific folder will be removed
PRIVATE_MESSAGE_TRASH_DURATION = 90
PRIVATE_MESSAGE_INBOX_SENT_DURATION = 180

# wiki settings
WIKI_MAIN_PAGE = 'Welcome'

Expand Down Expand Up @@ -315,6 +319,10 @@
'render_all_wiki_pages': {
'task': 'inyoka.wiki.tasks.render_all_pages',
'schedule': crontab(hour=23, minute=5),
},
'clean_private_message_folders': {
'task': 'inyoka.portal.tasks.clean_privmsg_folders',
'schedule': crontab(hour=6, minute=30, day_of_week='saturday'),
}
}

Expand Down
23 changes: 23 additions & 0 deletions inyoka/portal/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.core.cache import cache
from django.db import connection

from inyoka.portal.models import PrivateMessageEntry
from inyoka.portal.user import User
from inyoka.utils.logger import logger
from inyoka.utils.storage import storage
Expand Down Expand Up @@ -78,3 +79,25 @@ def query_counter_task(cache_key, sql):
cursor = connection.cursor()
cursor.execute(sql)
cache.set(cache_key, cursor.fetchone()[0])


@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.
"""
privmsgs_trash = PrivateMessageEntry.objects.filter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I's better to move logic out of @shared_task, to for example two methods in the model manger. So the shared task would only call these two (plus the logger)

This will make it possible to add some basic tests (for example: are only old messages are deleted? are messages in archive are kept? ...)

folder="2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would suggest to remove magic numbers (and instead use a constant in the model f.e.)

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