Snippets are deleted as soon as they exceed their expiration time.

It's no longer necessary to rely on the cleanup_snippets management command to do that.
This commit is contained in:
Martin Mahner 2019-12-07 07:13:22 +01:00
parent cdaf4f3f1c
commit cd586f62fe
4 changed files with 39 additions and 6 deletions

View file

@ -11,6 +11,11 @@ Changelog
- Dropped support for Django 2.1. ⚠️
- Added support for Python 3.8.
- Added support for Django 3.0.
- Snippets which are expired are now deleted as soon as they are requested
by a client. It's not necessary to purge them minutely with the
``cleanup_snipppet`` managemenent command. It's still encouraged to have the
management command setup, so snippets which expired but never got fetched
by a client are deleted properly.
- New AppConfig setting ``APPLICATION_NAME`` that can be used to replace the term
"dpaste" throughout the UI.
- New AppConfig setting ``EXTRA_HEAD_HTML`` that can be used to add custom HTML

View file

@ -9,8 +9,14 @@ Management Commands
Purge expired snippets
======================
dpaste ships with a management command ``cleanup_snippets`` that removes
expired snippets. To run it locally do:
Snippets are removed as soon as they exceed their expiration
date and get fetched by a client, however if they never get fetched this isn't
triggered. dpaste ships with a management command ``cleanup_snippets`` that
removes these expired snippets.
It's sufficient to run it daily.
To run it locally do:
.. code-block:: bash
@ -25,12 +31,11 @@ Options
Setup a Crontab
---------------
It's important that you setup a crontab or similar to remove expired snippets
as soon as they reach their expiration date. A crontab line might look like:
A crontab line might look like:
.. code-block:: bash
*/5 * * * * /srv/dpaste.de/pipenv run manage.py cleanup_snippets > /dev/null
1 20 * * * /srv/dpaste.de/pipenv run manage.py cleanup_snippets > /dev/null
.. note:: If you use the *database* session backend, you may also need to setup

View file

@ -199,6 +199,23 @@ class SnippetTestCase(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(Snippet.objects.count(), 1)
def test_snippet_is_removed_when_expired_and_as_soon_as_fetched(self):
data = self.valid_form_data()
self.client.post(self.new_url, data, follow=True)
# Set the expire time of the snippet to the past.
s = Snippet.objects.all()[0]
s.expires = s.expires - timedelta(days=30)
s.save()
# Next time its fetched its automatically deleted.
snippet_id = Snippet.objects.all()[0].secret_id
url = reverse('snippet_details', kwargs={'snippet_id': snippet_id})
response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 404)
self.assertEqual(Snippet.objects.count(), 0)
# -------------------------------------------------------------------------
# Snippet Functions
# -------------------------------------------------------------------------

View file

@ -12,6 +12,7 @@ from django.http import (
)
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import ugettext
from django.views.defaults import page_not_found as django_page_not_found
from django.views.defaults import server_error as django_server_error
@ -92,6 +93,9 @@ class SnippetDetailView(SnippetView, DetailView):
# One-Time snippet get deleted if the view count matches our limit
if (
snippet.expire_type == Snippet.EXPIRE_TIME
and snippet.expires < timezone.now()
) or (
snippet.expire_type == Snippet.EXPIRE_ONETIME
and snippet.view_count >= config.ONETIME_LIMIT
):
@ -163,7 +167,9 @@ class SnippetRawView(SnippetDetailView):
def dispatch(self, request, *args, **kwargs):
if not config.RAW_MODE_ENABLED:
return HttpResponseForbidden(
ugettext('This dpaste installation has Raw view mode disabled.')
ugettext(
'This dpaste installation has Raw view mode disabled.'
)
)
return super(SnippetRawView, self).dispatch(request, *args, **kwargs)