def test_resize_exception_is_handled(self): with HTTMock(image_url_mock): with self.assertRaises(Exception) as io_error: resize('test.jpg', 'jpg') self.assertEqual(str(io_error.exception), u'The image file couldn\'t be identified')
def test_resize_exception_is_handled(self): with HTTMock(image_url_mock): with self.assertRaises(Exception) as io_error: resize('test.jpg') self.assertEqual(io_error.exception.message, u'The image file couldn\'t be identified')
def handle(self, *args, **options): attachments_qs = Attachment.objects.select_related( 'instance', 'instance__xform') if options.get('username'): username = options.get('username') try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandError( "Error: username %(username)s does not exist" % {'username': username}) attachments_qs = attachments_qs.filter(instance__user=user) if options.get('id_string'): id_string = options.get('id_string') try: xform = XForm.objects.get(id_string=id_string) except XForm.DoesNotExist: raise CommandError( "Error: Form with id_string %(id_string)s does not exist" % {'id_string': id_string}) attachments_qs = attachments_qs.filter(instance__xform=xform) fs = get_storage_class('django.core.files.storage.FileSystemStorage')() for att in queryset_iterator(attachments_qs): filename = att.media_file.name default_storage = get_storage_class()() full_path = get_path(filename, settings.THUMB_CONF['small']['suffix']) if options.get('force') is not None: for s in ['small', 'medium', 'large']: fp = get_path(filename, settings.THUMB_CONF[s]['suffix']) if default_storage.exists(fp): default_storage.delete(fp) if not default_storage.exists(full_path): try: if default_storage.__class__ != fs.__class__: resize(filename, att.extension) else: resize_local_env(filename, att.extension) path = get_path(filename, '%s' % THUMB_CONF['small']['suffix']) if default_storage.exists(path): self.stdout.write( _(u'Thumbnails created for %(file)s') % {'file': filename}) else: self.stdout.write( _(u'Problem with the file %(file)s') % {'file': filename}) except (IOError, OSError) as e: self.stderr.write( _(u'Error on %(filename)s: %(error)s') % { 'filename': filename, 'error': e })
def handle(self, *args, **kwargs): attachments_qs = Attachment.objects.select_related( 'instance', 'instance__xform') if kwargs.get('username'): username = kwargs.get('username') try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandError( "Error: username %(username)s does not exist" % {'username': username}) attachments_qs = attachments_qs.filter(instance__user=user) if kwargs.get('id_string'): id_string = kwargs.get('id_string') try: xform = XForm.objects.get(id_string=id_string) except XForm.DoesNotExist: raise CommandError( "Error: Form with id_string %(id_string)s does not exist" % {'id_string': id_string}) attachments_qs = attachments_qs.filter(instance__xform=xform) for att in queryset_iterator(attachments_qs): filename = att.media_file.name default_storage = get_storage_class()() full_path = get_path(filename, settings.THUMB_CONF['small']['suffix']) if kwargs.get('force') is not None: for s in settings.THUMB_CONF.keys(): fp = get_path(filename, settings.THUMB_CONF[s]['suffix']) if default_storage.exists(fp): default_storage.delete(fp) if not default_storage.exists(full_path): try: resize(filename) if default_storage.exists( get_path( filename, '%s' % settings.THUMB_CONF['small']['suffix'])): print( _('Thumbnails created for %(file)s') % {'file': filename}) else: print( _('Problem with the file %(file)s') % {'file': filename}) except (IOError, OSError), e: print( _('Error on %(filename)s: %(error)s') % { 'filename': filename, 'error': e })
def handle(self, *args, **options): attachments_qs = Attachment.objects.select_related( 'instance', 'instance__xform') if options.get('username'): username = options.get('username') try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandError( "Error: username %(username)s does not exist" % {'username': username}) attachments_qs = attachments_qs.filter(instance__user=user) if options.get('id_string'): id_string = options.get('id_string') try: xform = XForm.objects.get(id_string=id_string) except XForm.DoesNotExist: raise CommandError( "Error: Form with id_string %(id_string)s does not exist" % {'id_string': id_string}) attachments_qs = attachments_qs.filter(instance__xform=xform) fs = get_storage_class('django.core.files.storage.FileSystemStorage')() for att in queryset_iterator(attachments_qs): filename = att.media_file.name default_storage = get_storage_class()() full_path = get_path(filename, settings.THUMB_CONF['small']['suffix']) if options.get('force') is not None: for s in ['small', 'medium', 'large']: fp = get_path(filename, settings.THUMB_CONF[s]['suffix']) if default_storage.exists(fp): default_storage.delete(fp) if not default_storage.exists(full_path): try: if default_storage.__class__ != fs.__class__: resize(filename, att.extension) else: resize_local_env(filename, att.extension) path = get_path( filename, '%s' % THUMB_CONF['small']['suffix']) if default_storage.exists(path): self.stdout.write( _(u'Thumbnails created for %(file)s') % {'file': filename}) else: self.stdout.write( _(u'Problem with the file %(file)s') % {'file': filename}) except (IOError, OSError) as e: self.stderr.write(_( u'Error on %(filename)s: %(error)s') % {'filename': filename, 'error': e})
def test_resize(self): self._publish_transportation_form() self._submit_transport_instance_w_attachment() attachment = Attachment.objects.first() media_filename = attachment.media_file.name resize(media_filename, attachment.extension) # small path = os.path.join(storage.path(''), media_filename[0:-4] + '-small.jpg') assert os.path.exists(path) # medium path = os.path.join(storage.path(''), media_filename[0:-4] + '-medium.jpg') assert os.path.exists(path) # large path = os.path.join(storage.path(''), media_filename[0:-4] + '-large.jpg') assert os.path.exists(path)