Exemplo n.º 1
0
 def get_messages(flight_id, lastMessageTime):
     if lastMessageTime is None:
         # Get all messages for the given flight
         sQuery = ("SELECT * FROM messages WHERE flight_id=? ORDER BY "
                   "time DESC")
     else:
         # Only get more recent messages since last push to client
         sQuery = ("SELECT * FROM messages WHERE flight_id=? AND time>" +
                   str(lastMessageTime) + " ORDER BY time DESC")
     with app.app_context():
         alive_counter = 0
         while True:  # TODO use blocking/events, not polling
             time2.sleep(push_poll)
             alive_counter = alive_counter + push_poll
             message_results = ApiFunctions.query_db(sQuery, [flight_id])
             if len(message_results) > 0:
                 lastMessageTime = message_results[0]['time']
                 # Format messages fo transport
                 messages = []
                 for row in message_results:
                     message = Message.Message(row['id'], row['body'],
                                               row['time'], flight_id)
                     messages.append(message.to_json())
                 # Break loop by returning the latest messages
                 # NB no need to use Flask's jsonify object here, as it adds
                 # lots of additional processing bloat for a Response object
                 # (which we do not use or need here for the stream).
                 return [
                     json.dumps({'messages': messages},
                                separators=(',', ':')),  # Minified
                     lastMessageTime
                 ]
             elif alive_counter >= keep_alive_poll:
                 raise IdleConnectionException()
Exemplo n.º 2
0
    def get_gates():
        gates = []
        results = ApiFunctions.query_db("SELECT * FROM gates")
        for row in results:
            gate = Gate(row['id'], row['name'], row['airport_id'])
            gates.append(gate.to_json())

        return jsonify({'gates': gates})
Exemplo n.º 3
0
    def get_users():
        users = []
        results = ApiFunctions.query_db("SELECT * FROM users")
        for row in results:
            user = User(row['id'])
            users.append(user.to_json())

        return jsonify({'users': users})
Exemplo n.º 4
0
    def get_airports():
        airports = []
        results = ApiFunctions.query_db("SELECT * FROM airports")
        for row in results:
            airport = Airport(row['id'], row['airport_short'],
                              row['airport_full'])
            airports.append(airport.to_json())

        return jsonify({'airports': airports})
Exemplo n.º 5
0
    def get_airline(airline_id):

        row = ApiFunctions.query_db("SELECT * FROM airlines WHERE id = ?",
                                    [airline_id], one=True)
        if row is None:
            abort(404)

        airline = Airline(row['id'], row['airline_short'], row['airline_full'])
        return jsonify({'airline': airline.to_json()})
Exemplo n.º 6
0
    def get_airlines():
        airlines = []
        results = ApiFunctions.query_db("SELECT * FROM airlines")
        for row in results:
            airline = Airline(row['id'], row['airline_short'],
                              row['airline_full'])
            airlines.append(airline.to_json())

        return jsonify({'airlines': airlines})
Exemplo n.º 7
0
    def get_messages():
        messages = []
        results = ApiFunctions.query_db("SELECT * FROM messages")
        for row in results:
            message = Message(row['id'], row['body'], row['time'],
                              row['flight_id'])
            messages.append(message.to_json())

        return jsonify({'messages': messages})
Exemplo n.º 8
0
    def get_flight_gates():
        flight_gates = []
        results = ApiFunctions.query_db("SELECT * FROM flight_gates")
        for row in results:
            flight_gate = FlightGate(row['id'], row['gate_id'],
                                     row['flight_id'])
            flight_gates.append(flight_gate.to_json())

        return jsonify({'flight_gates': flight_gates})
Exemplo n.º 9
0
    def get_user_messages(user_id):

        messages = []

        # Get the flight ids from ListeningUsers
        flights = ApiFunctions.query_db("SELECT * FROM listening_users WHERE user_id=?", [user_id]);

        for row in flights:
            flight_id = row['flight_id'];

            # Get all the messages that correspond to this flight_id
            message_results = ApiFunctions.query_db("SELECT * FROM messages where flight_id=?", [flight_id]);

            # Add all the messages from that flight to our list
            for row in message_results:
                message = Message.Message(row['id'], row['text'], row['time'], row['flight_id'])
                messages.append(message.to_json())

        return jsonify({'messages': messages})
