forked from mmmaly/chcemvediet
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#342 Create attachment_anonymization management command
- Loading branch information
1 parent
582eb0a
commit 6c86e80
Showing
7 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
65 changes: 65 additions & 0 deletions
65
chcemvediet/apps/anonymization/management/commands/attachment_anonymization.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from optparse import make_option | ||
|
||
import magic | ||
from django.core.files.base import ContentFile | ||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from poleno.attachments.models import Attachment | ||
from chcemvediet.apps.anonymization.models import AttachmentFinalization | ||
from chcemvediet.apps.inforequests.models import Action | ||
|
||
|
||
class Command(BaseCommand): | ||
help = u'Creates AttachmentFinalization for the specified Attachment.' | ||
|
||
option_list = BaseCommand.option_list + ( | ||
make_option(u'-f', u'--file', | ||
dest=u'filename', | ||
help=u'define file path'), | ||
make_option(u'--content_type', | ||
help=u'define content type of file', | ||
), | ||
make_option(u'--debug', | ||
default=u'', | ||
help=u'add debug message' | ||
), | ||
make_option(u'--force', | ||
action=u'store_true', | ||
help=u'overwrite an existing successful AttachmentFinalization' | ||
), | ||
) | ||
|
||
def usage(self, subcommand=None): | ||
return u'manage.py attachment_anonymization attachment [options] [file]' | ||
|
||
def handle(self, *args, **options): | ||
try: | ||
filename = options[u'filename'] or args[1] | ||
except IndexError: | ||
self.stdout.write(u'Usage: {}'.format(self.usage())) | ||
self.stdout.write( | ||
u'Try "manage.py attachment_anonymization --help" for more information.') | ||
return 1 | ||
|
||
with open(filename, u'rb') as file: | ||
try: | ||
attachment = (Attachment.objects | ||
.attached_to(Action) | ||
.not_normalized() | ||
.get(pk=int(args[0]))) | ||
attachments_finalization = (AttachmentFinalization.objects | ||
.filter(attachment=attachment) | ||
.successful()) | ||
if options[u'force'] or not attachments_finalization: | ||
attachments_finalization.delete() | ||
content = file.read() | ||
content_type = magic.from_buffer(content, mime=True) | ||
AttachmentFinalization.objects.create( | ||
attachment=attachment, | ||
successful=True, | ||
file=ContentFile(content), | ||
content_type=options[u'content_type'] or content_type, | ||
debug=options[u'debug'], | ||
) | ||
except Attachment.DoesNotExist: | ||
raise CommandError(u'Attachment {} does not exist'.format(args[0])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
71 changes: 71 additions & 0 deletions
71
chcemvediet/apps/anonymization/tests/test_management_commands.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import os | ||
from testfixtures import TempDirectory | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.test.utils import override_settings | ||
|
||
from chcemvediet.apps.anonymization.models import AttachmentFinalization | ||
from chcemvediet.tests import ChcemvedietTestCaseMixin | ||
|
||
|
||
class AttachmentAnonymizationManagementCommandTest(ChcemvedietTestCaseMixin, TestCase): | ||
|
||
def _pre_setup(self): | ||
super(AttachmentAnonymizationManagementCommandTest, self)._pre_setup() | ||
self.tempdir = TempDirectory() | ||
self.settings_override = override_settings( | ||
MEDIA_ROOT=self.tempdir.path, | ||
) | ||
self.settings_override.enable() | ||
self.attachment = self._create_attachment() | ||
|
||
def _post_teardown(self): | ||
self.settings_override.disable() | ||
self.tempdir.cleanup() | ||
super(AttachmentAnonymizationManagementCommandTest, self)._post_teardown() | ||
|
||
def _create_attachment(self, **kwargs): | ||
return super(AttachmentAnonymizationManagementCommandTest, self)._create_attachment( | ||
generic_object=self.action, | ||
**kwargs | ||
) | ||
|
||
|
||
def test_attachment_anonymization_command_with_valid_arguments_creates_attachment_finalization(self): | ||
with open(os.path.join(self.tempdir.path, u'myfile.txt'), u'w') as file: | ||
call_command(u'attachment_anonymization', self.attachment.pk, file.name) | ||
AttachmentFinalization.objects.get(attachment=self.attachment) | ||
|
||
def test_attachment_anonymization_command_file_option(self): | ||
with open(os.path.join(self.tempdir.path, u'myfile.txt'), u'w') as file: | ||
call_command(u'attachment_anonymization', self.attachment.pk, filename=file.name) | ||
AttachmentFinalization.objects.get(attachment=self.attachment) | ||
|
||
def test_attachment_anonymization_command_content_type_option(self): | ||
with open(os.path.join(self.tempdir.path, u'myfile.txt'), u'w') as file: | ||
call_command(u'attachment_anonymization', self.attachment.pk, file.name, content_type=u'type') | ||
attachment_finalization = AttachmentFinalization.objects.get(attachment=self.attachment) | ||
self.assertEqual(attachment_finalization.content_type, u'type') | ||
|
||
def test_attachment_anonymization_command_debug_option(self): | ||
with open(os.path.join(self.tempdir.path, u'myfile.txt'), u'w') as file: | ||
call_command(u'attachment_anonymization', self.attachment.pk, file.name, debug=u'debug') | ||
attachment_finalization = AttachmentFinalization.objects.get(attachment=self.attachment) | ||
self.assertEqual(attachment_finalization.debug, u'debug') | ||
|
||
def test_attachment_anonymization_command_with_existing_attachment_finalization(self): | ||
with open(os.path.join(self.tempdir.path, u'myfile.txt'), u'w') as file: | ||
call_command(u'attachment_anonymization', self.attachment.pk, file.name) | ||
attachment_finalization1 = AttachmentFinalization.objects.get(attachment=self.attachment) | ||
call_command(u'attachment_anonymization', self.attachment.pk, file.name) | ||
attachment_finalization2 = AttachmentFinalization.objects.get(attachment=self.attachment) | ||
self.assertEqual(attachment_finalization1.pk, attachment_finalization2.pk) | ||
|
||
def test_attachment_anonymization_command_force_option(self): | ||
with open(os.path.join(self.tempdir.path, u'myfile.txt'), u'w') as file: | ||
call_command(u'attachment_anonymization', self.attachment.pk, file.name) | ||
attachment_finalization1 = AttachmentFinalization.objects.get(attachment=self.attachment) | ||
call_command(u'attachment_anonymization', self.attachment.pk, file.name, force=True) | ||
attachment_finalization2 = AttachmentFinalization.objects.get(attachment=self.attachment) | ||
self.assertNotEqual(attachment_finalization1.pk, attachment_finalization2.pk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters