Esempio n. 1
0
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']
Esempio n. 2
0
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)
Esempio n. 3
0
 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!")
Esempio n. 4
0
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']
Esempio n. 5
0
 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!")
Esempio n. 6
0
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']
Esempio n. 7
0
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']
Esempio n. 8
0
 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.'))
Esempio n. 9
0
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']
Esempio n. 10
0
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']
Esempio n. 11
0
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']
Esempio n. 12
0
def forbidden(e):
    response_data = Utils.create_response_error('Forbidden', 'Forbidden', 403)
    return jsonify(response_data), response_data['status_code']
Esempio n. 13
0
def bad_request(e):
    response_data = Utils.create_response_error('BadRequest', 'Bad request',
                                                400)
    return jsonify(response_data), response_data['status_code']
Esempio n. 14
0
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']