Exemplo n.º 10
0
    def get_message(message_id):

        row = ApiFunctions.query_db("SELECT * FROM messages WHERE id = ?",
                                    [message_id], one=True)
        if row is None:
            abort(404)

        message = Message(row['id'], row['body'], row['time'],
                          row['flight_id'])
        return jsonify({'message': message.to_json()})
Exemplo n.º 11
0
    def find_flight(gate):
        row = ApiFunctions.query_db("SELECT * FROM flights WHERE gate = ?",
                                    [gate],
                                    one=True)
        if row is None:
            abort(404)

        airline = ApiFunctions.query_db(
            "SELECT airline_short, airline_full FROM airlines WHERE id = ?",
            [row['airline_id']],
            one=True)
        flight = DisplayFlight.DisplayFlight(row['id'],
                                             airline['airline_full'],
                                             airline['airline_short'],
                                             row['dest_airport'],
                                             row['flight_number'], row['gate'],
                                             row['departure'])

        return jsonify({'flights': [flight.to_json()]})
Exemplo n.º 12
0
    def get_flights():
        flights = []
        results = ApiFunctions.query_db(
            "SELECT * FROM flights WHERE departure > strftime('%s', 'now') ORDER BY departure"
        )
        for row in results:
            airline = ApiFunctions.query_db(
                "SELECT airline_short, airline_full FROM airlines WHERE id = ?",
                [row['airline_id']],
                one=True)
            flight = DisplayFlight.DisplayFlight(row['id'],
                                                 airline['airline_full'],
                                                 airline['airline_short'],
                                                 row['dest_airport'],
                                                 row['flight_number'],
                                                 row['gate'], row['departure'])

            flights.append(flight.to_json())

        return jsonify({'flights': flights})
Exemplo n.º 13
0
    def create_flight_gate():
        if not request.json or not 'gate_id' in request.json or not 'flight_id' in request.json:
            abort(400)

        gate_id = request.json['gate_id']
        flight_id = request.json['flight_id']
        result = ApiFunctions.post_db(
            "INSERT INTO flight_gates VALUES (NULL, ?, ?)",
            [gate_id, flight_id])
        inserted_id = c.lastrowid
        print u'Inserted new flight_gate at row ' + str(inserted_id)
        return jsonify({'id': str(inserted_id)}), 201
Exemplo n.º 14
0
    def find_gates():
        if not request.json or not 'id' in request.json:
            abort(400)

        airport_id = request.json['id']
        gates = []
        results = ApiFunctions.query_db(
            "SELECT * FROM gates where airport_id=?", [airport_id])
        for row in results:
            gate = Gate(row['id'], row['name'], row['airport_id'])
            gates.append(gate.to_json())

        return jsonify({'gates': gates})
Exemplo n.º 15
0
    def get_flight_messages(flight_id):
        try:
            message_results = ApiFunctions.query_db(
                "SELECT * FROM messages WHERE flight_id=? ORDER BY time DESC",
                [flight_id])

            messages = []
            for row in message_results:
                message = Message.Message(row['id'], row['body'], row['time'],
                                          flight_id)
                messages.append(message.to_json())
            return jsonify({'messages': messages})
        except Exception as e:
            return jsonify({'err': str(e)})
Exemplo n.º 16
0
    def create_airport():
        if not request.json or not 'airport_short' in request.json or not 'airport_full' in request.json:
            abort(400)

        airport_short = request.json['airport_short']
        airport_full = request.json['airport_full']

        result = ApiFunctions.post_db(
            "INSERT INTO airports VALUES (NULL, ?, ?)",
            [airport_short, airport_full])
        inserted_id = c.lastrowid

        print u'Inserted new airport at row ' + str(inserted_id)
        return jsonify({'id': str(inserted_id)}), 201
