Purge orphaned one time snippets.

This commit is contained in:
Martin Mahner 2019-12-07 20:19:54 +01:00
parent 77abb94fc6
commit a8574613ab
2 changed files with 32 additions and 9 deletions

View file

@ -16,17 +16,18 @@ Changelog
``cleanup_snipppet`` managemenent command. It's still encouraged to have the ``cleanup_snipppet`` managemenent command. It's still encouraged to have the
management command setup, just run it daily, so snippets which expired but management command setup, just run it daily, so snippets which expired but
never got fetched by a client are deleted properly. never got fetched by a client are deleted properly.
- Onetime snippets which were never viewed a second time are now deleted if
they reach the default expire date.
- New AppConfig setting ``APPLICATION_NAME`` that can be used to replace the - New AppConfig setting ``APPLICATION_NAME`` that can be used to replace the
term "dpaste" throughout the UI. term "dpaste" throughout the UI.
- New AppConfig setting ``EXTRA_HEAD_HTML`` that can be used to add custom HTML - New AppConfig setting ``EXTRA_HEAD_HTML`` and similars that can be used to
to each template, specifically used for custom CSS styles, to easily override add custom HTML to each template, to easily override the stock UI of dpaste.
the stock UI of dpaste.
- New "Slim" view that displays the highlighted snippet without header, - New "Slim" view that displays the highlighted snippet without header,
options etc. options etc, and can be iframed.
- Forced line-break for superlongwordsthatwouldexceedthecanvas. - Forced line-break for superlongwordsthatwouldexceedthecanvas.
- Testsuite now uses pytest.
- Local development is no longer centered around ``pipenv`` and is rather using - Local development is no longer centered around ``pipenv`` and is rather using
docker-compose or the classic virtualenv based setups. docker-compose or the classic virtualenv based setups.
- Testsuite now uses pytest.
3.3.1 (2019-08-04): 3.3.1 (2019-08-04):
------------------- -------------------

View file

@ -1,8 +1,14 @@
from datetime import timedelta
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.utils import timezone from django.utils import timezone
from django.apps import apps
from dpaste.models import Snippet from dpaste.models import Snippet
config = apps.get_app_config("dpaste")
class Command(BaseCommand): class Command(BaseCommand):
help = "Purges snippets that are expired" help = "Purges snippets that are expired"
@ -16,19 +22,35 @@ class Command(BaseCommand):
), ),
def handle(self, *args, **options): def handle(self, *args, **options):
deleteable_snippets = Snippet.objects.filter( deleteable_snippets = (
# Snippets which are expired but haven't been deleted by
# the view.
Snippet.objects.filter(
expires__isnull=False, expires__isnull=False,
expire_type=Snippet.EXPIRE_TIME, expire_type=Snippet.EXPIRE_TIME,
expires__lte=timezone.now(), expires__lte=timezone.now(),
) )
# Snipoets which are Onetime snippets but have never been viewed
# the second time. Delete them if they are older than our default
# expiration.
| Snippet.objects.filter(
expire_type=Snippet.EXPIRE_ONETIME,
published__lte=(
timezone.now() - timedelta(seconds=config.EXPIRE_DEFAULT)
),
)
)
if len(deleteable_snippets) == 0: if len(deleteable_snippets) == 0:
self.stdout.write(u"No snippets to delete.") self.stdout.write(u"No snippets to delete.")
return None return None
self.stdout.write( self.stdout.write(
u"Will delete %s snippet(s):\n" % deleteable_snippets.count() u"Will delete %s snippet(s):\n" % deleteable_snippets.count()
) )
for d in deleteable_snippets: for d in deleteable_snippets:
self.stdout.write(u"- %s (%s)\n" % (d.secret_id, d.expires)) self.stdout.write(u"- %s (%s)\n" % (d.secret_id, d.expires))
if options.get("dry_run"): if options.get("dry_run"):
self.stdout.write("Dry run - Not actually deleting snippets!\n") self.stdout.write("Dry run - Not actually deleting snippets!\n")
else: else: