Admin: sports edition
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
from flask import Blueprint, jsonify
|
||||
from flask import Blueprint, jsonify, request
|
||||
from mpwo_api import appLog, db
|
||||
from sqlalchemy import exc
|
||||
|
||||
from ..users.utils import authenticate
|
||||
from .models import Activity, Sport
|
||||
@ -10,7 +12,7 @@ activities_blueprint = Blueprint('activities', __name__)
|
||||
@authenticate
|
||||
def get_sports(auth_user_id):
|
||||
"""Get all sports"""
|
||||
sports = Sport.query.all()
|
||||
sports = Sport.query.order_by(Sport.id).all()
|
||||
sports_list = []
|
||||
for sport in sports:
|
||||
sport_object = {
|
||||
@ -27,6 +29,83 @@ def get_sports(auth_user_id):
|
||||
return jsonify(response_object), 200
|
||||
|
||||
|
||||
@activities_blueprint.route('/sports/<int:sport_id>', 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/<int:sport_id>', methods=['PATCH'])
|
||||
@authenticate
|
||||
def update_sport(auth_user_id, sport_id):
|
||||
"""Update a sport"""
|
||||
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('/activities', methods=['GET'])
|
||||
@authenticate
|
||||
def get_activities(auth_user_id):
|
||||
|
@ -1,7 +1,7 @@
|
||||
import datetime
|
||||
import json
|
||||
|
||||
from mpwo_api.tests.utils import add_activity, add_sport, add_user
|
||||
from mpwo_api.tests.utils import add_activity, add_admin, add_sport, add_user
|
||||
|
||||
|
||||
def test_get_all_sports(app):
|
||||
@ -85,3 +85,67 @@ def test_get_all_activities(app):
|
||||
assert 2 == data['data']['activities'][1]['sport_id']
|
||||
assert 3600 == data['data']['activities'][0]['duration']
|
||||
assert 1024 == data['data']['activities'][1]['duration']
|
||||
|
||||
|
||||
def test_get_a_sport(app):
|
||||
add_user('test', 'test@test.com', '12345678')
|
||||
add_sport('cycling')
|
||||
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='test@test.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = client.get(
|
||||
'/api/sports/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert 'cycling' in data['data']['sports'][0]['label']
|
||||
|
||||
|
||||
def test_update_a_sport(app):
|
||||
add_admin()
|
||||
add_sport('cycling')
|
||||
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(
|
||||
email='admin@example.com',
|
||||
password='12345678'
|
||||
)),
|
||||
content_type='application/json'
|
||||
)
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(
|
||||
label='cycling updated'
|
||||
)),
|
||||
headers=dict(
|
||||
Authorization='Bearer ' + json.loads(
|
||||
resp_login.data.decode()
|
||||
)['auth_token']
|
||||
)
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert 'success' in data['status']
|
||||
|
||||
assert len(data['data']['sports']) == 1
|
||||
assert 'cycling updated' in data['data']['sports'][0]['label']
|
||||
|
@ -5,6 +5,18 @@ from mpwo_api.activities.models import Activity, Sport
|
||||
from mpwo_api.users.models import User
|
||||
|
||||
|
||||
def add_admin():
|
||||
admin = User(
|
||||
username="admin",
|
||||
email="admin@example.com",
|
||||
password="12345678"
|
||||
)
|
||||
admin.admin = True
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
return admin
|
||||
|
||||
|
||||
def add_user(username, email, password):
|
||||
user = User(username=username, email=email, password=password)
|
||||
db.session.add(user)
|
||||
|
Reference in New Issue
Block a user