Exemple #1
0
def update(**kwargs):
    syncMap = kwargs.get("syncMap")
    updateIds = [djangoUtils.decodeId(updateId) for updateId in syncMap.keys()]

    newhotels = []
    hotels = {
        djangoUtils.encodeId(hotel.id): hotel
        for hotel in Hotel.objects.filter(
            pk__in=updateIds).prefetch_related("destination").all()
    }
    for updateId in syncMap:
        jsonData = json.loads(syncMap[updateId]["json"])
        name_cn = jsonData["name_cn"] if "name_cn" in jsonData else None
        name_en = jsonData["name_en"] if "name_en" in jsonData else None
        address = jsonData["address"] if "address" in jsonData else None
        zipCode = jsonData["zipCode"] if "zipCode" in jsonData else None
        latitude = jsonData["latitude"] if "latitude" in jsonData else None
        longitude = jsonData["longitude"] if "longitude" in jsonData else None
        starRating = jsonData[
            "starRating"] if "starRating" in jsonData else None
        telephone = jsonData["telephone"] if "telephone" in jsonData else None
        tosId = djangoUtils.decodeId(syncMap[updateId]["tosId"])

        versionData = [("name_cn", name_cn), ("name_en", name_en),
                       ("address", address), ("zipCode", zipCode),
                       ("longitude", longitude), ("latitude", latitude),
                       ("starRating", starRating), ("telephone", telephone)]
        version = utils.generateVersion(versionData)
        hotel = hotels[updateId]
        if name_cn:
            hotel.name_cn = name_cn
        if name_en:
            hotel.name_en = name_en
        if address:
            hotel.address = address
        if zipCode:
            hotel.zipCode = zipCode
        if longitude:
            hotel.longitude = longitude
        if latitude:
            hotel.latitude = latitude
        if starRating:
            hotel.starRating = starRating
        if telephone:
            hotel.telephone = telephone
        if tosId:
            hotel.tosId = tosId
        hotel.version = version
        hotel.save()
        newhotels.append(hotel)
    indexHotel(newhotels)
Exemple #2
0
def update(**kwargs):
    syncMap = kwargs.get("syncMap")
    updateIds = [djangoUtils.decodeId(updateId) for updateId in syncMap.keys()]

    destinationMap = {
        djangoUtils.encodeId(destination.id): destination
        for destination in Destination.objects.filter(pk__in=updateIds).all()
    }
    for updateId in syncMap:
        jsonData = json.loads(syncMap[updateId]["json"])
        name_cn = jsonData["name_cn"] if "name_cn" in jsonData else None
        name_en = jsonData["name_en"] if "name_en" in jsonData else None
        longitude = syncMap[updateId]["destination"]["longitude"]
        latitude = syncMap[updateId]["destination"]["latitude"]
        longitudeE = syncMap[updateId]["destination"]["longitudeE"]
        longitudeW = syncMap[updateId]["destination"]["longitudeW"]
        latitudeS = syncMap[updateId]["destination"]["latitudeS"]
        latitudeN = syncMap[updateId]["destination"]["latitudeN"]

        tosId = djangoUtils.decodeId(syncMap[updateId]["destination"]["tosId"])

        destination = destinationMap[updateId]
        versionData = [
            ("name_cn", name_cn),
            ("name_en", name_en),
        ]
        version = utils.generateVersion(versionData)
        if name_cn:
            destination.name_cn = name_cn
        if name_en:
            destination.name_en = name_en
        if tosId:
            destination.tosId = tosId
        if latitude:
            destination.latitude = latitude
        if longitude:
            destination.longitude = longitude
        if longitudeE:
            destination.longitudeE = longitudeE
        if longitudeW:
            destination.longitudeW = longitudeW
        if latitudeS:
            destination.latitudeS = latitudeS
        if latitudeN:
            destination.latitudeN = latitudeN
        destination.version = version
        destination.save()
Exemple #3
0
def read(**kwargs):
    query = kwargs.get("query")
    checkIn = query.get("checkIn")
    checkOut = query.get("checkOut")
    cityId = query.get("city")
    star = query.get("star")
    last = kwargs.get("last", None)
    start = kwargs.get("start", 0)
    count = kwargs.get("count", 24)
    last = djangoUtils.decodeId(last) if last else 0

    destination = Destination.objects.filter(
        tosId=djangoUtils.decodeId(cityId), source="dida").first()
    if not destination:
        return {"hotelQuotes": [], "hasMore": False}

    def doSearchHotels(fetchStart, fetchTripCnt):
        return searchHotels(fetchStart,
                            fetchTripCnt,
                            star=star,
                            longitude=destination.longitude,
                            latitude=destination.latitude,
                            distance=30000)

    hotelIds, hasMore, highlights, total = apiUtils.searchWithHighlights(
        last, start, count, doSearchHotels)
    if not hotelIds:
        return {"hotelQuotes": [], "hasMore": False}

    hotels = Hotel.objects.filter(pk__in=hotelIds).all()
    tosIdMap = {hotel.sourceId: hotel.tosId for hotel in hotels}

    client = DidaClient()
    hotelList = [int(hotel.sourceId) for hotel in hotels]
    hotels = client.searchHotelPrices(checkIn, checkOut, hotelList=hotelList)
    hotelDict = {str(hotel["HotelID"]): hotel for hotel in hotels}

    hotelQuotes = [{
        "price":
        hotelDict[sourceId]["LowestPrice"]["Value"],
        "currency":
        utils.getCurrencyCode(hotelDict[sourceId]["LowestPrice"]["Currency"]),
        "tosId":
        djangoUtils.encodeId(tosIdMap[sourceId])
        if sourceId in tosIdMap else None
    } for sourceId in hotelDict]
    return {"hotelQuotes": hotelQuotes, "hasMore": hasMore}
Exemple #4
0
 def tosId(self, fields=None):
     if self._destination.tosId:
         return djangoUtils.encodeId(self._destination.tosId)
     else:
         return None
Exemple #5
0
 def parentId(self, fields=None):
     if self._destination.parent and self._destination.parent.tosId:
         return djangoUtils.encodeId(self._destination.parent.tosId)
     else:
         return None
Exemple #6
0
 def tosId(self, fields=None):
     if self._hotel.tosId:
         return djangoUtils.encodeId(self._hotel.tosId)
     else:
         return None
Exemple #7
0
 def destination(self, fields=None):
     if fields == True:
         return djangoUtils.encodeId(self._hotel.destination_id)
     return DestinationSerializer(destination=self._hotel.destination, fields=DestinationFields.brief).data
Exemple #8
0
 def hotel(self, fields=None):
     if fields == True:
         return djangoUtils.encodeId(self._hotelUpdate.hotel_id)
     return HotelSerializer(hotel=self._hotelUpdate.hotel,
                            fields=HotelFields.brief)