def post(self): json_data = load_json() # get the email and password try: email = json_data['email'] password = json_data['password'] confirmed_password = json_data['confirmed_password'] except KeyError: return {'message': 'request must include email, password, and confirmed_password'}, 422 # check if the user exists test_user = UserModel.query.filter_by(email=email).first() if test_user: return {'message': f"There is already an account associated with {email}."}, 403 # check if the passwords match if password != confirmed_password: return {'message': 'Passwords do not match.'}, 409 # hash the password hashed_password = bcrypt.generate_password_hash(password).decode('utf-8') # create a user and add it to the database new_user = UserModel(email=email, password=hashed_password) db.session.add(new_user) db.session.commit() return {'status': 'success'}, 201
def post(self): data = parser.parse_args() current_user = UserModel.find_by_username(data['username']) if not current_user: return {'message': f"User {data['username']} doesn\'t exist"}, 400 if UserModel.verify_hash(data['password'], current_user.password): access_token = create_access_token(identity=data['username']) refresh_token = create_refresh_token(identity=data['username']) return { 'message': f'Logged in as {current_user.username}', 'access_token': access_token, 'refresh_token': refresh_token }, 200 else: return {'message': 'Wrong credentials'}, 400
def get(self, authy_id, user): if authy_id: logging.info(user['user_name']) my_feed = [] feed = UserModel.query().fetch(10) for f in feed: if f.user_name != user['user_name']: logging.info(f) recent_tweet = TweetModel.query(TweetModel.authy_id == f.authy_id).get() is_following = isFollowing(authy_id, f.authy_id) my_feed.append({'user_name':f.user_name, 'following': is_following, 'tweet':recent_tweet.tweet, 'date_created':recent_tweet.date_created.strftime('%m/%d/%Y')}) BaseHandler.server_resp(self, 200, 'My Tweets', my_feed) else: BaseHandler.server_resp(self, 401, 'Not logged in', {})
def post(self, authy_id, user): if authy_id: person_req = json.loads(self.request.body) person = UserModel.query(UserModel.user_name == person_req['user_name']).get() tweets = TweetModel.query(TweetModel.authy_id == person.authy_id).order(-TweetModel.date_created).fetch(20) person_tweets = [] for t in tweets: person_tweets.append( {'date_created':t.date_created.strftime('%m/%d/%Y') , 'tweet':t.tweet} ) is_following = isFollowing(authy_id, person.authy_id); person_data = {'user_name':person.user_name, 'following': is_following, 'tweets':person_tweets} BaseHandler.server_resp(self, 200, 'My Tweets', person_data) else: BaseHandler.server_resp(self, 401, 'Not logged in', {})
def post(self, authy_id, user): if authy_id: user_name = json.loads(self.request.body) follow_user = UserModel.query(UserModel.user_name == user_name['user_name']).get() user_id = follow_user.authy_id following = FollowModel.query(FollowModel.authy_id == authy_id, FollowModel.user_id == user_id).get() logging.info(following) if(following == None): FollowModel(authy_id=authy_id, user_id=user_id, is_following=True).put() else: if(following.is_following): following.is_following = False else: following.is_following = True following.put() BaseHandler.server_resp(self, 200, 'Follow a Person', {}) else: BaseHandler.server_resp(self, 401, 'Not logged in', {})
def post(self): data = parser.parse_args() if UserModel.find_by_username(data['username']): return {'message': f"{data['username']} already exists!!"}, 400 new_user = UserModel(username=data['username'], password=UserModel.generate_hash( data['password'])) try: new_user.save_to_db() access_token = create_access_token(identity=data['username']) refresh_token = create_refresh_token(identity=data['username']) return { 'message': f"User {data['username']} was created.", 'access_token': access_token, 'refresh_token': refresh_token }, 201 except: return {'message': 'Something went wrong'}, 500
def delete(self): return UserModel.delete_all()
def get(self): return UserModel.return_all()