def add_image(self, *_, **kwargs): """accept uploaded images and attach them to the record""" dummy = zoom.Record( filename='dummy.png', file=io.StringIO('test'), ) # put the uploaded image data in a bucket path = os.path.join(zoom.system.site.data_path, 'buckets') bucket = Bucket(path) f = kwargs.get('file', dummy) name = f.filename data = f.file.read() item_id = bucket.put(data) # create an image record image = Image( image_id=item_id, image_size=len(data), image_name=name, draft=True, ) images = zoom.store.store_of(Image) images.put(image) return item_id
def edit(self, key=None, *a, **k): path = os.path.join(system.site.data_path, 'buckets') bucket = Bucket(path) form1.update(sample) form1.update(k) content = form1.edit() return page(content, actions=actions, title='Edit Mode')
def add_image(self, *_, **kwargs): """accept uploaded images and attach them to the record""" dummy = Record( filename='dummy.png', file=io.StringIO('test'), ) # put the uploaded image data in a bucket path = os.path.join(zoom.system.site.data_path, 'buckets') bucket = Bucket(path) f = kwargs.get('file', dummy) name = f.filename data = f.file.read() item_id = bucket.put(data) # create an attachment record for this bucket c = self.collection field_name = kwargs.get('field_name', 'unknown') field_value = kwargs.get('field_value', 'unknown') attachment = Attachment( record_kind=c.store.kind, field_name=field_name, field_value=field_value, attachment_id=item_id, attachment_size=len(data), attachment_name=name, ) attachments = zoom.store.store_of(Attachment) attachments.put(attachment) return item_id
def test_delete(self): bucket = Bucket(self.path, self.ids.pop) self.assertEqual(bucket.put(b'some data'), 'id_0000') self.assertEqual(bucket.put(b'some more data'), 'id_0001') self.assertEqual(sorted(bucket.keys()), ['id_0000', 'id_0001']) for item_id in bucket.keys(): bucket.delete(item_id) self.assertEqual(sorted(bucket.keys()), [])
def add_image(self, *a, **k): """add a ImagesField image""" path = os.path.join(system.site.data_path, 'buckets') bucket = Bucket(path) f = k.get('file') name = f.filename data = f.file.read() item_id = bucket.put(data) return item_id
def remove_image(self, *a, **k): """remove a ImagesField image""" path = os.path.join(system.site.data_path, 'buckets') bucket = Bucket(path) items = bucket.keys() item_id = k.get('id', None) if item_id in bucket.keys(): bucket.delete(item_id) return 'ok' return 'empty'
def list_images(self, key='test', value='test'): """return list of images for an ImagesField value for this record""" attachments = store(Attachment) path = os.path.join(system.site.data_path, 'buckets') bucket = Bucket(path) t = [ dict( name=a.attachment_name, size=a.attachment_size, item_id=a.attachment_id, url=url_for('get_image', item_id=a.attachment_id), ) for a in attachments.find(field_value=value) ] return json.dumps(t)
def __call__(self, *args, **kwargs): """Overide default Controller.__call__ behaviour""" # see if an image is being requested if len(args) == 1 and not bool(kwargs): images = zoom.store.store_of(Image) image = images.first(image_id=args[0]) or images.first( image_name=args[0]) if image: item_id = image.image_id path = os.path.join(zoom.system.site.data_path, 'buckets') bucket = Bucket(path) return image_response(image.image_name, bucket.get(item_id)) return zoom.Controller.__call__(self, *args, **kwargs)
def remove_image(self, *_, **kwargs): """remove a dropzone image""" # k contains item_id and filename for file to be removed item_id = kwargs.get('id', None) # detach the image from the record if item_id: images = zoom.store.store_of(Image) key = images.first(image_id=item_id) if key: images.delete(key) # delete the bucket path = os.path.join(zoom.system.site.data_path, 'buckets') bucket = Bucket(path) if item_id in bucket.keys(): bucket.delete(item_id) return 'ok' return 'empty'
def get_image(self, *a, **k): """return one of the images from an ImagesField value""" item_id = k.get('item_id', None) path = os.path.join(system.site.data_path, 'buckets') bucket = Bucket(path) return image_response('house.png', bucket.get(item_id))
def test_keys(self): bucket = Bucket(self.path, self.ids.pop) self.assertEqual(bucket.put(b'some data'), 'id_0000') self.assertEqual(bucket.put(b'some more data'), 'id_0001') self.assertEqual(sorted(bucket.keys()), ['id_0000', 'id_0001'])
def test_item_exists(self): bucket = Bucket(self.path, self.ids.pop) item_id = bucket.put(b'some data') self.assertEqual(item_id, 'id_0000') self.assertEqual(bucket.exists(item_id), True)
def clear(self): bucket = Bucket(self.path, self.ids.pop) for item_id in bucket.keys(): bucket.delete(item_id)
def show(self, key): path = os.path.join(zoom.system.site.data_path, 'buckets') bucket = Bucket(path) return image_response('house.png', bucket.get(key))
def get_image(self, *a, **kwargs): # pylint: disable=W0613 """return one of the images""" item_id = kwargs.get('item_id', None) path = os.path.join(zoom.system.site.data_path, 'buckets') bucket = Bucket(path) return image_response('house.png', bucket.get(item_id))
def test_put_get(self): bucket = Bucket(self.path, self.ids.pop) item_id = bucket.put(b'some data') self.assertEqual(item_id, 'id_0000') self.assertEqual(bucket.get(item_id), b'some data')
def show(self, key): path = os.path.join(zoom.system.site.data_path, 'buckets') bucket = Bucket(path) image = bucket.get(key, False) if image: return image_response('image.png', image)