def add_to_images(self, source_filename, title=None, tags=None, target_filename=None): """Add an image to the final directory.""" template = os.path.join(self.path, 'images', source_filename) if not os.path.exists(template): return created_at = datetime.datetime.utcnow() final = os.path.join(partition(created_at, self.image_dir), target_filename or source_filename) shutil.copy(template, final) image = Image(title=title or '', tags=tags, created_at=created_at, filename=target_filename or source_filename) image.save() self.log.debug('Added image {pk}'.format(pk=image.pk)) return
def post(self): """Add a new image.""" json = request.get_json(force=True, silent=True) if not json: raise TagalleryRequestMustBeJSONException() # the required fields filename = json.get('filename') if not filename: raise TagalleryMissingFieldException('filename') tags = json.get('tags') tags = self._strip_tags(json.get('tags')) if not tags: raise TagalleryMissingFieldException('tags') self.log.debug('Tags (as Tags): {tags}'.format(tags=tags)) # optional fields title = json.get('title', '') # check if the file is in the queue queue_dir = current_app.config['QUEUE_DIR'] in_queue = os.path.join(queue_dir, filename) if not os.path.exists(in_queue): raise TagalleryNotSuchFilenameException() # everything in position, try to move the file from the queue to the # final directory created_at = datetime.date.today() final = os.path.join(partition(created_at), filename) shutil.move(in_queue, final) # save the image Image(title=title, tags=tags, created_at=created_at, filename=filename) return jsonify(status='OK')
def raw(self, image_id): image = Image[image_id] final = os.path.join(partition(image.created_at), image.filename) return send_from_directory(final)