def clean(self): files = [] errors = [] images = 'images' in self.cleaned_data and self.cleaned_data[ 'images'] or [] for f in images: v = parse_image_name(f.name) data = f.read() size = get_image_size_from_handle(io.BytesIO(data), len(data)) if size: files.append({ 'name': f.name, 'data': data, 'width': size[0], 'height': size[1], 'index': v['index'], 'format': v['format'] }) else: errors.append( ValidationError("File '%(fn)s' neither a Jpeg nor a PNG", params={'fn': f.name}, code='unknown-file-format')) if errors: raise ValidationError(errors) self.cleaned_data['files'] = files return self.cleaned_data
def validate_file_image(f): if not parse_image_name(f.name): raise ValidationError( 'File %(fn)s is not called image<nn> or refl-image', params={'fn': f.name}, code='bad-image-file-name') if IMAGE_UPLOAD_SIZE_LIMIT < f.size: raise ValidationError("File '%(fn)s' is too large", params={'fn': f.name}, code='file-too-large')
def copyimages(src, grain): for n in sorted(os.listdir(src)): v = parse_image_name(n) srcname = os.path.join(src, n) if os.path.isfile(srcname) and v != None: with open(srcname, mode='rb') as f: Image(grain=grain, format=v['format'], ft_type='S', index=v['index'], data=f.read()).save()
def do_create(self, request, grain_id): grain = Grain.objects.get(pk=grain_id) if (not request.user.is_superuser and grain.get_owner() != request.user): raise exceptions.PermissionDenied filename = self.initial_data['data'].name info = parse_image_name(filename) data = self.initial_data['data'].read() self.save(index=info['index'], grain=grain, format=info['format'], ft_type=info['ft_type'], data=data)
def perform_update(self, serializer): kwargs = {} if 'grain' in serializer.initial_data: grain = Grain.objects.get(pk=serializer.initial_data['grain']) kwargs['grain'] = grain if (not self.request.user.is_superuser and grain.get_owner() != self.request.user): raise exceptions.PermissionDenied if 'data' in serializer.initial_data: filename = serializer.initial_data['data'].name info = parse_image_name(filename) kwargs.update(info) kwargs['data'] = serializer.initial_data['data'].read().decode( 'utf-8') serializer.save(**kwargs)