Move retention policies enforcement code to the DocumentType model manager. Add corresponding tests (test_auto_trashing and test_auto_delete).
This commit is contained in:
@@ -24,72 +24,12 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
@app.task(ignore_result=True)
|
||||
def task_check_delete_periods():
|
||||
logger.info('Executing')
|
||||
|
||||
for document_type in DocumentType.objects.all():
|
||||
logger.info(
|
||||
'Checking deletion period of document type: %s', document_type
|
||||
)
|
||||
if document_type.delete_time_period and document_type.delete_time_unit:
|
||||
delta = timedelta(
|
||||
**{
|
||||
document_type.delete_time_unit: document_type.delete_time_period
|
||||
}
|
||||
)
|
||||
logger.info(
|
||||
'Document type: %s, has a deletion period delta of: %s',
|
||||
document_type, delta
|
||||
)
|
||||
for document in DeletedDocument.objects.filter(document_type=document_type):
|
||||
# TODO: Don't iterate, filter documents by expiration
|
||||
if now() > document.deleted_date_time + delta:
|
||||
logger.info(
|
||||
'Document "%s" with id: %d, trashed on: %s, exceded '
|
||||
'delete period', document, document.pk,
|
||||
document.deleted_date_time
|
||||
)
|
||||
document.delete()
|
||||
else:
|
||||
logger.info(
|
||||
'Document type: %s, has a no retention delta', document_type
|
||||
)
|
||||
|
||||
logger.info('Finshed')
|
||||
DocumentType.objects.check_delete_periods()
|
||||
|
||||
|
||||
@app.task(ignore_result=True)
|
||||
def task_check_trash_periods():
|
||||
logger.info('Executing')
|
||||
|
||||
for document_type in DocumentType.objects.all():
|
||||
logger.info(
|
||||
'Checking trash period of document type: %s', document_type
|
||||
)
|
||||
if document_type.trash_time_period and document_type.trash_time_unit:
|
||||
delta = timedelta(
|
||||
**{
|
||||
document_type.trash_time_unit: document_type.trash_time_period
|
||||
}
|
||||
)
|
||||
logger.info(
|
||||
'Document type: %s, has a trash period delta of: %s',
|
||||
document_type, delta
|
||||
)
|
||||
for document in Document.objects.filter(document_type=document_type):
|
||||
# TODO: Don't iterate, filter documents by expiration
|
||||
if now() > document.date_added + delta:
|
||||
logger.info(
|
||||
'Document "%s" with id: %d, added on: %s, exceded '
|
||||
'trash period', document, document.pk,
|
||||
document.date_added
|
||||
)
|
||||
document.delete()
|
||||
else:
|
||||
logger.info(
|
||||
'Document type: %s, has a no retention delta', document_type
|
||||
)
|
||||
|
||||
logger.info('Finshed')
|
||||
DocumentType.objects.check_trash_periods()
|
||||
|
||||
|
||||
@app.task(ignore_result=True)
|
||||
|
||||
Reference in New Issue
Block a user