Fixed #107 and fixed #125 -- Periodic cleanup tasks

This commit is contained in:
Raphael Michel
2016-03-26 19:10:31 +01:00
parent 6e60332948
commit 1011893543
8 changed files with 59 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ class PretixBaseConfig(AppConfig):
def ready(self):
from . import exporter # NOQA
from . import payment # NOQA
from .services import export, mail, tickets, cart, orders # NOQA
from .services import export, mail, tickets, cart, orders, cleanup # NOQA
try:
from .celery import app as celery_app # NOQA

View File

View File

@@ -0,0 +1,12 @@
from django.core.management import call_command
from django.core.management.base import BaseCommand
from ...signals import periodic_task
class Command(BaseCommand):
help = "Run periodic tasks"
def handle(self, *args, **options):
periodic_task.send(self)
call_command('clearsessions')

View File

@@ -299,6 +299,11 @@ class QuestionAnswer(models.Model):
else:
return self.answer
def save(self, *args, **kwargs):
if self.orderposition and self.cartposition:
raise ValueError('QuestionAnswer cannot be linked to an order and a cart position at the same time.')
super().save(*args, **kwargs)
class AbstractPosition(models.Model):
"""

View File

@@ -0,0 +1,21 @@
from datetime import timedelta
from django.dispatch import receiver
from django.utils.timezone import now
from ..models import CachedFile, CartPosition, InvoiceAddress
from ..signals import periodic_task
@receiver(signal=periodic_task)
def clean_cart_positions(sender, **kwargs):
for cp in CartPosition.objects.filter(expires__lt=now() - timedelta(days=14)):
cp.delete()
for ia in InvoiceAddress.objects.filter(order__isnull=True, last_modified__lt=now() - timedelta(days=14)):
ia.delete()
@receiver(signal=periodic_task)
def clean_cached_files(sender, **kwargs):
for cf in CachedFile.objects.filter(expires__isnull=False, expires__lt=now()):
cf.delete()

View File

@@ -95,3 +95,13 @@ don't know how to turn it into human-readable text.
logentry_display = EventPluginSignal(
providing_args=["logentry"]
)
"""
This is a regular django signal (no pretix event signal) that we send out every
time the periodic task cronjob runs. This interval is not sharply defined, it can
be everything between a minute and a day. The actions you perform should be
idempotent, i.e. it should not make a difference if this is send out more often
than expected.
"""
periodic_task = django.dispatch.Signal()