def post(self): data = load_json() # get the data try: email = data['email'] password = data['password'] except KeyError: return {'message': 'email and password are required'}, 422 # choose whether the email and password match or not validated, user, code = validate_user(email, password) if validated: give_token = True output = {'status': 'success', 'loggedIn': True} else: give_token = False user['loggedIn'] = False output = user # add a token to the output (if applicable) if give_token: token = jwt.encode( { 'id': user.id, 'exp': dt.utcnow() + timedelta(minutes=TOKEN_MINUTES) }, app.config.get('SECRET_KEY')) output['token'] = token.decode('UTF-8') return output, code
def put(self): data = load_json() # get the information try: token = data['token'] user_email = data['user_email'] new_balance = data['new_balance'] except KeyError: return { 'message': 'request must include token, user_email, new_balance' }, 422 # validate the token message, error_code = validate_admin_token(token) if message: return message, error_code # get the user by email user = UserModel.query.filter_by(email=user_email).first() if not user: return {'message': f"no account associated with {user_email}"}, 404 # else update the balance user.balance = new_balance db.session.commit() return { 'status': 'success', 'message': f"{user.email} now has a ${'{:.2f}'.format(user.balance)} balance" }, 201
def delete(self): # validate the data data = load_json() # get the right stuff out try: token = data['token'] email = data['email'] except KeyError: return {'message': 'must include: token, email'}, 422 # validate the admin privileges, code = validate_admin_token(token) # return a code if invalid if code >= 400: return privileges, code # check if the streamer exists (email must be unique) streamer = StreamerModel.query.filter_by(email=email).first() if not streamer: return {'message': f"No streamer found with email {email}"}, 404 # else delete the streamer db.session.delete(streamer) db.session.commit() return { 'status': 'success', 'message': f"Deleted account associated with {streamer.email}" }, 201
def put(self): # get the data data = load_json() try: token = data['token'] constant_name = data['name'] new_constant_value = data['newConstantValue'] except KeyError: return {'message': 'must include: token, constant_name, new_constant_value'}, 422 # validate the admin privileges, code = validate_admin_token(token) # return a code if invalid if code >= 400: return privileges, code # change the constant constant = FloatConstantModel.query.filter_by( name=constant_name).first() if not constant: return {'message': f"Did not find any constants associated with {constant_name}"} constant.constant = new_constant_value db.session.commit() return {'status': 'success', 'message': f"Changed {constant.name} to {constant.constant}"}, 201
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 delete(self): data = load_json() # validate the admin token = load_header_token() message, error_code = validate_admin_token(token) if message: return message, error_code # find the user to delete via email alumni_id = data['alumni_id'] alumni_first_name = data['first_name'] alumni_last_name = data['last_name'] alumni = AlumniModel.query.filter_by(id=alumni_id).first() # if no user, return so if not alumni: return { 'message': f"no account associated with {alumni_id}, {alumni_first_name}, {alumni_last_name}" }, 406 # else delete the user db.session.delete(alumni) db.session.commit() return { 'status': 'success', 'message': f"Deleted {alumni_id}, {alumni_first_name}, {alumni_last_name}" }, 204
def get(self): #should return a list of possible choices for a filter data = load_json() filter_type = data['filter_type'] unique_options = [] try: if (filter_type == "job_company"): unique_options = [ item.job_company for item in db.session.query( AlumniModel.job_company).distinct() ] elif (filter_type == "job_title"): unique_options = [ item.job_title for item in db.session.query( AlumniModel.job_title).distinct() ] elif (filter_type == "job_company_industry"): unique_options = [ item.job_company_industry for item in db.session.query( AlumniModel.job_company_industry).distinct() ] elif (filter_type == "last_name"): unique_options = [ item.last_name for item in db.session.query( AlumniModel.last_name).distinct() ] elif (filter_type == "grad_year"): #not sure if this works yet schools = db.session.query(SchoolModel).filter( SchoolModel.name.like("Georgetown%"), extract('year', SchoolModel.end_date) != dt.strptime( "1000", "%Y").year).all() unique_options = [item.end_date.year for item in schools] options_set = set(unique_options) unique_options = list(options_set) else: return {'message': f"invalid filter type"}, 400 except: return {'message': f"error returning filter options"}, 500 return {'options': unique_options}, 200
def get(self): #get the data based on a filter data = load_json() #dict of filter parameters #ex. 'first_name' : "first name filter" etc ''' filters: last name, graduation year, company, job title, industry ''' filter_params = {} try: filter_params = data['filter'] except: return {'message': f"filter not defined properly"}, 400 results = [] try: if (filter_params['grad_year'] != ""): results = db.session.query(AlumniModel).join( SchoolModel).filter( AlumniModel.last_name.like(filter_params['last_name'] + "%"), AlumniModel.job_company.like( filter_params['job_company'] + "%"), AlumniModel.job_title.like(filter_params['job_title'] + "%"), AlumniModel.job_company_industry.like( filter_params['job_company_industry'] + "%"), SchoolModel.name.like("Georgetown%"), extract('year', SchoolModel.end_date) == dt.strptime( filter_params['grad_year'], "%Y").year).all() else: results = AlumniModel.query.filter( AlumniModel.last_name.like(filter_params['last_name'] + "%"), AlumniModel.job_company.like(filter_params['job_company'] + "%"), AlumniModel.job_title.like(filter_params['job_title'] + "%"), AlumniModel.job_company_industry.like( filter_params['job_company_industry'] + "%")).all() except: return {'message': f"error with filters"}, 500 try: alumni = [object_as_dict(person) for person in results] return {'alumni': alumni}, 200 except: return {'message': f"error returning alumni"}, 500
def post(self): # use a token (proprietary formula) data = load_json() # take out the necessary stuff try: token = data['token'] streamer_count = int(data['streamerCount']) stream_time = float(data['streamTime']) * 60 except KeyError: return { 'message': 'request must include token, streamerCount, and streamTime' }, 422 # validate the token privileges, code = validate_user_token(token) # return the errors if applicable if code >= 400: return privileges, code # else get the active streamers available_streamer_count = len( self.get_available_streamers(streamer_count)) # check if there are enough streamers if available_streamer_count >= streamer_count: sufficient_streamers = True else: sufficient_streamers = False # get how much it would cost cost = self.calculate_cost(stream_time, streamer_count) # check if everything is good user, code = self.check_availability(streamer_count, stream_time, privileges['id']) if code >= 400: return user, code # check if the user has sufficient funds balance = UserModel.query.filter_by( id=privileges['id']).first().balance if balance < cost: sufficient_funds = False else: sufficient_funds = True return { 'status': 'success', 'balance': balance, 'cost': cost, 'available_streamers': available_streamer_count, 'sufficient_funds': sufficient_funds, 'sufficient_streamers': sufficient_streamers }, 201
def post(self): # get the admin_data and authenticate the admin data = load_json() # validate the admin privileges = jwt.decode(data['token'], app.config.get('SECRET_KEY')) if not (privileges['admin_access'] is True): return { 'message': 'You are not allowed to access this resource.' }, 403 # get the data from all the users users = [{'email': user.email} for user in User.query.all()] return {'users': users}, 201
def post(self): data = load_json() # validate the admin if not validate_admin(data['email'], data['password']): return { 'message': 'You are not allowed to access this resource.' }, 403 # get a token and give it to the admin token = jwt.encode( { 'admin_access': True, 'exp': dt.utcnow() + timedelta(minutes=TOKEN_MINUTES) }, app.config['SECRET_KEY']).decode('UTF-8') return {'status': 'success', 'token': token}
def delete(self): json_data = load_json() # get the data try: email = json_data['email'] password = json_data['password'] except KeyError: return {'message': 'email and password are required'}, 422 validated, user, code = validate_user(email, password) if not validated: return user, code # delete the user db.session.delete(user) db.session.commit() return {'status': 'success', 'message': f"Deleted account attached to {user.email}"}, 201
def put(self): json_data = load_json() # get relevant data try: email = json_data['email'] old_password = json_data['old_password'] new_password = json_data['new_password'] except KeyError: return {'message', 'email, old_password, and new_password are required'}, 422 validated, user, code = validate_user(email, old_password) if not validated: return user, code # change the password user.password = bcrypt.generate_password_hash(new_password).decode('utf-8') db.session.commit() return {'status': 'success'}, 201
def delete(self): data = load_json() # validate the admin message, error_code = validate_admin_token(data['token']) if message: return message, error_code # find the user to delete via email user_email = data['user_email'] user = UserModel.query.filter_by(email=user_email).first() # if no user, return so if not user: return {'message': f"no account associated with {user_email}"}, 404 # else delete the user db.session.delete(user) db.session.commit() return {'status': 'success', 'message': f"Deleted {user_email}"}, 201
def delete(self): data = load_json() # validate the admin privileges = jwt.decode(data['token'], app.config.get('SECRET_KEY')) if not (privileges['admin_access'] is True): return { 'message': 'You are not allowed to access this resource.' }, 403 # find the user to delete via email user_email = data['user_email'] user = User.query.filter_by(email=user_email).first() # if no user, return so if not user: return {'message': f"no account associated with {user_email}"}, 404 # else delete the user db.session.delete(user) db.session.commit() return {'status': 'success', 'message': f"Deleted {user_email}"}, 201
def put(self): # get the user data data = load_json() # take out the necessary stuff try: token = data['token'] streamer_count = int(data['streamerCount']) self.stream_time = float(data['streamTime']) * 60 self.stream_url = data['streamUrl'] except KeyError: return { 'message': 'request must include token, streamerCount, streamTime, and streamUrl' }, 422 # validate the token privileges, code = validate_user_token(token) # return the errors if applicable if code >= 400: return privileges, code # check if everything is good user, code = self.check_availability(streamer_count, self.stream_time, privileges['id']) if code >= 400: return user, code # else, charge them cost = self.calculate_cost(self.stream_time, streamer_count) user.balance -= cost db.session.commit() # create a streamer for all of them and start running it --> use a for loop to start streaming immediately available_streamers = self.get_available_streamers(streamer_count) available_streamer_count = len(available_streamers) self.manager = Manager() self.bots = [] self.proc = [] # log in each streamer to check if they are available for streamer in available_streamers: self.add_bot(streamer) newest_model = StreamerModel.query.filter_by( id=available_streamers[-1]).first() while len(self.bots) < available_streamer_count: # get the next newest model that is active newest_model = StreamerModel.query.filter( StreamerModel.id > newest_model.id, StreamerModel.active is True).first() # break if no more streamers if not newest_model: print(f"No more streamers found") break streamer, active = self.check_bot(newest_model) # add the bot to the bots if active: self.add_bot(newest_model.id) print('Joining all threads') for p in self.proc: p.join() return {'status': 'success'}, 201
def post(self): # validate the data data = load_json() # get the right stuff out try: token = data['token'] email = data['email'] email_password = data['emailPassword'] host = data['host'] port = data['port'] proxy_username = data['proxyUsername'] proxy_password = data['proxyPassword'] except KeyError: return { 'message': 'must include: token, host, port, email, emailPassword, proxyUsername, proxyPassword' }, 422 # validate the admin privileges, code = validate_admin_token(token) # return a code if invalid if code >= 400: return privileges, code # check if the streamer exists (email must be unique) test_streamer = StreamerModel.query.filter_by(email=email).first() if test_streamer: # set object as inactive update_obj( test_streamer, dict(host=host, port=port, email=email, email_password=email_password, proxy_username=proxy_username, proxy_password=proxy_password, active=False)) message = { 'status': 'success', 'message': f"updated streamer on {host}:{port} under account {email}" } else: # else add the streamer (email same as username) new_streamer = StreamerModel(host=host, port=port, email=email, email_password=email_password, proxy_username=proxy_username, proxy_password=proxy_password) db.session.add(new_streamer) message = { 'status': 'success', 'message': f"added streamer on {host}:{port} under account {email}" } db.session.commit() return message, 201
def post(self): data = load_json() token = load_header_token() message, error_code = validate_admin_token(token) if message: return message, error_code # get the data try: first_name = data['first_name'] last_name = data['last_name'] except KeyError: return {'message': f"must include first_name and last_name"}, 400 # check if the alumni exists test_alumni = AlumniModel.query.filter_by( first_name=first_name).filter_by(last_name=last_name).first() if test_alumni: return { 'message': f"There is already an account associated with {last_name}, {first_name}." }, 403 new_alumni = AlumniModel(first_name=first_name, last_name=last_name) try: linkedin_url = data['linkedin_url'] new_alumni.linkedin_url = linkedin_url except KeyError: linkedin_url = "" try: job_title = data['job_title'] new_alumni.job_title = job_title except KeyError: job_title = "" try: job_title_role = data['job_title_role'] new_alumni.job_title_role = job_title_role except KeyError: job_title_role = "" try: job_company = data['job_company'] new_alumni.job_company = job_company except KeyError: job_company = "" try: job_company_industry = data['job_company_industry'] new_alumni.job_company_industry = job_company_industry except KeyError: job_company_industry = "" try: job_company_locations_locality = data[ 'job_company_locations_locality'] new_alumni.job_company_locations_locality = job_company_locations_locality except KeyError: job_company_locations_locality = "" try: phone_numbers = data['phone_numbers'] except KeyError: phone_numbers = [] try: emails = data['emails'] except KeyError: emails = [] try: interests = data['interests'] except KeyError: interests = [] try: experiences = data['experiences'] except KeyError: experiences = [] try: education = data['education'] except KeyError: education = [] ''' new_alumni = AlumniModel(first_name=first_name, last_name=last_name, linkedin_url=linkedin_url, job_title=job_title, job_title_role=job_title_role, job_company_industry=job_company_industry, job_company_locations_locality=job_company_locations_locality) ''' for number in phone_numbers: new_alumni.phone_numbers.append( PhoneNumberModel(ph_number=number, alumni_id=new_alumni.id)) for email in emails: new_alumni.emails.append( EmailModel(email=email, alumni_id=new_alumni.id)) for interest in interests: new_alumni.interests.append( InterestModel(interest=interest, alumni_id=new_alumni.id)) for experience in experiences: try: new_experience = ExperienceModel( start_date=dt.strptime(experience['start_date'], "%Y-%m-%d"), end_date=dt.strptime(experience['end_date'], "%Y-%m-%d"), title_name=experience['title_name'], title_role=experience['title_role'], alumni_id=new_alumni.id) new_location = LocationModel( city=experience['company']['location']['city'], state=experience['company']['location']['state'], country=experience['company']['location']['country'], zipcode=experience['company']['location']['zipcode'], locality=experience['company']['location']['locality']) new_company = CompanyModel(name=experience['company']['name'], experience_id=new_experience.id) new_location.company_id = new_company.id new_company.location = [new_location] new_experience.company = [new_company] new_alumni.experiences.append(new_experience) except KeyError: return {'message': f"key error with experiences"}, 500 except: return {'message': f"error adding experience"}, 500 for school in education: try: new_school = SchoolModel( name=school['name'], start_date=dt.strptime(school['start_date'], "%Y-%m-%d"), end_date=dt.strptime(school['end_date'], "%Y-%m-%d"), alumni_id=new_alumni.id) for major in school['majors']: new_major = MajorModel(name=major['name'], school_id=new_school.id) new_school.majors.append(new_major) for minor in school['minors']: new_minor = MinorModel(name=minor['name'], school_id=new_school.id) new_school.minors.append(new_minor) new_alumni.education.append(new_school) except: return {'message': f"error adding education"}, 500 db.session.add(new_alumni) db.session.commit() return { 'status': 'success', 'message': f"Added {new_alumni.first_name}, {new_alumni.last_name}" }, 201