def create_user(): data_received = request.get_json() new_user = UserModel(data_received['username'], data_received['password']) if new_user.user_exist(): return jsonify({'message': "Username already exist"}), 400 new_user.save_to_db() return jsonify({ 'message': "Created Successfully", "username": data_received['username'] }), 200
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = UserModel(username=form.username.data, password=hashed_password, email=form.email.data) user.save_to_db() flash(f'account created for {form.username.data}', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def post(self): data = _user_login.parse_args() username = data['username'] password = data['password'] service = data['service'] if not username or not password or not service: return {"message": "Request data is not correct"}, 400 user = UserModel.find_user_by_username(username) if user and user.password == hashlib.sha256(password.encode( "utf-8")).hexdigest() and user.service == service: ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr) user_agent = request.headers.get('User-Agent') login_history = Login_History(user.id, datetime.datetime.now(), ip, user_agent) login_history.save_to_db() access_token = create_access_token(identity=user.id, fresh=True) refresh_token = create_refresh_token(identity=user.id) return { "access_token": access_token, "refresh_token": refresh_token }, 200 return {"message": "Invalid credentials!"}, 401
def delete(self, user_id): user = UserModel.find_user_by_id(user_id) if user: user.remove_from_db() return {"message": "User deleted!"}, 200 return {"message": "User not found!"}, 404
def put(self, id): user = UserModel.find_user_by_id(id) if user: user.account_status = "terminated" user.save_to_db() return {"message": "Successfully Terminated"}, 200 return 'User not found', 404
def put(self, id): valid_until = request.json.get('valid_until') user = UserModel.find_user_by_id(id) if user: user.valid_until = valid_until user.save_to_db() return {"message": "Successfully Updated"}, 200 return 'User not found', 404
def users_data(): """Sample users data.""" db.session.add(UserModel(username='******', password='******', email='test1@com', active=True, created_datetime='2020-05-16 13:51:18.468379')) db.session.commit()
def put(self, id): old_password = request.json.get('old_password') new_password = request.json.get('new_password') user = UserModel.find_user_by_id(id) if user.password == hashlib.sha256( old_password.encode("utf-8")).hexdigest(): user.password = hashlib.sha256( new_password.encode("utf-8")).hexdigest() user.save_to_db() return {"message": "Password has been changed successfully."}, 200 return 'Incorrect password', 400
def post(self): data = _user_parser.parse_args() username = data["username"] email = data["email"] password = hashlib.sha256(data["password"].encode("utf-8")).hexdigest() country = data["country"] service = data["service"] lang = data["lang"] mobilenumber = data["mobilenumber"] registered = datetime.datetime.now() registration_ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr) b64_img = data["b64_img"] account_status = "pending" valid_until = datetime.datetime.now() + datetime.timedelta(hours=1) verified = False payment_reference = data["payment_reference"] if UserModel.find_user_by_username(data["username"]): return {"message": "User exists!"}, 400 user = UserModel(username, email, password, country, service, lang, mobilenumber, registered, registration_ip, b64_img, account_status, valid_until, verified, payment_reference) user.save_to_db() email_confirmation_data = Email_Settings.query.filter_by( service=user.service).first() mail.init_app(app, email_confirmation_data) token = generate_confirmation_token(user.email) confirm_url = url_for('confirmationview', token=token, _external=True) html = render_template('confirmation.html', confirm_url=confirm_url) subject = "Please confirm your email" send_email(user.email, subject, html) return {"message": "User {} created!".format(data["username"])}
def login(): data_received = request.get_json() user = UserModel.find_by_username(data_received['username']) if user is not None: if user.verify_user(data_received['password']): user_json = dict() user_json['username'] = user.username user_json['id'] = user.id access_token = create_access_token(identity=user_json, fresh=True) refresh_token = create_refresh_token(identity=user_json) return jsonify({ 'message': 'Login Successful', 'access_token': access_token, 'refresh_token': refresh_token }) return jsonify({'message': "Login Unsuccessful"}), 401
def post(self): data = _user_parser.parse_args() if UserModel.find_by_username(data['username']): return { "message": "User with this username is already exists." }, 400 elif UserModel.find_by_email(data['email']): return {"message": "User with this email is already exists."}, 400 user = UserModel(**data) user.save_to_db() return {"message": "User created successfully."}, 201
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('home')) user = UserModel.verify_reset_token(token) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your password has been updated! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('reset_token.html', title='Reset Password', form=form)
def seed_db(): db.session.add_all([ TeamModel(team_name='test', spi=17.5, off=1.2, defi=4.1, goal_dif=1.1, pts=2.1, relegated=1, make_from_playoffs=2, promoted=0, win_championship=1), TeamModel(team_name='test2', spi=17.5, off=4.2, defi=4.1, goal_dif=1.1, pts=2.1, relegated=0, make_from_playoffs=2, promoted=1, win_championship=0) ]) db.session.commit() db.session.add_all([ MatchModel(match_id=1, match_date='2019-08-10', round_name='Week 1', first_squad_name='Liverpool', first_squad_score=0, first_squad_points=0, second_squad_name='Manchester City', second_squad_score=5, second_squad_points=3), MatchModel(match_id=2, match_date='2019-08-10', round_name='Week 2', first_squad_name='Manchester City', first_squad_score=0, first_squad_points=0, second_squad_name='Liverpool', second_squad_score=5, second_squad_points=2), MatchModel(match_id=3, match_date='2019-08-10', round_name='Week 2', first_squad_name='Manchester City', first_squad_score=0, first_squad_points=0, second_squad_name='Burnley', second_squad_score=5, second_squad_points=2) ]) db.session.commit() db.session.add( UserModel(username='******', password='******', email='test@com', active=True, created_datetime='2020-05-16 13:51:18.468379')) db.session.commit()
def get(cls, user_id): user = UserModel.find_by_id(user_id) if not user: return {'message': "User '{}' not found.".format(user_id)}, 404 return user.json()
def get(self, user_id): user = UserModel.find_user_by_id(user_id) if user: return user.json() return {"message": "User not found!"}, 404
def delete(cls, user_id): user = UserModel.find_by_id(user_id) if not user: return {'message': "User '{}' not found.".format(user_id)}, 404 user.delete_from_db() return {'message': "User '{}' deleted.".format(user_id)}, 200
def get(self): users = [user.json() for user in UserModel.find_all()] return {'users': users}