Move stub deletion code from tasks.py to the manager of the Document model.
This commit is contained in:
@@ -7,6 +7,7 @@ from django.apps import apps
|
||||
from django.db import models
|
||||
from django.utils.timezone import now
|
||||
|
||||
from .literals import STUB_EXPIRATION_INTERVAL
|
||||
from .settings import setting_recent_count
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -105,6 +106,10 @@ class DocumentTypeManager(models.Manager):
|
||||
|
||||
|
||||
class DocumentManager(models.Manager):
|
||||
def delete_stubs(self):
|
||||
for stale_stub_document in self.filter(is_stub=True, date_added__lt=now() - timedelta(seconds=STUB_EXPIRATION_INTERVAL)):
|
||||
stale_stub_document.delete(trash=False)
|
||||
|
||||
def get_by_natural_key(self, uuid):
|
||||
return self.get(uuid=uuid)
|
||||
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import OperationalError
|
||||
from django.utils.timezone import now
|
||||
|
||||
from mayan.celery import app
|
||||
|
||||
@@ -13,7 +11,7 @@ from common.models import SharedUploadedFile
|
||||
|
||||
from .literals import (
|
||||
UPDATE_PAGE_COUNT_RETRY_DELAY, UPLOAD_NEW_VERSION_RETRY_DELAY,
|
||||
NEW_DOCUMENT_RETRY_DELAY, STUB_EXPIRATION_INTERVAL
|
||||
NEW_DOCUMENT_RETRY_DELAY
|
||||
)
|
||||
from .models import Document, DocumentPage, DocumentType, DocumentVersion
|
||||
|
||||
@@ -40,10 +38,7 @@ def task_clear_image_cache():
|
||||
@app.task(ignore_result=True)
|
||||
def task_delete_stubs():
|
||||
logger.info('Executing')
|
||||
|
||||
for stale_stub_document in Document.objects.filter(is_stub=True, date_added__lt=now() - timedelta(seconds=STUB_EXPIRATION_INTERVAL)):
|
||||
stale_stub_document.delete(trash=False)
|
||||
|
||||
Document.objects.delete_stubs()
|
||||
logger.info('Finshed')
|
||||
|
||||
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import timedelta
|
||||
import time
|
||||
|
||||
from django.core.files import File
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from ..literals import STUB_EXPIRATION_INTERVAL
|
||||
from ..models import DeletedDocument, Document, DocumentType
|
||||
|
||||
from .literals import (
|
||||
TEST_DOCUMENT_TYPE, TEST_DOCUMENT_PATH, TEST_MULTI_PAGE_TIFF_PATH,
|
||||
TEST_OFFICE_DOCUMENT_PATH, TEST_SMALL_DOCUMENT_PATH
|
||||
)
|
||||
from ..models import DeletedDocument, Document, DocumentType
|
||||
|
||||
|
||||
@override_settings(OCR_AUTO_OCR=False)
|
||||
@@ -225,3 +228,30 @@ class DocumentVersionTestCase(TestCase):
|
||||
self.document.versions.first().revert()
|
||||
|
||||
self.assertEqual(self.document.versions.count(), 1)
|
||||
|
||||
|
||||
@override_settings(OCR_AUTO_OCR=False)
|
||||
class DocumentManagerTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.document_type = DocumentType.objects.create(
|
||||
label=TEST_DOCUMENT_TYPE
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
self.document_type.delete()
|
||||
|
||||
def test_document_stubs_deletion(self):
|
||||
document_stub = Document.objects.create(
|
||||
document_type=self.document_type
|
||||
)
|
||||
|
||||
Document.objects.delete_stubs()
|
||||
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
|
||||
document_stub.date_added = document_stub.date_added - timedelta(seconds=STUB_EXPIRATION_INTERVAL + 1)
|
||||
document_stub.save()
|
||||
|
||||
Document.objects.delete_stubs()
|
||||
|
||||
self.assertEqual(Document.objects.count(), 0)
|
||||
|
||||
Reference in New Issue
Block a user