示例#1
0
    def put(station_id):
        """
        Update a station that is registered in the database.
        :param: code: Code of the origin station (i.e. FRADI) - Default is the station's one
                name: Code of the destination station (i.e. FRAFJ) - Default is the station's one
        :return: A JSON file of the station newly updated.
        """

        # station parser
        station_parser = reqparse.RequestParser()
        station_parser.add_argument(
            name='code',
            type=str,
            help="The  code of the station (i.e. FRAFJ)")
        station_parser.add_argument(name='name',
                                    type=str,
                                    help="The name of the station")
        station_args = station_parser.parse_args()

        station = dbStation.objects(id=station_id).first()

        # get default values
        if station_args['code'] is None:
            station_args['code'] = station.code
        if station_args['name'] is None:
            station_args['name'] = station.name

        dbStation.objects(id=station_id).update_one(
            set__code=station_args['code'], set__name=station_args['name'])
        return json.loads(
            dbStation.objects(id=station_id).first().to_json()), 200
示例#2
0
 def delete(station_id):
     """
     Delete from the database a single station registered in the database.
     :param: request_id: Id of the station to delete
     :return: 204.
     """
     dbStation.objects(id=station_id).delete()
     return "", 204
示例#3
0
def get_trains_data(date, origin_name, destination_name):
    print("Getting response...")
    origin_code = Station.get_code_by_name(origin_name)
    destination_code = Station.get_code_by_name(destination_name)
    trains = Travel.search(date, origin_code, destination_code)
    trains_data = []
    for train in trains:
        trains_data.append(get_train_data(train))
    return trains_data
示例#4
0
 def get(station_id):
     """
     Get a single station registered in the database.
     :param: station_id: Id of the station to get
     :return: A JSON file of the station.
     """
     if dbStation.objects(id=station_id).first() is not None:
         return json.loads(
             dbStation.objects(id=station_id).first().to_json()), 200
     else:
         return "Station not found at this id {}".format(station_id), 404
示例#5
0
def train_to_string(train_data):
    duration = str(train_data["minuteDuration"]//60) + \
        "h"+str(train_data["minuteDuration"] % 60)
    string = "Train ("+",".join(train_data["type"])+") numero "+",".join(train_data["numbers"])+":\n"+"Depart de "+Station.get_name_by_code(train_data["origin_code"])+" : " + \
        train_data["departureDate"]+"\n"+"Arrivee a "+Station.get_name_by_code(
            train_data["destination_code"])+" : " + train_data["arrivalDate"]+"\n"+"Duree : "+duration+"\n"+"Prix :\n"
    for price in train_data["prices"]:
        string += str(price[1]) + " restants à " + str(price[0]) + "e\n"
    # print(string)
    return string
示例#6
0
    def post():
        """
        Add a NEW station that will be registered in the database.
        :param: Arguments of the POST request.
                code: Code of the station (i.e. FRAFJ) - Required
                name: Name of the station - Required
        :return: A JSON file of the station newly registered.
        """

        # stations parser
        stations_parser = reqparse.RequestParser()
        stations_parser.add_argument(
            name='code',
            type=str,
            required=True,
            help="The  code of the station (i.e. FRAFJ)")
        stations_parser.add_argument(name='name',
                                     type=str,
                                     required=True,
                                     help="The name of the station")
        stations_args = stations_parser.parse_args()

        # checks if the station already exists in the database
        station_exist = dbStation.objects(
            code=stations_args['code'],
            name=stations_args['name']).first() is not None

        if station_exist:
            station_id = dbStation.objects(
                code=stations_args['code'],
                name=stations_args['name']).first().id
            return "The station already exists at id {}".format(
                station_id), 208
        else:
            station = dbStation(code=stations_args['code'],
                                name=stations_args['name'])
            station.save()
            return json.loads(station.to_json()), 201
示例#7
0
 def search(date: DateTime, origin_code: str, destination_code: str) \
         -> list:
     """
     Search for trains
     :param date: date to search, datetime object
     :param origin_code: code of the origin (i.e. FRXXX)
     :param destination_code: code of the destination (i.e. FRXXX)
     :return: list of available trains
     """
     origin = {
         "code": origin_code,
         "name": Station.get_name_by_code(origin_code)
     }
     destination = {
         "code": destination_code,
         "name": Station.get_name_by_code(destination_code)
     }
     response_json = Travel._query(date, origin, destination).json()
     if "trainProposals" in response_json:
         trains = response_json["trainProposals"]
         return trains
     else:
         raise ValueError('Bad response from request')
示例#8
0
    def save_to_database(self):
        list_of_proposition = []
        for proposition in self.propositions:
            p = Proposition(type=proposition['type'],
                            amount=proposition['amount'],
                            remainingSeat=proposition['remaining_seat'])
            p.save()
            list_of_proposition.append(p)
        propositions = Propositions(recordedTime=self.recorded_date,
                                    content=list_of_proposition)
        propositions.save()

        # tr = TrainRecord(departureTime=self.departure_date,
        #                 arrivalTime=self.arrival_date,
        #                 origin=Station.get_station_by_code(self.origin_code),
        #                 destination=Station.get_station_by_code(self.destination_code),
        #                 duration=self.duration,
        #                 recordedTime=self.recorded_date,)
        # tr.save()
        tr = TrainRecord.objects(
            departureTime=self.departure_date,
            arrivalTime=self.arrival_date,
            origin=Station.get_station_by_code(self.origin_code),
            destination=Station.get_station_by_code(self.destination_code))
        if not tr:
            tr = TrainRecord(
                departureTime=self.departure_date,
                arrivalTime=self.arrival_date,
                origin=Station.get_station_by_code(self.origin_code),
                destination=Station.get_station_by_code(self.destination_code),
                duration=self.duration,
                propositions=[propositions])
            tr.save()
        else:
            tr.update_one(push__propositions=propositions)
        return tr
示例#9
0
    def get():
        """
        Get the list of all the stations registered in the database.
        :return: A list of JSON each containing a station.
        """

        # stations parser
        stations_parser = reqparse.RequestParser()
        stations_parser.add_argument(name='name',
                                     type=str,
                                     help="The name of the station")
        stations_args = stations_parser.parse_args()

        start = time.time()
        if stations_args['name'] is not None:
            stations = json.loads(
                dbStation.search_station(
                    stations_args['name']).order_by("name").to_json())
        else:
            stations = json.loads(dbStation.objects.order_by("name").to_json())
        end = time.time()
        print("GET /stations took " + str(end - start) + " s")
        return stations, 200