def api_delete_album(album_id): validator = Validator([Validation.album_exists(album_id)]) if not validator.validate(request): return validator.get_error_response() album = Album.query.get(album_id).delete() return '', status.HTTP_200_OK
def api_update_image(image_id): validator = Validator( [Validation.is_json_payload(), Validation.image_exists(image_id)]) if not validator.validate(request): return validator.get_error_response() image = Image.query.get(image_id) caption = request.get_json().get('caption') if caption: image.update_caption(caption) location = request.get_json().get('location') if location: image.update_location(location) date = request.get_json().get('date') if date: image.update_date(datetime.datetime.fromisoformat(date)) keyword_strings = request.get_json().get('keywords') if keyword_strings: keywords = [Keyword(s) for s in keyword_strings] image.update_keywords(keywords) return jsonify(image), status.HTTP_200_OK
def api_delete_image(image_id): validator = Validator([Validation.image_exists(image_id)]) if not validator.validate(request): return validator.get_error_response() Image.query.get(image_id).delete() return '', status.HTTP_200_OK
def api_delete_portfolio(portfolio_id): validator = Validator([Validation.portfolio_exists(portfolio_id)]) if not validator.validate(request): return validator.get_error_response() Portfolio.query.get(portfolio_id).delete() return '', status.HTTP_200_OK
def api_create_album(): validator = Validator( [Validation.is_json_payload(), Validation.required_json('name')]) if not validator.validate(request): return validator.get_error_response() image_ids = request.get_json().get('image_ids', []) images = [Image.query.get(id) for id in image_ids] new_album = Album(request.get_json().get('name'), images) db.session.add(new_album) db.session.commit() return jsonify(new_album), status.HTTP_201_CREATED
def api_album_remove_image(album_id, image_id): validator = Validator([ Validation.album_exists(album_id), Validation.image_exists(image_id) ]) if not validator.validate(request): return validator.get_error_response() album = Album.query.get(album_id) image = Image.query.get(image_id) if image in album.images: album.remove_image(image) return '', status.HTTP_200_OK else: return '', status.HTTP_400_BAD_REQUEST
def api_portfolio_add_album(portfolio_id, album_id): validator = Validator([ Validation.portfolio_exists(portfolio_id), Validation.album_exists(album_id) ]) if not validator.validate(request): return validator.get_error_response() portfolio = Portfolio.query.get(portfolio_id) album = Album.query.get(album_id) if album not in portfolio.albums: portfolio.add_album(album) return '', status.HTTP_200_OK else: return '', status.HTTP_400_BAD_REQUEST
def api_update_album(album_id): validator = Validator( [Validation.is_json_payload(), Validation.album_exists(album_id)]) if not validator.validate(request): return validator.get_error_response() album = Album.query.get(album_id) name = request.get_json().get('name') if name: album.update_name(name) image_ids = request.get_json().get('image_ids', []) images = [Image.query.get(id) for id in image_ids] if images: album.update_images(images) return jsonify(album), status.HTTP_200_OK
def api_create_portfolio(): validator = Validator([ Validation.is_json_payload(), Validation.required_json('name'), Validation.required_json('primary_album_id') ]) if not validator.validate(request): return validator.get_error_response() name = request.get_json().get('name') primary_album_id = request.get_json().get('primary_album_id') album_ids = request.get_json().get('album_ids', []) albums = [Album.query.get(id) for id in album_ids] new_portfolio = Portfolio(name, primary_album_id, albums) db.session.add(new_portfolio) db.session.commit() return jsonify(new_portfolio), status.HTTP_201_CREATED
def api_create_image(): # TODO: Check some kind of user authorization unique_image_validation = Validation.custom_json_validation( 'image_name', lambda v: not image_name_exists_in_database(v), lambda v: f'Image with the name {v} already exists in the database' ) validator = Validator([ Validation.is_json_payload(), Validation.required_json('image_name'), Validation.required_json('image_data'), unique_image_validation ]) if not validator.validate(request): return validator.get_error_response() new_image = Image.fromNameAndData( request.get_json().get('image_name'), bytes(request.get_json().get('image_data'), encoding='utf8')) db.session.add(new_image) db.session.commit() return jsonify(new_image), status.HTTP_201_CREATED
def api_update_portfolio(portfolio_id): validator = Validator([ Validation.is_json_payload(), Validation.portfolio_exists(portfolio_id) ]) if not validator.validate(request): return validator.get_error_response() portfolio = Portfolio.query.get(portfolio_id) name = request.get_json().get('name') if name: portfolio.update_name(name) primary_album_id = request.get_json().get('primary_album_id') if primary_album_id: portfolio.update_primary_album(primary_album_id) album_ids = request.get_json().get('album_ids', []) albums = [Album.query.get(id) for id in album_ids] if albums: portfolio.update_albums(albums) return jsonify(portfolio), status.HTTP_200_OK
def api_get_album(album_id): validator = Validator([Validation.album_exists(album_id)]) if not validator.validate(request): return validator.get_error_response() return jsonify(Album.query.get(album_id)), status.HTTP_200_OK