def withdraw(user_id): data = request.get_json(force=True) amount = data['amount'] pin = data['pin'] if amount > 2000: abort( 400, description='You are not allowed to go over 2000 euro daily limit') else: user = User.query.get_or_404(user_id, "User does nor exist") if pin == user.pin: if amount <= user.balance: user.balance -= amount db.session.commit() user_schema = UserSchema() response = user_schema.dump(user).data return jsonify(response) else: abort( 400, description= "You are not allowed to withdraw more money than you have on your account!", ) else: abort(400, description="Pin is not correct")
def transfer(user_id): data = request.get_json(force=True) amount = data["amount"] pin_number = data["pin"] receiver_id = data["receiverId"] if amount >= 3000: abort( 400, description='You are not allowed to go over 3000 euro daily limit') else: sender = User.query.get_or_404(user_id, "Sender does not exist") if pin_number == sender.pin: if amount <= sender.balance: receiver = User.query.get_or_404( receiver_id, "Receiver user does not exist") sender.balance -= amount receiver.balance += amount db.session.commit() user_schema = UserSchema() response = user_schema.dump(receiver).data return jsonify(response) else: abort(400, description= 'You dont have enought amount of money in your acount!') else: abort(400, description="Pin is not correct")
def get_user(user_id): user = User.query.filter_by(id=user_id).first() if user: user_schema = UserSchema() response = user_schema.dump(user).data return jsonify(response) else: abort(404, description="User does not exist")
def update_user(user_id): data = request.get_json() new_name = data['name'] new_pin = data['pin'] new_balance = data['balance'] user = User.query.get_or_404(user_id, "Baby, the perfect man does not exist, kisses !") user.name = new_name user.pin = new_pin user.balance = new_balance db.session.commit() user_schema = UserSchema() response = user_schema.dump(user).data return jsonify(response)
def deposit(user_id): data = request.get_json(force=True) amount = data['amount'] pin = data['pin'] if amount >= 3000: abort( 400, description='You are not allowed to go over 3000 euro daily limit') else: user = User.query.get_or_404(user_id, "User does nor exist") if pin == user.pin: user.balance += amount db.session.commit() user_schema = UserSchema() response = user_schema.dump(user).data return jsonify(response) else: abort(400, description="Pin is not correct")
def update_user(user_id): data = request.get_json( force=True ) # force=True will make sure this works even if a client does not specify application/json validate(instance=data, schema=user_request_schema) user_name = data["name"] pin = data["pin"] user = User.query.filter_by(id=user_id).first() if user: user.name = user_name user.pin = pin db.session.commit() user_schema = UserSchema() response = user_schema.dump(user).data return jsonify(response) else: abort(404, description="User does not exist" ) # for now we will abort, then work on exception handling
def create_user(): data = request.get_json() new_name = data['name'] new_pin = int(data['pin']) new_balance = int(data['balance']) if not new_name and new_balance and new_pin: abort(400, description="Your fault client!!") else: # check user does exist user = User.query.filter_by(name=new_name).first() if user: abort(400,description="Upss! User already exists") else: new_user = User(name=new_name, pin=new_pin, balance=new_balance) db.session.add(new_user) db.session.commit() user_schema = UserSchema() response = user_schema.dump(new_user).data return jsonify(response)
def get_user(user_id): user = User.query.get_or_404(user_id, "Baby, the perfect man does not exist, kisses !") user_schema = UserSchema() response = user_schema.dump(user).data return jsonify(response)
def get_users(): user_schema = UserSchema(many=True) users = User.query.all() response = user_schema.dump(users).data return jsonify(response)
# Library imports from flask_restplus import Namespace, Resource, fields from flask import request, current_app from flask_praetorian import auth_required, current_user from flask_praetorian.exceptions import MissingUserError # Local imports from project.models import db from project.auth import guard, jwt_blacklist from project.schemas import UserSchema from project.core import user_utils, limit_access api = Namespace('users') user_schema = UserSchema(dump_only=('date_created', 'translations', 'terms', 'roles')) # Flask-Restplus models for documentation login_model = api.model( 'Login', { 'username': fields.String(required=True, min_length=3, max_length=25), 'password': fields.String(required=True, min_length=8), }) register_model = api.inherit( 'Register', login_model, {'email': fields.String(example='*****@*****.**')}) @api.route('/register/', endpoint='register') class RegisterAPI(Resource):