def user_posts(): """Retrieve all posts from a certain username. Checks are in place to check if the user is indeed a member of FedNet. This function uses the get_posts function below to actually retrieve the posts. Returns: JSON response that contains all posts of a certain user. """ username = request.args.get('username') if username is None or username == '': username = auth_username() if username is None: return bad_json_response("Bad request: Missing parameter 'username'.") # Check if user id exists. if not users.exists(username=username): return bad_json_response('User not found.') # Send no data in case the users are not friends. if username != get_jwt_identity() and is_friend(username) != 1: return good_json_response({'posts': {}}) return good_json_response({ 'posts': get_posts(username) })
def hobby(): """Get all hobby details from a certain user. Returns: JSON reponse with the hobby details from the hobbies table. """ username = request.args.get('username') if username is None or username == '': username = auth_username() if username is None: return bad_json_response("Bad request: Missing parameter 'username'.") # Extract all the needed data from the hobbies table in the database. hobbies_details = hobbies.export('id', 'title', username=username) hobbies_array = [ { 'id': item[0], 'title': item[1] } for item in hobbies_details ] return good_json_response({ 'hobbies': hobbies_array })
def delete(): """Delete a post. For a post to be deleted, the post ID is requested in the form. The post entry corresponding to the given post ID is then deleted from the posts table. Returns: Success JSON response if the operation succeeded. Else a failed JSON response is returned with the correct error message. """ username = get_jwt_identity() post_id = request.form['post_id'] if username is None: return bad_json_response("Bad request: Missing parameter 'username'.") if post_id is None: return bad_json_response("Bad request: Missing parameter 'post_id'.") if not users.exists(username=username): return bad_json_response('User not found') if not posts.exists(id=post_id): return bad_json_response('Post not found') # Check if the user is the post owner. post_username = posts.export_one('username', id=post_id) if post_username != username: return bad_json_response('Not your post') # Delete post. posts.delete(id=post_id) return good_json_response('success')
def delete_hobby(): """Delete a hobby entry from the hobbies table for a certain user. """ username = get_jwt_identity() id = request.form['id'] hobbies.delete(id=id) return good_json_response('success')
def add_hobby(): """Add a hobby to the hobbies table for a certain user. """ username = get_jwt_identity() title = request.form['title'] hobbies.insert(username=username, title=title) return good_json_response('success')
def timeline(): """This function handles the timeline, making sure you only see posts from the correct people. If you are friends with a certain user, that users posts will be shown in your timeline. Imported: get_friends function from api/data/friend.py Returns: JSON reponse that contains all the posts that are shown in the timeline. """ from app.api.data.friend import get_friends username = get_jwt_identity() # Check if user exists. if not users.exists(username=username): return bad_json_response('user not found') # Get the user's own posts. posts_array = get_posts(username) # Get the user's friends. friends = get_friends(username) for i in range(len(friends)): try: friend = friends[i]['username'] friend_address = get_user_ip(friend) # Get the posts of the friend. response = requests.get( friend_address + '/api/user/posts', params={ 'username': friend }, headers=request.headers ).json() if response['success']: posts = response['data']['posts'] posts_array = posts_array + posts except BaseException: continue posts_array = sorted( posts_array, key=lambda k: datetime.datetime.strptime(k['creation_date'], '%Y-%m-%d %H:%M:%S'), reverse=True ) return good_json_response({ 'posts': posts_array })
def forgotpassword(): username = request.form['username'] password = request.form['password'] if password is None: return bad_json_response("Bad request: Missing parameter 'password'.") new_password = sha256_crypt.encrypt(request.form['password']) users.update({'password': new_password}, username=username) return good_json_response('Succes')
def edit(): """Edit all your personal information and profile picture. All the correct tables are updated accordingly after an edit has been submitted. """ username = get_jwt_identity() if 'new_firstname' in request.form: new_firstname = request.form['new_firstname'] users.update({'firstname': new_firstname}, username=username) if 'new_lastname' in request.form: new_lastname = request.form['new_lastname'] users.update({'lastname': new_lastname}, username=username) if 'file' in request.files: image_filename = request.files['file'].filename image = request.files['file'].read() if image is not 0: uploads_id = save_file(image, filename=image_filename) if uploads_id is not False: users.update({'uploads_id': uploads_id}, username=username) if 'new_location' in request.form: new_location = request.form['new_location'] users.update({'location': new_location}, username=username) if 'new_study' in request.form: new_study = request.form['new_study'] users.update({'study': new_study}, username=username) if 'new_bio' in request.form: new_bio = request.form['new_bio'] users.update({'bio': new_bio}, username=username) if 'new_password' in request.form: new_password = sha256_crypt.encrypt(request.form['new_password']) users.update({'password': new_password}, username=username) if 'new_relationship_status' in request.form: new_relationship_status = request.form['new_relationship_status'] users.update({'relationship_status': new_relationship_status}, username=username) if 'new_phone_number' in request.form: new_phone_number = request.form['new_phone_number'] users.update({'phone_number': new_phone_number}, username=username) return good_json_response('success')
def delete(): """Delete a user from FedNet. The users details in the users table are deleted. """ username = get_jwt_identity() if users.exists(username=username): # Everything that belongs to user is deleted automatically. users.delete(username=username) return good_json_response({'user': username}) else: return bad_json_response('Username is not registered.')
def users_all(): """Get all usernames in the users table. Returns: JSON reponse that contains all the usernames in the users table. """ usernames = users.export('username') if len(usernames) == 0: return bad_json_response('No users found.') return good_json_response({ 'usernames': usernames })
def delete_comment(): """Delete an existing comment on a certain post. To delete a comment on a certain post, the post ID is requested in the form. The comment details corresponding to the given post ID are then deleted from the comments table in the database. Returns: Success JSON response if the operation succeeded. """ id = request.form['id'] comments.delete(id=id) return good_json_response('success')