def _parse_response(self, response_json): """ Builds search solution. Adds to MongoDB and returns the Solution object. """ self._logger.info('Creating objects to insert to database') solution = Solution(engine=self.engine, origin=self.origin, destination=self.destination, depart_date=self.depart_date, return_date=self.return_date) solution.min_price = response_json['result']['solutionList']['minPrice'] for sol in response_json['result']['solutionList']['solutions']: origin_flight_airline = sol['itinerary']['slices'][0]['flights'][0][:2] origin_flight_number = int(sol['itinerary']['slices'][0]['flights'][0][2:]) dep_time = datetime.datetime.strptime(sol['itinerary']['slices'][0]['departure'][:-6], "%Y-%m-%dT%H:%M") arr_time = datetime.datetime.strptime(sol['itinerary']['slices'][0]['arrival'][:-6], "%Y-%m-%dT%H:%M") arr_city = sol['itinerary']['slices'][0]['destination']['code'] dep_city = sol['itinerary']['slices'][0]['origin']['code'] origin_flight = Flight(airline=origin_flight_airline, fno=origin_flight_number, dep_city=dep_city, arr_city=arr_city, dep_time=dep_time, arr_time=arr_time) return_flight_airline = sol['itinerary']['slices'][1]['flights'][0][:2] return_flight_number = int(sol['itinerary']['slices'][1]['flights'][0][2:]) dep_time = datetime.datetime.strptime(sol['itinerary']['slices'][1]['departure'][:-6], "%Y-%m-%dT%H:%M") arr_time = datetime.datetime.strptime(sol['itinerary']['slices'][1]['arrival'][:-6], "%Y-%m-%dT%H:%M") arr_city = sol['itinerary']['slices'][1]['destination']['code'] dep_city = sol['itinerary']['slices'][1]['origin']['code'] return_flight = Flight(airline=return_flight_airline, fno=return_flight_number, dep_city=dep_city, arr_city=arr_city, dep_time=dep_time, arr_time=arr_time) flight_list = [origin_flight, return_flight] price = sol['displayTotal'] itinerary = Itinerary(flights=flight_list, price=price) solution.itineraries.append(itinerary) solution.save() return solution
def solutions(self): """ Returns a Solution object from MongoDB """ return Solution.objects(depart_date=self.depart_date, return_date=self.return_date, origin=self.origin, destination=self.destination)
def _parse_response(self, response_json): """ Builds search solution. Adds to MongoDB and returns the Solution object. """ self._logger.info('Creating objects to insert to database') solution = Solution(engine=self.engine, origin=self.origin, destination=self.destination, depart_date=self.depart_date, return_date=self.return_date) solution.min_price = response_json['result']['solutionList'][ 'minPrice'] for sol in response_json['result']['solutionList']['solutions']: origin_flight_airline = sol['itinerary']['slices'][0]['flights'][ 0][:2] origin_flight_number = int( sol['itinerary']['slices'][0]['flights'][0][2:]) dep_time = datetime.datetime.strptime( sol['itinerary']['slices'][0]['departure'][:-6], "%Y-%m-%dT%H:%M") arr_time = datetime.datetime.strptime( sol['itinerary']['slices'][0]['arrival'][:-6], "%Y-%m-%dT%H:%M") arr_city = sol['itinerary']['slices'][0]['destination']['code'] dep_city = sol['itinerary']['slices'][0]['origin']['code'] origin_flight = Flight(airline=origin_flight_airline, fno=origin_flight_number, dep_city=dep_city, arr_city=arr_city, dep_time=dep_time, arr_time=arr_time) return_flight_airline = sol['itinerary']['slices'][1]['flights'][ 0][:2] return_flight_number = int( sol['itinerary']['slices'][1]['flights'][0][2:]) dep_time = datetime.datetime.strptime( sol['itinerary']['slices'][1]['departure'][:-6], "%Y-%m-%dT%H:%M") arr_time = datetime.datetime.strptime( sol['itinerary']['slices'][1]['arrival'][:-6], "%Y-%m-%dT%H:%M") arr_city = sol['itinerary']['slices'][1]['destination']['code'] dep_city = sol['itinerary']['slices'][1]['origin']['code'] return_flight = Flight(airline=return_flight_airline, fno=return_flight_number, dep_city=dep_city, arr_city=arr_city, dep_time=dep_time, arr_time=arr_time) flight_list = [origin_flight, return_flight] price = sol['displayTotal'] itinerary = Itinerary(flights=flight_list, price=price) solution.itineraries.append(itinerary) solution.save() return solution