Exemplo n.º 17
0
    def create_gate():
        if not request.json or not 'name' in request.json or not 'airport_id' in request.json:
            abort(400)

        name = request.json['name']
        airport_id = request.json['airport_id']

        if not name:
            abort(400)

        result = ApiFunctions.post_db("INSERT INTO gates VALUES (NULL, ?, ?)",
                                      [name, airport_id])
        inserted_id = c.lastrowid
        print u'Inserted new gate at row ' + str(inserted_id)
        return jsonify({'id': str(inserted_id)}), 201
Exemplo n.º 18
0
    def create_flight_message(flight_id):
        if not request.json or not 'body' in request.json or not 'time' in request.json:
            abort(400)

        body = request.json['body']
        time = request.json['time']

        if not body:
            abort(400)

        result = ApiFunctions.post_db(
            "INSERT INTO messages VALUES (NULL, ?, ?, ?)",
            [body, time, flight_id])
        inserted_id = c.lastrowid

        return jsonify({'id': str(inserted_id)}), 201
Exemplo n.º 19
0
    def create_flight():
        if not request.json or not 'airline_id' in request.json or not 'dest_airport' in request.json or not 'flight_number' in request.json or not 'gate' in request.json or not 'departure' in request.json:
            abort(400)

        airline_id = request.json['airline_id']
        dest_airport = request.json['dest_airport']
        flight_number = request.json['flight_number']
        gate = request.json['gate']
        departure = request.json['departure']

        if not airline_id or not dest_airport or not gate:
            abort(400)

        result = ApiFunctions.post_db(
            "INSERT INTO flights VALUES (NULL, ?, ?, ?, ?, ?)",
            [airline_id, dest_airport, flight_number, gate, departure])
        inserted_id = c.lastrowid

        print u'Inserted new flight at row ' + str(inserted_id)
        return jsonify({'id': str(inserted_id)}), 201
Exemplo n.º 20
0
 def create_user():
     result = ApiFunctions.post_db("INSERT INTO users VALUES (NULL)");
     inserted_id = c.lastrowid
     user = User(id)
     print u'Inserted new user at row ' + str(inserted_id)
     return jsonify({'id': str(inserted_id)}), 201
Exemplo n.º 21
0
 def delete_message(message_id):
     ApiFunctions.post_db("DELETE FROM messages WHERE id=?", [message_id])
     print u'Deleted message with ID ' + str(inserted_id)
     return jsonify({'result': True})
Exemplo n.º 22
0
    def delete_airport(airport_id):

        ApiFunctions.post_db("DELETE FROM airports WHERE id=?", [airport_id])
        print u'Deleted airport with ID ' + str(inserted_id)
        return jsonify({'result': True})
Exemplo n.º 23
0
 def delete_flight_gate(flight_gate_id):
     ApiFunctions.post_db("DELETE FROM flight_gates WHERE id=?",
                          [flight_gate_id])
     print u'Deleted listening_user with ID ' + str(inserted_id)
     return jsonify({'result': True})
Exemplo n.º 24
0
 def delete_user(user_id):
     ApiFunctions.post_db("DELETE FROM users WHERE id=?", [user_id])
     print u'Deleted user with ID ' + str(inserted_id)
     return jsonify({'result': True})
Exemplo n.º 25
0
import requests
import json
import ApiFunctions as api

apiUrl = " https://us-central1-marcy-playground.cloudfunctions.net/ordoroCodingTest"
r = requests.get(apiUrl)
if not r.ok:
    raise Exception("url down {}".format(apiUrl))
data = r.json()['data']

users = {}
for access in data:
    api.registerLogin(access, users)

emails = api.emailList(users)
domains = api.usersPerDomain(users)
aprilBois = api.aprilUsers(users)

my_response = {
    "your_email_address": "*****@*****.**",
    "unique_emails": emails,
    "user_domain_counts": domains,
    "april_emails": aprilBois
}

r = requests.post(apiUrl, json=my_response)
print("Submitted {}".format(json.dumps(my_response)), "\n")
print("Post Response {}".format(r), "\n")
Exemplo n.º 26
0
 def delete_flight(flight_id):
     ApiFunctions.post_db("DELETE FROM flights WHERE id=?", [flight_id])
     print u'Deleted flight with ID ' + str(inserted_id)
     return jsonify({'result': True})