def image_url(attachment, suffix): '''Return url of an image given size(@param suffix) e.g large, medium, small, or generate required thumbnail ''' url = attachment.media_file.url if suffix == 'original': return url else: default_storage = get_storage_class()() fs = get_storage_class('django.core.files.storage.FileSystemStorage')() if suffix in settings.THUMB_CONF: size = settings.THUMB_CONF[suffix]['suffix'] filename = attachment.media_file.name if default_storage.exists(filename): if default_storage.exists(get_path(filename, size)) and\ default_storage.size(get_path(filename, size)) > 0: url = default_storage.url(get_path(filename, size)) else: if default_storage.__class__ != fs.__class__: resize(filename) else: resize_local_env(filename) return image_url(attachment, suffix) else: return None return url
def image_url(attachment, suffix): '''Return url of an image given size(@param suffix) e.g large, medium, small, or generate required thumbnail ''' url = attachment.media_file.url if suffix == 'original': return url else: default_storage = get_storage_class()() fs = get_storage_class('django.core.files.storage.FileSystemStorage')() if settings.THUMB_CONF.has_key(suffix): size = settings.THUMB_CONF[suffix]['suffix'] filename = attachment.media_file.name if default_storage.exists(filename): if default_storage.exists(get_path(filename, size)): url = default_storage.url( get_path(filename, size)) else: if default_storage.__class__ != fs.__class__: resize(filename) else: resize_local_env(filename) return image_url(attachment, suffix) else: return None return url
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) 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 kwargs.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) else: resize_local_env(filename) if default_storage.exists( get_path( filename, '%s' % settings.THUMB_CONF['small']['suffix'])): print( _(u'Thumbnails created for %(file)s') % {'file': filename}) else: print( _(u'Problem with the file %(file)s') % {'file': filename}) except (IOError, OSError), e: print _(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) 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 kwargs.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) else: resize_local_env(filename) if default_storage.exists(get_path( filename, '%s' % settings.THUMB_CONF['small']['suffix'])): print (_(u'Thumbnails created for %(file)s') % {'file': filename}) else: print (_(u'Problem with the file %(file)s') % {'file': filename}) except (IOError, OSError), e: print _(u'Error on %(filename)s: %(error)s') \ % {'filename': filename, 'error': e}
def _save_thumbnails(image, path, size, suffix, filename=None): # If filename is present, resize on s3 fs if filename: default_storage = get_storage_class()() fs = get_storage_class('django.core.files.storage.FileSystemStorage')() image.thumbnail(get_dimensions(image.size, size), Image.ANTIALIAS) image.save(get_path(path, suffix)) default_storage.save(get_path(filename, suffix), fs.open(get_path(path, suffix))) else: image.thumbnail(get_dimensions(image.size, size), Image.ANTIALIAS) image.save(get_path(path, suffix))
def _save_thumbnails(image, path, size, suffix, filename=None): # If filename is present, resize on s3 fs if filename: default_storage = get_storage_class()() fs = get_storage_class('django.core.files.storage.FileSystemStorage')() # Ensure conversion to float in operations image.thumbnail(get_dimensions(image.size, float(size)), Image.ANTIALIAS) image.save(get_path(path, suffix)) default_storage.save(get_path(filename, suffix), fs.open(get_path(path, suffix))) else: try: image.thumbnail(get_dimensions(image.size, size), Image.ANTIALIAS) except ZeroDivisionError: pass image.save(get_path(path, suffix))
def handle(self, *args, **kwargs): fs = get_storage_class('django.core.files.storage.FileSystemStorage')() for att in queryset_iterator(Attachment.objects.select_related( 'instance', 'instance__xform').all()): filename = att.media_file.name default_storage = get_storage_class()() if not default_storage.exists(get_path(filename, settings.THUMB_CONF['smaller']['suffix'])): try: if default_storage.__class__ != fs.__class__: resize(filename) else: resize_local_env(filename) if default_storage.exists(get_path(filename, settings.THUMB_CONF['smaller']['suffix'])): print 'Thumbnails created for %s' % filename else: print 'Something didn\'t go right for %s' % filename except (IOError, OSError), e: print 'Error on %s: %s' % (filename, e)
def _save_thumbnails(image, path, size, suffix): nm = NamedTemporaryFile(suffix='.%s' % settings.IMG_FILE_TYPE) default_storage = get_storage_class()() try: # Ensure conversion to float in operations image.thumbnail(get_dimensions(image.size, float(size)), Image.ANTIALIAS) except ZeroDivisionError: pass image.save(nm.name) default_storage.save(get_path(path, suffix), ContentFile(nm.read())) nm.close()
def handle(self, *args, **kwargs): fs = get_storage_class('django.core.files.storage.FileSystemStorage')() for att in queryset_iterator(Attachment.objects.select_related( 'instance', 'instance__xform').all()): filename = att.media_file.name default_storage = get_storage_class()() if not default_storage.exists(get_path(filename, settings.THUMB_CONF['smaller']['suffix'])): try: if default_storage.__class__ != fs.__class__: resize(filename) else: resize_local_env(filename) if default_storage.exists(get_path(filename, '%s.%s' % (settings.THUMB_CONF['smaller']['suffix'], settings.IMG_FILE_TYPE))): print _(u'Thumbnails created for %s') % filename else: print _(u'Problem with the file %s') % filename except (IOError, OSError), e: print _(u'Error on %(filename)s: %(error)s') \ % {'filename': filename, 'error': e}
def _save_thumbnails(image, path, size, suffix): nm = NamedTemporaryFile(suffix='.%s' % settings.IMG_FILE_TYPE) default_storage = get_storage_class()() try: # Ensure conversion to float in operations image.thumbnail( get_dimensions(image.size, float(size)), Image.ANTIALIAS) except ZeroDivisionError: pass image.save(nm.name) default_storage.save( get_path(path, suffix), ContentFile(nm.read())) nm.close()
def handle(self, *args, **kwargs): fs = get_storage_class('django.core.files.storage.FileSystemStorage')() for att in queryset_iterator( Attachment.objects.select_related('instance', 'instance__xform').all()): filename = att.media_file.name default_storage = get_storage_class()() if not default_storage.exists( get_path(filename, settings.THUMB_CONF['smaller']['suffix'])): try: if default_storage.__class__ != fs.__class__: resize(filename) else: resize_local_env(filename) if default_storage.exists( get_path( filename, settings.THUMB_CONF['smaller']['suffix'])): print 'Thumbnails created for %s' % filename else: print 'Something didn\'t go right for %s' % filename except (IOError, OSError), e: print 'Error on %s: %s' % (filename, e)
def _save_thumbnails(image, path, size, suffix): ext = path.split('.')[-1] lsfx = { 'jp2': ext, 'png': ext, 'bmp': ext, 'gif': ext, 'tiff': ext, 'jpeg': 'jpg', 'jpg': ext, 'tiff': ext }.get(ext.lower(), settings.IMG_FILE_TYPE) nm = NamedTemporaryFile(suffix='.%s' % lsfx) default_storage = get_storage_class()() try: # Ensure conversion to float in operations image.thumbnail(get_dimensions(image.size, float(size)), Image.ANTIALIAS) except ZeroDivisionError: pass image.save(nm.name) default_storage.save(get_path(path, suffix), ContentFile(nm.read())) nm.close()