def get_user_jobs(user_id): """Used to retrieve user's jobs. Args: user_id (str): Unique id of the user Return: List of dictionaries (where each dictionary is a matched object """ user = database.get('User', user_id) if request.method == 'GET': results = user.get_jobs_applied() return jsonify(results), 200 if request.is_json is False: return jsonify(error="Not a valid JSON"), 400 data = request.get_json() if request.method == 'POST': user.create_jobs_applied(**data) if request.method == 'PUT': user.edit_job(**data) if request.method == 'DELETE': user.delete_job(**data) return jsonify(success="yay"), 200
def csv(): """ API call for the CSV formatted jobs_applied variable of a user :return: CSV formatted jobs_applied variable of the user """ user = database.get('User', session['id']) return user.get_csv()
def user_token(): """ API call for obtaining the user's current amount of currency :return: Currency of a user """ user = database.get('User', session['id']) return jsonify({'currency': user.currency})
def user_info(): """ API call for returning a user for userpage :return: JSON formatted user information """ user = database.get('User', session['id']) return jsonify(user.to_json())
def jobs_applied(): """ Used to retrieve, add, update, and delete jobs that the user has applied to :return: A user's jobs_applied for successful GET requests, error on invalid requests, and status code otherwise """ user = database.get('User', session['id']) # GET: Return all jobs that user has applied to if request.method == 'GET': return jsonify(user.jobs_applied), 200 if request.is_json is False: return jsonify(error="Not a valid JSON"), 400 jobs = json.loads(user.jobs_applied) data = request.get_json() if request.method != 'POST': job_id = data.get('id') if ((request.method == 'DELETE' or request.method == 'PUT') and (job_id not in jobs)): response = {'error': 'Not a valid ID'} else: # PUT: Change an existing entry response = {'success': True} message = '' if request.method == 'PUT': token = 10 for key, value in data.items(): if key != 'id': if value == 'Rejection': message = 'Rejection is hard - Have some extra coins!' token += 20 if value == 'Received Offer': message = 'Congratulations!!!!' token += 50 jobs[job_id][key] = value user.currency += token # POST: Creates a new entry elif request.method == 'POST': print(data) job_id = str(uuid.uuid4()) jobs[job_id] = data token = 30 user.currency += token # DELETE: Deletes an entry elif request.method == 'DELETE': jobs.pop(job_id) user.jobs_applied = json.dumps(jobs) user.save() response['token'] = token response['message'] = message status = 200 if 'success' in response.keys() else 404 return jsonify(response), status
def rewards(): """ Processes GET and POST requests to fetch a randomized array of rewards or update user_rewards with a newly created user_reward relationship, respectively :return: For GET requests will return a dictionary with an array of randomized rewards, populated based on rarity For POST requests will take the data and add the user_reward object to the database based on user_id and reward_id """ if request.method == 'GET': rand_rewards = {'data': []} for i in range(10): random_int = randint(0, 100) """ Setting rarities of rewards based on the result of a random int """ if random_int < 70: roll = database.get('Reward', str(randint(26, 48))) elif random_int < 90: roll = database.get('Reward', str(randint(17, 25))) elif random_int < 98: roll = database.get('Reward', str(randint(3, 16))) else: roll = database.get('Reward', str(randint(0, 2))) if roll is None: roll = database.get('Reward', '30') rand_rewards['data'].append({'name': roll.name, 'img': roll.image, 'rarity': roll.rarity, 'id': roll.id}) return jsonify(rand_rewards) data = request.get_json() user = database.get('User', data['user_id']) """ Checks database for duplicate user_reward entry """ if user.check_duplicate_reward(data['reward_id']) is False: new_user_reward = UserReward(**data) new_user_reward.save() user.currency -= 30 user.save() return jsonify(user.to_json())
def get_user_jobs_appliedstats(user_id): """Used to retrieve jobs applied stats of user Args: user_id (str): unique id of user Returns: Dictionary of the following results: avg_applications (int): Average applications per week up to today this_week (int): Applications this week """ user = database.get('User', user_id) results = user.get_jobs_applied_stats(date.today()) return jsonify(results), 200
def jobs_interested(): """ Used to retrieve, add, update, and delete jobs that the user is interested in :return: A user's jobs_interested for valid GET request, error, or status code for valid non GET requests """ user = database.get('User', session['id']) # GET: Return all jobs that user is interested in if request.method == 'GET': return jsonify(user.jobs_interested), 200 if request.is_json is False: return jsonify(error="Not a valid JSON"), 400 jobs = json.loads(user.jobs_interested) data = request.get_json() job_id = data.get('id') if ((request.method == 'DELETE' or request.method == 'PUT') and (job_id not in jobs)): response = {'error': 'Not a valid job ID'} else: # PUT: Change an existing entry if request.method == 'PUT': for key, value in data.items(): if key != 'id': jobs[job_id][key] = value user.currency += 10 # POST: Creates a new entry elif request.method == 'POST': if job_id not in jobs: data.pop('id') jobs[job_id] = data user.currency += 10 # DELETE: Deletes an entry elif request.method == 'DELETE': jobs.pop(job_id) user.jobs_interested = json.dumps(jobs) user.save() response = {'success': True} status = 200 if 'success' in response else 404 return jsonify(response), status
def user_email(user_id): """Used to retrieve, add and update user's email Assumptions: All checks to verify if email is valid will be performed on frontend side User id should exist in database Args: Params: user_id should be specified in url Response Body: Put Request - Should contain new email address Returns: Email address of the user. """ user = database.get('User', user_id) if request.method == 'PUT': body = request.get_json() user.email = body.get('email') user.save() return jsonify(email=user.email), 200
def rewards(): """ testing things """ if request.method == 'GET': rewards = {'data': []} for i in range(10): random_int = randint(0, 100) """ This logic can be changed as rewards becomes more populated""" if random_int < 70: roll = database.get('Reward', str(randint(26, 48))) elif random_int < 90: roll = database.get('Reward', str(randint(17, 25))) elif random_int < 98: roll = database.get('Reward', str(randint(3, 16))) else: roll = database.get('Reward', str(randint(0, 2))) if roll is None: roll = database.get('Reward', '30') rewards['data'].append({ 'name': roll.name, 'img': roll.image, 'rarity': roll.rarity, 'id': roll.id }) return jsonify(rewards) data = request.get_json() user = database.get('User', data['user_id']) if database.duplicateUserReward(data['user_id'], data['reward_id']) is False: new_user_reward = UserReward(**data) new_user_reward.save() user.currency -= 30 user.save() print('this is the current currency {}'.format(user.currency)) return jsonify(user.to_json())
def user_rewards(): """ return all rewards associated with a user """ user = database.get('User', session['id']) return jsonify(user.get_user_rewards())
def jobs_applied(): """ Used to retrieve, add, update, and delete jobs that the user has applied to :return: A user's jobs_applied for successful GET requests, error on invalid requests, and status code otherwise """ user = database.get('User', session['id']) # GET: Return all jobs that user has applied to if request.method == 'GET': results = user.get_jobs_applied() return jsonify(results), 200 if request.is_json is False: return jsonify(error="Not a valid JSON"), 400 data = request.get_json() if request.method != 'POST': job_id = data.get('id') if ((request.method == 'DELETE' or request.method == 'PUT') and (job_id not in jobs)): response = {'error': 'Not a valid ID'} else: # PUT: Change an existing entry response = {'success': True } message = '' if request.method == 'PUT': token = 10 for key, value in data.items(): if key != 'id': if value == 'Rejection': message = 'Rejection is hard - Have some extra coins!' token += 20 if value == 'Received Offer': message = 'Congratulations!!!!' token += 50 jobs[job_id][key] = value user.currency += token # POST: Creates a new entry elif request.method == 'POST': # TODO create a method within user class to add jobs user.create_jobs_applied(**data) token = 30 user.currency += token # DELETE: Deletes an entry elif request.method == 'DELETE': token = 0 jobs.pop(job_id) # TODO: make sure to update how delete and put save the new data! there # is currently nothing right now user.save() response['token'] = token response['message'] = message # response['updated_jobs'] = jobs print('return value from applied', response) status = 200 if 'success' in response.keys() else 404 return jsonify(response), status
def csv(): """ returns a string csv of the jobs applied for a user """ user = database.get('User', session['id']) return user.get_csv()
def user_token(): """ returns the currency that a user has """ user = database.get('User', session['id']) return jsonify({'currency': user.currency})
def user_info(): """ return user info in order to populate user page """ user = database.get('User', session['id']) return jsonify(user.to_json())