def post(self): parser = reqparse.RequestParser() parser.add_argument('name', required=True, help="The username field is required") parser.add_argument('surname', required=True, help="The username field is required") parser.add_argument('email', required=True, help="The username field is required") parser.add_argument('phoneNo', required=True, help="The username field is required") parser.add_argument('gender', type=int, required=True, help="The username field is required") parser.add_argument('nationality', required=True, help="The username field is required") parser.add_argument('password', required=True, help="The username field is required") parser.add_argument('placement', type=int, required=True, help="The username field is required") data = parser.parse_args() try: user = LongrichUserModel(name=data.name, surname=data.surname, email=data.email, phoneNo=data.phoneNo, gender=data.gender, nationality=data.nationality, placement=data.placement, password=generate_password_hash( data.password)) user.save() return {"error": 0} except: return {"error": 1}
def post(self): data = RootLongrichUser.parser.parse_args() user = LongrichUserModel( name=data.name, surname=data.surname, email=data.email, phoneNo=data.phoneNo, gender=data.gender, nationality=data.nationality, placement=0, password=generate_password_hash(data.password)) user.save() return {"error": 0}
def getLongrichAccounts(name, country, offset): users = LongrichUserModel.get_users_by_offset(name, country, offset) placements = [] for u in users: if u.placementId not in placements: placements.append(u.placementId) placementsFound = LongrichUserModel.get_placements(placements) content = [] for u in users: x = {} x['account'] = u.json() for y in placementsFound: if u.placementId == y.id: x['account']['placement'] = y.json() content.append(x) return {"error": 0, "content": content}
def post(self): data = LongrichUser.parser.parse_args() placement = LongrichUserModel.find_placement(data.placement) if bool(placement): user = LongrichUserModel(name=data.name, surname=data.surname, email=data.email, phoneNo=data.phoneNo, gender=data.gender, nationality=data.nationality, placement=placement.id, password=generate_password_hash( data.password)) user.save() content = {"placement": placement.json()} return {"error": 0, "content": content} else: return {"error": 1}
def put(self, param): parser = reqparse.RequestParser() parser.add_argument('placementId', type=int, required=True, help="The username field is required") data = parser.parse_args() user = LongrichUserModel.find_by_id(int(param)) if user: user.placementId = data.placementId user.save() return {"error": 0} else: return {"error": 1}
def loginAuth(): parser = reqparse.RequestParser() parser.add_argument('code', required=True, help="The username field is required") parser.add_argument('usernameType', type=int, required=True, help="The usernameType field is required") parser.add_argument('username', required=True, help="The username field is required") parser.add_argument('password', required=True, help="The password field is required") data = parser.parse_args() if data.usernameType == 1: current_user = LongrichUserModel.find_by_email(data.username) if not current_user: return jsonify({ "error": 1, 'message': 'User {} doesn\'t exist'.format(data.username) }) if not current_user.verified: if len(data.code) <= 0: return jsonify({ "error": 2, 'message': 'User {} has not been verified. Please provide an account code if this is the first time.' .format(data.username) }) current_user.verify(data.code) elif data.usernameType == 2: current_user = LongrichUserModel.find_by_code(data.username) if not current_user: return jsonify({ "error": 4, 'message': 'A user with the account code {} doesn\'t exist'.format( data.username) }) else: return jsonify({"error": 5, 'message': 'Invalid username Type'}) if current_user.authenticate(data.password): access_token = create_access_token(identity=current_user.id) refresh_token = create_refresh_token(identity=current_user.id) resp = jsonify({ 'error': 0, 'message': 'Logged in as {}'.format(current_user.name) }) set_access_cookies(resp, access_token, 900) set_refresh_cookies(resp, refresh_token) return resp, 200 else: return jsonify({'error': 3, 'message': 'Wrong credentials'})
def identity(payload): user_id = payload['identity'] return LongrichUserModel.find_by_id(user_id)
def authenticate(username, password): user = LongrichUserModel.find_by_username(username) if user and user.authenticate(password): return user