def add_member_to_group(group_id): log("Adding member to group...") user_id = request.json.get('user_id') user_email = request.json.get('email') log("Going to check e-mail and user id...") if user_email != "" and user_email is not None and Utils.email_is_valid( user_email): user = User.get_by_email(user_email) if user is not None: log("Email: Adding {} to group {}".format(user_email, group_id)) Group.add_member(group_id, user.id) else: invite = Invite.create(user_email, g.user.id) invite.save() invite.send() else: if user_id != "" and user_id is not None: log("ID: Adding {} to group {}".format(user_id, group_id)) Group.add_member(group_id, user_id) else: response_data = Utils.create_response_error( "InternalServerError", "The server could not fulfil your request", 500) return jsonify(response_data), response_data['status_code'] response_data = Utils.create_response_data( Group.get_by_id(group_id).to_dict(), 200) return jsonify(response_data), response_data['status_code']
def login_user(): email = request.json.get('email') password = request.json.get('password') if not (email or password): response_data = Utils.create_response_error( 'EmptyEmailOrPassword', 'The email or password is empty', 409) return jsonify(response_data) try: user = User.login(email, password) except User.IncorrectEmailOrPassword: response_data = Utils.create_response_error( 'IncorrectEmailOrPassword', 'The email or password is incorrect', 409) return jsonify(response_data) except User.UserNotExists: response_data = Utils.create_response_error( 'UserNotExists', 'The user was not found in the database!', 409) return jsonify(response_data) g.user = user response_data = Utils.create_response_data(user.to_dict(), 200) return jsonify(response_data)
def test_create_response_data(self): data = Utils.create_response_data("Test", 500) self.assertEqual(data['data'], "Test", "Data created via `create_response_data` is not equal to input data!") self.assertEqual(data['status_code'], 500, "Status code created via `create_response_data` is not equal to input status code!")
def create_group(): group_id = request.json.get('group_id') name = request.json.get('name') group = Group.create(group_id=group_id, creator=g.user.id, name=name) group.save() response_data = Utils.create_response_data(group.to_dict(), 200) return jsonify(response_data), response_data['status_code']
def test_create_response_error(self): data = Utils.create_response_error("InternalServerError", "The Server could not fulfil your request", 500) self.assertEqual(data['error']['name'], "InternalServerError", "Error name created via `create_response_error` is not equal to input!") self.assertEqual(data['error']['message'], "The Server could not fulfil your request", "Error message created via `create_response_error` is not equal to input!") self.assertEqual(data['status_code'], 500, "Status code created via `create_response_error` is not equal to input status code!")
def register_user(): email = request.json.get('email') password = request.json.get('password') if not Utils.email_is_valid(email): response_data = Utils.create_response_error('InvalidEmail', 'This email is invalid', 409) return jsonify(response_data) if not password: response_data = Utils.create_response_error( 'InvalidPassword', 'This password is invalid', 409) return jsonify(response_data) try: user = User.register(email, password) except User.EmailAlreadyInUse: response_data = Utils.create_response_error( 'UsedEmail', 'This email is already in use', 409) return jsonify(response_data) user.save() g.user = user # Create a Friends default group for the user # This group has the same id as the user id friends_group = Group.create(group_id=user.id, name="Friends", creator=user.id) friends_group.save() response_data = Utils.create_response_data(user.to_dict(), 200) return jsonify(response_data), response_data['status_code']
def confirm(token): log("Starting confirmation...") invite = Invite.get_by_token(token) log("Inviter ID: {}".format(invite.inviter_id)) inviter = User.get_by_id(invite.inviter_id) log("Invited by: {}".format(inviter.email)) try: return render_template('invite.html', email=invite.email, token=token, inviter_email=inviter.email), 200 except jinja2.TemplateNotFound: response_data = Utils.create_response_error( "InternalServerError", "The server could not display the template", 500) return jsonify(response_data), response_data['status_code']
def test_email_is_valid(self): self.assertTrue(Utils.email_is_valid('*****@*****.**')) self.assertFalse(Utils.email_is_valid('test123')) self.assertFalse(Utils.email_is_valid('test123@gmail')) self.assertFalse(Utils.email_is_valid('test123@gmail.'))
def internal_server_error(e): response_data = Utils.create_response_error( 'InternalServerError', 'The server could not fulfill the request', 500) return jsonify(response_data), response_data['status_code']
def method_not_allowed(e): response_data = Utils.create_response_error( 'MethodNotAllowed', 'The method is not allowed for the requested URL', 405) return jsonify(response_data), response_data['status_code']
def page_not_found(e): response_data = Utils.create_response_error('PageNotFound', 'Sorry, nothing at this URL', 404) return jsonify(response_data), response_data['status_code']
def forbidden(e): response_data = Utils.create_response_error('Forbidden', 'Forbidden', 403) return jsonify(response_data), response_data['status_code']
def bad_request(e): response_data = Utils.create_response_error('BadRequest', 'Bad request', 400) return jsonify(response_data), response_data['status_code']
def activate_invite(token): password = request.form['password'] Invite.activate(token, password) response_data = Utils.create_response_data("Success!", 200) return jsonify(response_data), response_data['status_code']