Ejemplo n.º 1
0
def sign_up(): #email, password, firstname, familyname, gender, city, country
    email = request.form['signupUsernameInput']
    password = request.form['signupPasswordInput']
    firstName = request.form['signupFirstnameInput']
    familyName = request.form['signupLastnameInput'] 
    gender = request.form['signupGenderInput']
    city = request.form['signupCityInput']
    country = request.form['signupCountryInput']

    #check is user already exists
    userExists = database_helper.user_exists(email)
    if (userExists == False):
        #hash password
        hashedPassword, salt = hashPw(password)
        #insert new user into db
        database_helper.insert_user(email, hashedPassword, salt, firstName, familyName, gender, city, country)
        #check if user was corretly inserted into the db
        if (database_helper.user_exists(email) == True):
            #get token
            token = get_unique_token()
            #add token and mail to active users list
            active_users[token] = email
            return json.dumps({"success": "true", "message": "Sign up successful.", "data": token})
        else:
            return json.dumps({"success": "false", "message": "Inserting the user into the database failed."})
    else:
        return json.dumps({"success": "false", "message": "User already exists."})  
Ejemplo n.º 2
0
def get_user_data_by_email(email, token):
    token_email = dh.email_from_token(token)
    if token_email[0] is None:
        return json.dumps({"message": "User not logged in"}), 501
    if dh.user_exists(email) is None:
        return json.dumps({"message": "No such user"}), 501
    user_data = dh.get_user_data(email, True)
    return json.dumps({"message": "User data", "data": user_data}), 200
Ejemplo n.º 3
0
def sign_up(email, password, firstname, lastname, gender, city, country):

	if db.user_exists(email):
		return {'status' : 'Fail', 'msg' : 'User already exists.'}
		
	if not valid_user_data(email, password, firstname, lastname, gender, city, country):
		return {'status' : 'Fail', 'msg' : 'Invalid user data.'}

	db.create_user(email, password, firstname, lastname, gender, city, country)
	return {'status' : 'Success', 'msg' : 'User registered.'}
Ejemplo n.º 4
0
def remove_user():
    data = request.get_json()
    email = data['email']

    if (database_helper.user_exists(email)):
        result = database_helper.delete_user(email)
        if (result):
            return create_response(True, 'Successfully removed user')
    else:
        return create_response(False, 'No such user')
    return create_response(False, 'Failed to remove user')
Ejemplo n.º 5
0
def get_user_data_by_token():
    #data = request.get_json()

    if 'Authorization' in request.headers:
        a_token = request.headers.get('Authorization')

    email = database_helper.token_to_email(a_token)

    if (not database_helper.user_logged_in(a_token)):
        return create_response(False, 'You are not logged in')
    elif (database_helper.user_exists(email) == False):
        return create_response(False, 'No such user')

    result = database_helper.get_user_data(email)
    return create_response(True, "User data retrieved", result)
Ejemplo n.º 6
0
def get_user_messages_by_email():
    if request.method == 'POST':
        token = request.form['token']
        email = request.form['email']
        if database_helper.is_logged_in(token):
            messages = database_helper.get_user_messages_by_email(email)
            if database_helper.user_exists(email):
                return json.dumps({
                    'success': True,
                    'message': '''Messages retrieved successfully''',
                    'messages': messages
                })
        return json.dumps({
            'success': False,
            'message': '''There is no user with that email'''
        })
Ejemplo n.º 7
0
def sign_in():
    if request.method == 'POST':

        data = request.json
        email = data['username']
        password = data['password']

        exist = database_helper.user_exists(email=email, password=password)
        if exist:
            if id_socket.has_key(str(email)):
                # Already connected on other device
                disconnect(email)
            return connect(email)
        else:
            return json.dumps({'success': False, 'message': 'User is not in the database', 'data': ''})
    else:
        return json.dumps({'success': False, 'message': 'Not a POST method', 'data': ''})
Ejemplo n.º 8
0
def sign_up(email=None,
            password=None,
            firstname=None,
            familyname=None,
            gender=None,
            city=None,
            country=None):
    if dh.user_exists(email):
        return json.dumps({"message":
                           "User with given email already exists"}), 501
    if not email or not password or not firstname or not familyname or not gender or not city or not country:
        return json.dumps({"message": "Incomplete sign up form"}), 501
    if len(password) < 5:
        return json.dumps({"message": "Password too short"}), 501
    dh.create_new_user(email, password, firstname, familyname, gender, city,
                       country)
    return json.dumps({"message": "You are now signed up"}), 200
