def followRequest(): data = flask_post_json() target = getUser(data["author"]["id"]) if target.origin in data["friend"]["url"]: newfriend = getUser(str(data["friend"]["id"])) target.followers.append(newfriend.uid) else: f = poll.get(data["friend"]["url"].rstrip("/")+"/author/"+str(data["friend"]["id"])) # parse data into object for creation uid = f["id"] first = f["displayName"].split(" ")[0] last = f["displayName"].split(" ")[-1] email = f["email"] # add potential gitid if "gitId" in f.keys(): gitId = f["gitId"] else: gitId = "abramhindle" #create usermodel to be committed u = UserModel(uid, first, last, email, "default", gitId) u.origin = f["host"] db.session.add(u) db.session.commit() return json.dumps({})
def put(self, token): user = UserModel.verify_token(token) if not isinstance(user, UserModel): return user user.set_status(1) return {'msg': 'You successfuly activated your account!'}, 200
def delete(self): _id = request.args.get('id') user = UserModel.find_by_id(_id) if user is None: return {'msg': f"There is no user with ID:{_id}!"}, 404 user.set_status(0) return {'msg': f"User with ID:{_id} successfully deactivated!"}
def register_account(email: str, password: str) -> UserModel: now = datetime.utcnow() expiry = now + timedelta(hours=24) token = uuid.uuid4().hex salt = bcrypt.gensalt() encoded_password = password.encode(encoding='utf-8') hashed = bcrypt.hashpw(encoded_password, salt) new_user = UserModel( username=email, salt=salt, hash=hashed, token=token, login_date=now, login_expiry=expiry, register_date=now, ) db.session.add(new_user) return new_user
def put(self): data = User.get_update_args() token = request.headers.get('x-auth') user = UserModel.verify_token(token) if not user.check_password(data['old_password']): return {'msg': 'Password is incorrect!'}, 400 if data['new_password'] != data['confirm_password']: return {'msg': 'Passwords don\'t match'}, 400 user.first_name = data['first_name'] user.last_name = data['last_name'] user.password = user.hash_password(data['new_password']) user.save_to_db() return {'msg': 'You successfully changed your password'}, 200
def post(self): start_time = time.time() data = LoginUser.get_login_args() user = UserModel.find_by_username(data['user_name']) if not user: return {'error': 'No user with given username'}, 400 if user.status == 0: return {'error': 'You must activate your account'}, 400 if not user.check_password(data['password']): return {'error': 'Password is incorrect'}, 400 token = user.get_token() log = ActivityModel(user.id, url_for('loginuser'), (time.time() - start_time)) log.save_to_db() return {'token': token.decode('utf-8')}
def post(self): data = RegisterUser.get_register_args() user = UserModel.find_by_email(data['email']) if user: return {'msg': 'User with given email already exists!'}, 400 user = UserModel.find_by_username(data['user_name']) if user: return {'msg': 'User with given username already exists!'}, 400 user = UserModel(**data) user.save_to_db() send_activation_email(user) return {'msg': 'Please check mail to activate the account'}, 201
def post(self): data = User.get_create_args() user = UserModel.find_by_email(data['email']) if user: return {'msg': 'User with given email already exists!'}, 400 user = UserModel.find_by_username(data['user_name']) if user: return {'msg': 'User with given username already exists!'}, 400 user = UserModel(**data) user.save_to_db() if not current_app.config['TESTING']: send_activation_email(user) return {'msg': 'Please check mail to activate the account'}, 201