| from flask import jsonify, request, url_for, g, abort |
| |
| from app import db |
| from app.api import bp |
| from app.api.auth import token_auth |
| from app.api.errors import bad_request |
| from app.models import User |
| |
| @bp.route('/users/<int:id>', methods=['GET']) |
| @token_auth.login_required |
| def get_user(id): |
| return jsonify(User.query.get_or_404(id).to_dict()) |
| |
| @bp.route('/users', methods=['GET']) |
| @token_auth.login_required |
| def get_users(): |
| page = request.args.get('page', 1, type=int) |
| per_page = min(request.args.get('per_page', 10, type=int), 100) |
| data = User.to_collection_dict(User.query, page, per_page, 'api.get_users') |
| return jsonify(data) |
| |
| @bp.route('/users', methods=['POST']) |
| def create_user(): |
| data = request.get_json() or {} |
| if 'email' not in data or 'password' not in data: |
| return bad_request('must include email and password fields') |
| if User.query.filter_by(email=data['email']).first(): |
| return bad_request('please use a different email address') |
| user = User() |
| user.from_dict(data, new_user=True) |
| db.session.add(user) |
| db.session.commit() |
| response = jsonify(user.to_dict()) |
| response.status_code = 201 |
| response.headers['Location'] = url_for('api.get_user', id=user.id) |
| return response |
| |
| @bp.route('/users/<int:id>', methods=['PUT']) |
| @token_auth.login_required |
| def update_user(id): |
| user = User.query.get_or_404(id) |
| data = request.get_json() or {} |
| user.from_dict(data, new_user=False) |
| db.session.commit() |
| return jsonify(user.to_dict()) |