def create(user): """ This function allows user to login input = { "number" : number - string } output = {'spam' : spam details} """ if not request.is_json or 'number' not in request.get_json(): return bad_request('Missing required data.') try: return add_spam(user, request) except: return bad_request(error_messages['cannot_report'])
def login_user(request): try: if not request.get_json()['phone_number'].isdigit(): return bad_request('wrong arguments') user = find_user(request.get_json()['phone_number'], login=True) if user is not None and (user.password == hash_string( request.get_json()['password'])): #create token token = generate_token(user) return jsonify({'token': token}), 201 else: return jsonify(not_found( error_messages['invalid_credentials'])), 404 except Exception: return bad_request(error_messages['invalid_credentials'])
def create_user(): """ This function allows user to create their account input = { "name": user name - string, "phone_number": number - string - unique, "email": email address - string - optional, "password": password - password } output = {user : user details} """ if not request.is_json or 'name' not in request.get_json( ) or 'phone_number' not in request.get_json( ) or 'password' not in request.get_json(): return bad_request('Missing required data.') try: return add_user(request) except: return bad_request(error_messages['user_exist'])
def search(user, param): """ This function allows user to search a user or row in Global database input = search string output = {'token' : [user or list of similar rows in Global database]} errors = param string is too short | no user found """ if len(param) <= 2: return bad_request(error_messages['too_short']) return search_user(param.lower(), user)
def login(): """ This function allows user to login input = { 'phone_number' : 'number - string' , 'password' : 'password - string' } output = {'token' : 'JWT token'} """ if not request.is_json or 'phone_number' not in request.get_json( ) or 'password' not in request.get_json(): return bad_request('Missing required data.') return login_user(request)
def add_spam(user, request): if not request.get_json()['number'].isdigit(): return bad_request('wrong arguments') reportedUser = find_user(request.get_json()['number'], spam=True) if reportedUser is not None: spam = Spam(user.id, request.get_json()['number'], reportedUser.id) else: spam = Spam(user.id, request.get_json()['number'], None) db.session.add(spam) db.session.commit() return jsonify({'spam': spam.serialize}), 201
def sync(user): """ This function is only accessible to admin user to upadate spam likelihood in global database """ if (user.role is not user_roles['admin']): return bad_request(error_messages['unauthorized_access']) try: update_spam_likelihood() return jsonify({'task': error_messages['completed']}), 200 except: return jsonify({'task': error_messages['something_went_wrong']})
def add_user(request): if request.get_json()['email'] != "" and not re.search( email_regex, request.get_json()['email']) or not request.get_json( )['phone_number'].isdigit(): return bad_request('wrong arguments') user = Users(request.get_json()['name'].lower(), request.get_json()['email'], request.get_json()['phone_number'], hash_string(request.get_json()['password'])) globalDB = GlobalDB(request.get_json()['name'].lower(), request.get_json()['email'], request.get_json()['phone_number'], 0, isUser=globaldb_is_user['user']) db.session.add(user) db.session.add(globalDB) db.session.commit() return jsonify({'user': user.serialize}), 201