Esempio n. 1
0
def driver_to_db(data):
    if driver_exists(data['iddriver']):
        return 1
    else :
        driver = generate_driver(data['iddriver'])
        session.add(driver)
        session.commit()
        return 0
Esempio n. 2
0
def truck_position_to_db(data):
    truck_position = TruckPosition(
        iddriver=data['iddriver'],
        idtruck=data['idtruck'],
        status=data['status'],
        iditinerary=data['iditinerary'],
        timestamp=data['timestamp'],
        position=WKTElement(pos_to_string(data['position']), srid=4269)
    )
    session.add(truck_position)
    session.commit()
    return f'Truck position {truck_position.position} written successfully'
Esempio n. 3
0
def itinerary_to_db(data):
    if itinerary_exists(data['iditinerary']):
        return 1
    else:
        itinerary = Itinerary(
            iditinerary=data['iditinerary'],
            mission=data['mission'],
            departure=WKTElement(pos_to_string(data['departure']), srid=4326),
            arrival=WKTElement(pos_to_string(data['arrival']), srid=4326)
        )
        session.add(itinerary)
        session.commit()
        return 0
Esempio n. 4
0
def driver_to_db(data):
    def generate_driver(iddriver):
        firstname = names.get_first_name()
        lastname = names.get_last_name()
        return Driver(iddriver, firstname, lastname)

    def driver_exists(iddriver):
        q = session.query(Driver).filter_by(iddriver=iddriver).exists()
        return session.query(q).scalar()

    if driver_exists(data['iddriver']):
        return 1
    else:
        driver = generate_driver(data['iddriver'])
        session.add(driver)
        session.commit()
        return 0
Esempio n. 5
0
def itinerary_to_db(data):
    def pos_to_string(position):
        template = 'POINT(%f %f)'
        return template % (position[0], position[1])

    def itinerary_exists(iditinerary):
        q = session.query(Itinerary).filter_by(
            iditinerary=iditinerary).exists()
        return session.query(q).scalar()

    if itinerary_exists(data['iditinerary']):
        return 1
    else:
        itinerary = Itinerary(
            iditinerary=data['iditinerary'],
            mission=data['mission'],
            departure=WKTElement(pos_to_string(data['departure']), srid=4326),
            arrival=WKTElement(pos_to_string(data['arrival']), srid=4326))
        session.add(itinerary)
        session.commit()
        return 0
Esempio n. 6
0
 def tearDown(self):
     session.delete(DbTestCase.driver)
     session.commit()
     session.delete(DbTestCase.itinerary)
     session.commit()
     session.delete(DbTestCase.truck_position)
     session.commit()
Esempio n. 7
0
 def setUp(self):
     session.add(DbTestCase.driver)
     session.commit()
     session.add(DbTestCase.itinerary)
     session.commit()
     session.add(DbTestCase.truck_position)
     session.commit()
Esempio n. 8
0
def truck_position_to_db_missing_foreign(data):
    session.rollback()
    truck_position = TruckPosition(
        iddriver=data['iddriver'],
        idtruck=data['idtruck'],
        status=data['status'],
        iditinerary=data['iditinerary'],
        timestamp=data['timestamp'],
        position=WKTElement(pos_to_string(data['position']), srid=4269)
    )
    if not driver_exists(data['iddriver']):
        session.add(generate_driver(data['iddriver']))
    if not itinerary_exists(data['iditinerary']):
        itinerary = Itinerary(
            mission=data['mission'],
            departure=WKTElement(pos_to_string(data['departure']), srid=4326),
            arrival=WKTElement(pos_to_string(data['arrival']), srid=4326)
        )
        session.add(itinerary) 
    session.commit()
    session.add(truck_position)
    session.commit()
    return f'Truck position {truck_position.position} written successfully'
Esempio n. 9
0
from geoalchemy2 import *
from datetime import datetime
from db_utilis.initiate_db import session, Driver, Itinerary, TruckPosition

driver = Driver(firstname='Robin', lastname='Ambert')
session.add(driver)
session.commit()

depart = 'POINT(-126.4 45.32)'
arrivee = 'POINT(-124.4 42.32)'
itinerary = Itinerary(mission="Transport",
                      departure=WKTElement(depart, srid=4326),
                      arrival=WKTElement(arrivee, srid=4326))
session.add(itinerary)
session.commit()

position = 'POINT(-126.4 45.32)'

truck_position = TruckPosition(iddriver=2,
                               idtruck=1,
                               status="test",
                               iditinerary=2,
                               timestamp=datetime.now(),
                               position=WKTElement(position, srid=4269))

session.add(truck_position)
session.commit()