Skip to content

Commit

Permalink
#342 Use transaction.atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Jan 23, 2021
1 parent f9ba598 commit 96a0427
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import magic
from django.core.files.base import ContentFile
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction

from poleno.attachments.models import Attachment
from poleno.utils.misc import squeeze
Expand All @@ -14,10 +15,9 @@
class Command(BaseCommand):
args = u'attachment_id [file]'
help = squeeze(u"""
Creates anonymization for the specified Attachment. The content source is file, that can
be passed as an argument, or stdin. Preferred source is file. If file is not specified
and stdin is empty, the command will fail. Anonymization created this way will be marked
as successful. Only one successful anonymization can be assigned to the Attachment.
Creates anonymization for the specified Attachment. The anonymized content is read from
the given file or from stdin if no file is specified. If no file is specified and stdin
is empty, the command will fail.
""")

option_list = BaseCommand.option_list + (
Expand All @@ -33,15 +33,11 @@ class Command(BaseCommand):
),
make_option(u'--force',
action=u'store_true',
help=squeeze(u"""
The command refuses to anonymize attachment if a successful
anonymization already exists. This flag disables this check. Deletes all
existing successful anonymizations and creates new one. Unsuccessful
anonymizations will stay unaffected.
""")
help=u'Overwrite any existing anonymization for the attachment.'
),
)

@transaction.atomic
def handle(self, *args, **options):
if not args:
raise CommandError(u'attachment_anonymization takes at least 1 argument (0 given).')
Expand All @@ -61,10 +57,7 @@ def handle(self, *args, **options):
.filter(attachment=attachment)
.successful())
if not options[u'force'] and attachments_finalization:
raise CommandError(squeeze(u"""
Anonymization files already exist. Use the --force option to overwrite
them.
"""))
raise CommandError(u'Anonymization already exists. Use the --force to overwrite it.')

if len(args) == 2:
filename = args[1]
Expand All @@ -78,11 +71,12 @@ def handle(self, *args, **options):
if not content:
raise CommandError(u'No content given.')

attachments_finalization.delete()
AttachmentFinalization.objects.create(
attachment=attachment,
successful=True,
file=ContentFile(content),
content_type=options[u'content_type'] or magic.from_buffer(content, mime=True),
debug=options[u'debug'],
)
with transaction.atomic():
attachments_finalization.delete()
AttachmentFinalization.objects.create(
attachment=attachment,
successful=True,
file=ContentFile(content),
content_type=options[u'content_type'] or magic.from_buffer(content, mime=True),
debug=options[u'debug'],
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_attachment_argument_may_not_be_omitted(self):
with self.assertRaisesMessage(CommandError, u'attachment_anonymization takes at least 1 argument (0 given).'):
call_command(u'attachment_anonymization')

def test_non_existent_attachment_raises_exception(self):
def test_non_existing_attachment_raises_exception(self):
with self.assertRaisesMessage(CommandError, u'Attachment instance with pk "-1" does not exist.'):
call_command(u'attachment_anonymization', u'-1', self.filename)

Expand Down Expand Up @@ -74,7 +74,7 @@ def test_preferred_content_source_is_file(self):
attachment_finalization = AttachmentFinalization.objects.get(attachment=self.attachment)
self.assertEqual(attachment_finalization.file.read(), u'Default testing content')

def test_file_is_invalid(self):
def test_invalid_file_raises_exception(self):
filename = u'/tmp/invalid.txt'
with self.assertRaisesMessage(CommandError, u'Could not open file: '.format(filename)):
call_command(u'attachment_anonymization', self.attachment.pk, filename)
Expand Down Expand Up @@ -104,11 +104,12 @@ def test_force_option(self):
attachment_finalization1 = AttachmentFinalization.objects.get(attachment=self.attachment)
call_command(u'attachment_anonymization', self.attachment.pk, self.filename, force=True)
attachment_finalization2 = AttachmentFinalization.objects.get(attachment=self.attachment)
self.assertNotEqual(attachment_finalization1.pk, attachment_finalization2.pk)
with self.assertRaisesMessage(AttachmentFinalization.DoesNotExist, u'AttachmentFinalization matching query does not exist'):
AttachmentFinalization.objects.get(pk=attachment_finalization1.pk)

def test_existent_attachment_finalization_raises_exception_if_force_option_is_omitted(self):
def test_existing_attachment_finalization_raises_exception_if_force_option_is_omitted(self):
call_command(u'attachment_anonymization', self.attachment.pk, self.filename)
attachment_finalization = AttachmentFinalization.objects.get(attachment=self.attachment)
with self.assertRaisesMessage(CommandError, u'Anonymization files already exist. Use the --force option to overwrite them.'):
with self.assertRaisesMessage(CommandError, u'Anonymization already exists. Use the --force to overwrite it.'):
call_command(u'attachment_anonymization', self.attachment.pk, self.filename)
2 changes: 1 addition & 1 deletion misc/anonymization.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Computed Properties:


Creates AttachmentFinalization instance for the specified Attachment. The content source is file,
that can be passed as an argument, or stdin. Preferred source is file. If file is not specified and
that can be passed as an argument, or stdin. Preferred source is file. If no file is specified and
stdin is empty, the command will fail.

AttachmentFinalization created this way will be marked as successful. Only one successful
Expand Down

0 comments on commit 96a0427

Please sign in to comment.