Ejemplo n.º 1
0
def create(mongo, data=None):
    data = form_to_dict(data)
    if not user_controller.user_exists(mongo, {'username': data['user']}):
        return ERR.USER_NOT_FOUND

    mongo.db[MONGO.REMINDERS].insert(data)

    return RESP.REMINDER_CREATED
Ejemplo n.º 2
0
def create(mongo, data=None):
    data = form_to_dict(data)
    if not user_controller.user_exists(mongo, {'username': data['user']}):
        return ERR.USER_NOT_FOUND

    mongo.db[MONGO.REMINDERS].insert(data)

    return RESP.REMINDER_CREATED
Ejemplo n.º 3
0
def location():
    user_id = request.headers.get('Authorization')

    if user_id is not None and user_exists(user_id):
        data = request.json

        err_msg = None
        if 'latitude' not in data:
            err_msg = 'Mandatory field "latitude" not present!'
        elif 'longitude' not in data:
            err_msg = 'Mandatory field "longitude" not present!'

        if err_msg is not None:
            return jsonify({"error": err_msg}), 400

        #get user fligths
        r = requests.get(url_user_flight + "flight-users/" + user_id)
        relations = r.json()

        results = []
        for rel in relations:
            #get flight destAirport
            r = requests.get(url_flight + "flights/" + rel['flightNumber'])
            res = r.json()
            destAirport = res['arrAirport']
            format_parse = '%Y-%m-%dT%H:%M:%S.%fZ'
            timeArr = datetime.datetime.strptime(res['arrTime'], format_parse)

            data = {
                'airport': destAirport,
                'latitude': data['latitude'],
                'longitude': data['longitude']
            }

            r = requests.post(url_calculate_route + 'route', json=data)
            resp = r.json()
            time = datetime.timedelta(seconds=resp['duration'])
            dist = resp['distance'] / 1000  # Km

            depart = timeArr - time
            if depart < datetime.datetime.now():  # in case user asks info late
                depart = 'now'
            else:
                depart = depart.strftime('%H:%M')
            time = ':'.join(str(time).split(':')[:2])

            data = {
                'distance': dist,
                'destAirport': destAirport,
                'timeNeeded': time,
                'depart': depart
            }
            results.append(data)

        return jsonify(results), 200
    else:
        return jsonify({"error": "Not a valid user!"}), 401
Ejemplo n.º 4
0
def find(airport):
    user_id = request.headers.get('Authorization')

    if user_id is not None and user_exists(user_id):
        r = requests.get(url_search_flight + 'flights/' + airport)
        resp = r.json() if r.status_code == 200 else {
            'error': 'Invalid airport!'
        }
        return jsonify(resp), r.status_code
    else:
        return jsonify({"error": "Not a valid user!"}), 401
Ejemplo n.º 5
0
def signup():
    if request.method == 'POST':
        username = request.form['name']
        password = request.form['password']
        error = None

        if not user_exists(username):
            create_user(username,password)
        else:
            error = 'Username already exists.'

        if  error is None:
            return redirect(url_for('login'))

        flash(error)


    return render_template('signup.html')
Ejemplo n.º 6
0
def track():
    data = request.json
    user_id = request.headers.get('Authorization')

    if user_id is not None and user_exists(user_id):
        if 'flightNumber' not in data:
            err_msg = 'Mandatory field "flightNumber" not present!'
            return jsonify({"error": err_msg}), 400

        data = {'userId': user_id, 'flightNumber': data['flightNumber']}
        r = requests.post(url_register_flight + 'userflight', json=data)
        resp = {
            'msg': 'Inserted correctly'
        } if r.status_code == 201 else {
            'error': 'Flight not found!'
        }
        return jsonify(resp), r.status_code
    else:
        return jsonify({"error": "Not a valid user!"}), 401
Ejemplo n.º 7
0
def register():
    data = request.json

    err_msg = None
    if 'id' not in data:
        err_msg = 'Mandatory field "id" not present!'
    elif 'username' not in data:
        err_msg = 'Mandatory field "username" not present!'
    elif 'firstName' not in data:
        err_msg = 'Mandatory field "firstName" not present!'
    elif 'chatId' not in data:
        err_msg = 'Mandatory field "chatId" not present!'

    if err_msg is not None:
        return jsonify({"error": err_msg}), 400

    # call service, check id
    if user_exists(data['id']):
        update_user(data['id'], data)
    else:
        register_user(data)
    return jsonify({"msg": "OK"}), 200
Ejemplo n.º 8
0
def update_password():
    if request.method == 'POST':

        username = request.form['username']
        prev_password = request.form['password']
        new_password = request.form['new_password']
        verified_new_password = request.form['verify_new_password']

        error = None
        if(new_password == verified_new_password):
            if user_exists(username):
                update_user(username,prev_password,new_password)
            else:
                error = 'User doesnt exist.'
        else:
            error = 'Passwords do not match'

        if  error is None:
            return redirect(url_for('login'))

        flash(error)


    return render_template('update-password.html')