def address(): """Get the address of a certain user. From the users and servers tables, necessary details are extracted from entries containing the given username. Returns: JSON response containing the address details of a certain user. If the user is not found or the server is non existant, a failed JSON response is returned. """ username = request.args.get('username') # If username is not given, use the logged in username. if username is None or username == '': username = auth_username() if username is None or username == '': return bad_json_response("Bad request: Missing parameter 'username'.") if users.exists(username=username): server_id = users.export_one('server_id', username=username) if not servers.exists(id=server_id): bad_json_response('Server is not registered.') name, address = servers.export_one('name', 'address', id=server_id) return good_json_response({ 'name': name, 'address': address, 'username': username }) else: return bad_json_response('User is not found.')
def language(): """Get all language details from a certain user. Returns: JSON reponse with the language details from the languages 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 language table in the database. language_details = languages.export('id', 'title', 'skill_level', username=username) language_array = [ { 'id': item[0], 'title': item[1], 'skill_level': item[2] } for item in language_details ] return good_json_response({ 'languages': language_array })
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 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 user(): """Get user details depending on friendship. If you are friends, sensitive data will be shown aswell. Returns: JSON reponse with the basic and sensitive user details. """ 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'.") # Export used details of the user. user_details = users.export( 'username', 'firstname', 'lastname', 'uploads_id', 'location', 'study', 'bio', 'creation_date', 'last_edit_date', 'relationship_status', 'phone_number', username=username ) if not user_details: return bad_json_response('User not found') # Check what the status of the friendship is between the users. friend_status = is_friend(username) if username == get_jwt_identity(): friend_status = 1 # Get image. up_id = user_details[0][3] imageurl = '../static/images/default.jpg' if friend_status == 1 and uploads.exists(id=up_id): filename = uploads.export_one('filename', id=up_id) imageurl = get_own_ip() + 'file/{}/{}'.format(up_id, filename) # Basic information visible if not friends. basic_info = { 'username': user_details[0][0], 'friend': friend_status, 'image_url': imageurl } if friend_status != 1: return good_json_response(basic_info) # All information visible if friends. sensitive_info = { 'firstname': user_details[0][1], 'lastname': user_details[0][2], 'location': user_details[0][4], 'study': user_details[0][5], 'bio': user_details[0][6], 'creation_date': str(user_details[0][7]), 'last_edit_date': str(user_details[0][8]), 'relationship_status': user_details[0][9], 'phone_number': user_details[0][10] } return good_json_response({**basic_info, **sensitive_info})