From 7c27086f8649c32cce055ce8f3ebcb9aadf0b009 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 1 May 2018 16:17:12 +0200 Subject: [PATCH] API: minor refactor --- mpwo_api/mpwo_api/__init__.py | 2 + mpwo_api/mpwo_api/activities/activities.py | 199 +-------------------- mpwo_api/mpwo_api/activities/sports.py | 199 +++++++++++++++++++++ 3 files changed, 204 insertions(+), 196 deletions(-) create mode 100644 mpwo_api/mpwo_api/activities/sports.py diff --git a/mpwo_api/mpwo_api/__init__.py b/mpwo_api/mpwo_api/__init__.py index 525bc53d..0bcd69ff 100644 --- a/mpwo_api/mpwo_api/__init__.py +++ b/mpwo_api/mpwo_api/__init__.py @@ -29,10 +29,12 @@ def create_app(): from .users.auth import auth_blueprint # noqa from .users.users import users_blueprint # noqa from .activities.activities import activities_blueprint # noqa + from .activities.sports import sports_blueprint # noqa app.register_blueprint(users_blueprint, url_prefix='/api') app.register_blueprint(auth_blueprint, url_prefix='/api') app.register_blueprint(activities_blueprint, url_prefix='/api') + app.register_blueprint(sports_blueprint, url_prefix='/api') if app.debug: logging.getLogger('sqlalchemy').setLevel(logging.WARNING) diff --git a/mpwo_api/mpwo_api/activities/activities.py b/mpwo_api/mpwo_api/activities/activities.py index 0f9740dd..8ae0e5bf 100644 --- a/mpwo_api/mpwo_api/activities/activities.py +++ b/mpwo_api/mpwo_api/activities/activities.py @@ -1,204 +1,11 @@ -from flask import Blueprint, jsonify, request -from mpwo_api import appLog, db -from sqlalchemy import exc +from flask import Blueprint, jsonify -from ..users.utils import authenticate, is_admin -from .models import Activity, Sport +from ..users.utils import authenticate +from .models import Activity activities_blueprint = Blueprint('activities', __name__) -@activities_blueprint.route('/sports', methods=['GET']) -@authenticate -def get_sports(auth_user_id): - """Get all sports""" - sports = Sport.query.order_by(Sport.id).all() - sports_list = [] - for sport in sports: - sport_object = { - 'id': sport.id, - 'label': sport.label - } - sports_list.append(sport_object) - response_object = { - 'status': 'success', - 'data': { - 'sports': sports_list - } - } - return jsonify(response_object), 200 - - -@activities_blueprint.route('/sports/', methods=['GET']) -@authenticate -def get_sport(auth_user_id, sport_id): - """Get a sport""" - sport = Sport.query.filter_by(id=sport_id).first() - sports_list = [] - if sport: - sports_list.append({ - 'id': sport.id, - 'label': sport.label - }) - response_object = { - 'status': 'success', - 'data': { - 'sports': sports_list - } - } - code = 200 - else: - response_object = { - 'status': 'not found', - 'data': { - 'sports': sports_list - } - } - code = 404 - return jsonify(response_object), code - - -@activities_blueprint.route('/sports', methods=['POST']) -@authenticate -def post_sport(auth_user_id): - """Post a sport""" - if not is_admin(auth_user_id): - response_object = { - 'status': 'error', - 'message': 'You do not have permissions.' - } - return jsonify(response_object), 401 - - sport_data = request.get_json() - if not sport_data or sport_data.get('label') is None: - response_object = { - 'status': 'error', - 'message': 'Invalid payload.' - } - return jsonify(response_object), 400 - - sports_list = [] - try: - new_sport = Sport(label=sport_data.get('label')) - db.session.add(new_sport) - db.session.commit() - sports_list.append({ - 'id': new_sport.id, - 'label': new_sport.label - }) - response_object = { - 'status': 'created', - 'data': { - 'sports': sports_list - } - } - code = 201 - except (exc.IntegrityError, exc.OperationalError, ValueError) as e: - db.session.rollback() - appLog.error(e) - response_object = { - 'status': 'error', - 'message': 'Error. Please try again or contact the administrator.' - } - code = 500 - return jsonify(response_object), code - - -@activities_blueprint.route('/sports/', methods=['PATCH']) -@authenticate -def update_sport(auth_user_id, sport_id): - """Update a sport""" - if not is_admin(auth_user_id): - response_object = { - 'status': 'error', - 'message': 'You do not have permissions.' - } - return jsonify(response_object), 401 - - sport_data = request.get_json() - if not sport_data or sport_data.get('label') is None: - response_object = { - 'status': 'error', - 'message': 'Invalid payload.' - } - return jsonify(response_object), 400 - - sports_list = [] - try: - sport = Sport.query.filter_by(id=sport_id).first() - if sport: - sport.label = sport_data.get('label') - db.session.commit() - sports_list.append({ - 'id': sport.id, - 'label': sport.label - }) - response_object = { - 'status': 'success', - 'data': { - 'sports': sports_list - } - } - code = 200 - else: - response_object = { - 'status': 'not found', - 'data': { - 'sports': sports_list - } - } - code = 404 - except (exc.IntegrityError, exc.OperationalError, ValueError) as e: - db.session.rollback() - appLog.error(e) - response_object = { - 'status': 'error', - 'message': 'Error. Please try again or contact the administrator.' - } - code = 500 - return jsonify(response_object), code - - -@activities_blueprint.route('/sports/', methods=['DELETE']) -@authenticate -def delete_sport(auth_user_id, sport_id): - """Delete a sport""" - if not is_admin(auth_user_id): - response_object = { - 'status': 'error', - 'message': 'You do not have permissions.' - } - return jsonify(response_object), 401 - - sports_list = [] - try: - sport = Sport.query.filter_by(id=sport_id).first() - if sport: - db.session.delete(sport) - db.session.commit() - response_object = { - 'status': 'no content' - } - code = 204 - else: - response_object = { - 'status': 'not found', - 'data': { - 'sports': sports_list - } - } - code = 404 - except (exc.IntegrityError, exc.OperationalError, ValueError) as e: - db.session.rollback() - appLog.error(e) - response_object = { - 'status': 'error', - 'message': 'Error. Please try again or contact the administrator.' - } - code = 500 - return jsonify(response_object), code - - @activities_blueprint.route('/activities', methods=['GET']) @authenticate def get_activities(auth_user_id): diff --git a/mpwo_api/mpwo_api/activities/sports.py b/mpwo_api/mpwo_api/activities/sports.py new file mode 100644 index 00000000..1dc7ea42 --- /dev/null +++ b/mpwo_api/mpwo_api/activities/sports.py @@ -0,0 +1,199 @@ +from flask import Blueprint, jsonify, request +from mpwo_api import appLog, db +from sqlalchemy import exc + +from ..users.utils import authenticate, is_admin +from .models import Sport + +sports_blueprint = Blueprint('sports', __name__) + + +@sports_blueprint.route('/sports', methods=['GET']) +@authenticate +def get_sports(auth_user_id): + """Get all sports""" + sports = Sport.query.order_by(Sport.id).all() + sports_list = [] + for sport in sports: + sport_object = { + 'id': sport.id, + 'label': sport.label + } + sports_list.append(sport_object) + response_object = { + 'status': 'success', + 'data': { + 'sports': sports_list + } + } + return jsonify(response_object), 200 + + +@sports_blueprint.route('/sports/', methods=['GET']) +@authenticate +def get_sport(auth_user_id, sport_id): + """Get a sport""" + sport = Sport.query.filter_by(id=sport_id).first() + sports_list = [] + if sport: + sports_list.append({ + 'id': sport.id, + 'label': sport.label + }) + response_object = { + 'status': 'success', + 'data': { + 'sports': sports_list + } + } + code = 200 + else: + response_object = { + 'status': 'not found', + 'data': { + 'sports': sports_list + } + } + code = 404 + return jsonify(response_object), code + + +@sports_blueprint.route('/sports', methods=['POST']) +@authenticate +def post_sport(auth_user_id): + """Post a sport""" + if not is_admin(auth_user_id): + response_object = { + 'status': 'error', + 'message': 'You do not have permissions.' + } + return jsonify(response_object), 401 + + sport_data = request.get_json() + if not sport_data or sport_data.get('label') is None: + response_object = { + 'status': 'error', + 'message': 'Invalid payload.' + } + return jsonify(response_object), 400 + + sports_list = [] + try: + new_sport = Sport(label=sport_data.get('label')) + db.session.add(new_sport) + db.session.commit() + sports_list.append({ + 'id': new_sport.id, + 'label': new_sport.label + }) + response_object = { + 'status': 'created', + 'data': { + 'sports': sports_list + } + } + code = 201 + except (exc.IntegrityError, exc.OperationalError, ValueError) as e: + db.session.rollback() + appLog.error(e) + response_object = { + 'status': 'error', + 'message': 'Error. Please try again or contact the administrator.' + } + code = 500 + return jsonify(response_object), code + + +@sports_blueprint.route('/sports/', methods=['PATCH']) +@authenticate +def update_sport(auth_user_id, sport_id): + """Update a sport""" + if not is_admin(auth_user_id): + response_object = { + 'status': 'error', + 'message': 'You do not have permissions.' + } + return jsonify(response_object), 401 + + sport_data = request.get_json() + if not sport_data or sport_data.get('label') is None: + response_object = { + 'status': 'error', + 'message': 'Invalid payload.' + } + return jsonify(response_object), 400 + + sports_list = [] + try: + sport = Sport.query.filter_by(id=sport_id).first() + if sport: + sport.label = sport_data.get('label') + db.session.commit() + sports_list.append({ + 'id': sport.id, + 'label': sport.label + }) + response_object = { + 'status': 'success', + 'data': { + 'sports': sports_list + } + } + code = 200 + else: + response_object = { + 'status': 'not found', + 'data': { + 'sports': sports_list + } + } + code = 404 + except (exc.IntegrityError, exc.OperationalError, ValueError) as e: + db.session.rollback() + appLog.error(e) + response_object = { + 'status': 'error', + 'message': 'Error. Please try again or contact the administrator.' + } + code = 500 + return jsonify(response_object), code + + +@sports_blueprint.route('/sports/', methods=['DELETE']) +@authenticate +def delete_sport(auth_user_id, sport_id): + """Delete a sport""" + if not is_admin(auth_user_id): + response_object = { + 'status': 'error', + 'message': 'You do not have permissions.' + } + return jsonify(response_object), 401 + + sports_list = [] + try: + sport = Sport.query.filter_by(id=sport_id).first() + if sport: + db.session.delete(sport) + db.session.commit() + response_object = { + 'status': 'no content' + } + code = 204 + else: + response_object = { + 'status': 'not found', + 'data': { + 'sports': sports_list + } + } + code = 404 + except (exc.IntegrityError, exc.OperationalError, ValueError) as e: + db.session.rollback() + appLog.error(e) + response_object = { + 'status': 'error', + 'message': 'Error. Please try again or contact the administrator.' + } + code = 500 + return jsonify(response_object), code