def setUp(self, **kwargs): server.app.config['MONGO_DB'] = 'tagallery-test' server.app.config['TESTING'] = True server.app.config['DEBUG'] = True if kwargs: server.app.config.update(**kwargs) server.init_db('tagallery-test') User.drop_collection() Image.drop_collection() self.app = server.app.test_client() return
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 index(self): """List the images.""" # after = request.values.get('after') # per_page = request.values.get('ipp', 15) tags = request.values.get('tags') self.log.debug('Tags = {tags}'.format(tags=tags)) params = {} if tags: tags = [tag.strip() for tag in tags.split(',') if tag.strip()] if tags: params['tags__all'] = tags result = [] self.log.debug('Query params: {params}'.format(params=params)) for image in Image.objects(**params).order_by('-created_at'): record = mongoengine_to_dict(image) record['url'] = url_for('ImageView:raw', image_id=image.id) result.append(record) return jsonify(status='OK', images=result) # select # images.title # from # images, # tagged, # tags # where # images.uid == tagged.image # AND tagged.tag == tags.uid # AND tags.desc in (:list) # GROUP BY # images.title # HAVING # count(distinct tags.uid) >= :count raise NotImplemented
def tearDown(self): User.drop_collection() Image.drop_collection() return