Ejemplo n.º 9
0
def user_get_messages_token():
    data = request.get_json()

    if 'Authorization' in request.headers:
        a_token = request.headers.get('Authorization')

    email = database_helper.token_to_email(a_token)
    print(email)

    if (not database_helper.user_logged_in(a_token)):
        return create_response(False, 'You are not logged in')
    elif (not database_helper.user_exists(email)):
        return create_response(False, "No such user")
    else:
        data = database_helper.get_messages_by_email(a_token, email)
        if (data is not False):
            return create_response(True, "User messages retrieved", data)
    return create_response(False, "Something went wrong")
Ejemplo n.º 10
0
def get_user_data_by_email():
    data = request.get_json()
    email = data['email']

    if 'Authorization' in request.headers:
        a_token = request.headers.get('Authorization')

    if (not database_helper.user_logged_in(a_token)):
        return create_response(False, 'You are not logged in')
    elif (database_helper.user_exists(email) == False):
        return create_response(False, 'No such user')

    database_helper.add_view(email)
    result = database_helper.get_user_data(email)
    #print(pageviews[email])
    #pageviews[email] = pageviews[email] + 1

    return create_response(True, "User data retrieved", result)
Ejemplo n.º 11
0
def user_post_message():
    data = request.get_json()
    message = data['message']
    email = data['email']

    if 'Authorization' in request.headers:
        a_token = request.headers.get('Authorization')

    sender = database_helper.token_to_email(a_token)

    if (not database_helper.user_logged_in(a_token)):
        return create_response(False, 'You are not logged in')
    elif (not database_helper.user_exists(email)):
        return create_response(False, "No such user")
    else:
        result = database_helper.post_message(email, sender, message)
        if (result):
            return create_response(True, "Message posted")
    return create_response(False, "Something went wrong")
Ejemplo n.º 12
0
def get_user_data_by_email():
    if request.method == 'POST':
        token = request.form['token']
        email = request.form['email']
        if database_helper.user_exists(email) == False:
            return json.dumps({
                'success': False,
                'message': '''Email doesn't exist'''
            })
        if database_helper.is_logged_in(token):
            user_data = database_helper.get_user_data_by_email(email)
            return json.dumps({
                'success': True,
                'message': 'Userdata retrieved',
                'email': user_data[0],
                'firstname': user_data[1],
                'familyname': user_data[2],
                'gender': user_data[3],
                'city': user_data[4],
                'country': user_data[5]
            })
        return json.dumps({'success': False, 'message': ''})
Ejemplo n.º 13
0
def post_message():
    if request.method == 'POST':
        token = request.form['token']
        message = request.form['message']
        email = request.form['email']
        sender = database_helper.get_email(token)
        if sender == False:
            return json.dumps({
                'success': False,
                'message': '''Token doesn't exists'''
            })
        if database_helper.user_exists(email) == False:
            return json.dumps({
                'success': False,
                'message': '''Receiver does not exists'''
            })
        result = database_helper.post_message(sender, message, email)
        if result:
            return json.dumps({
                'success': True,
                'message': '''Message posted successfully'''
            })
        return json.dumps({'success': False, 'message': '''Invalid message'''})
Ejemplo n.º 14
0
def sign_up():
    if request.method == 'POST':
        email = request.json['email']
        password = request.json['password']
        firstname = request.json['firstname']
        familyname = request.json['familyname']
        gender = request.json['gender']
        city = request.json['city']
        country = request.json['country']

        exists = database_helper.user_exists(email=email, password=password)
        if exists:
            return json.dumps({'success': False, 'message': 'User already exists', 'data': ''})
        else:
            result = json.loads(
                    database_helper.insert_user(email, password, firstname, familyname, gender, city, country))
            # user added to the database
            if result['success']:
                return connect(email)
            # user hasn't been added
            else:
                return json.dumps({'success': False, 'message': result['message'], 'data': ''})
    else:
        return json.dumps({'success': False, 'message': 'Not a POST method', 'data': ''})
Ejemplo n.º 15
0
def get_user_data_by_token(token):
    email = dh.email_from_token(token)
    if dh.user_exists(email[0]) is None:
        return json.dumps({"message": "No such user"}), 501
    user_data = dh.get_user_data(email[0], False)
    return json.dumps({"message": "User data", "data": user_data}), 200