Exemple #1
0
def save_results(rates, session, hotel, govt):

    for item in rates:
        rate = Rate(**item)

        try:
            # check if already in database
            q = session.query(Rate).filter(Rate.hotel == hotel['object'],
                                           Rate.arrive == rate.arrive).first()

            # update inital_rate if that field is empty
            if q:
                if 'govt_rate' in item and q.govt_rate_initial is None:
                    q.govt_rate_initial = rate.govt_rate
                elif 'commercial_rate' in item and q.commercial_rate_initial is None:
                    q.commercial_rate_initial = rate.commercial_rate

            if q and govt is True:
                q.updated = datetime.utcnow()
                q.govt_rate = rate.govt_rate
            elif q and govt is False:
                q.updated = datetime.utcnow()
                q.commercial_rate = rate.commercial_rate
            else:
                if govt is True:
                    rate.govt_rate_initial = rate.govt_rate
                elif govt is False:
                    rate.commercial_rate_initial = rate.commercial_rate
                hotel['object'].rates.append(rate)
            session.commit()
        except:
            session.rollback()
            raise
Exemple #2
0
def save_result(arrive, govt_rate, commercial_rate, item, govt_link,
                commercial_link):
    # create db session
    session = create_db_session()

    # get location and hotel
    location = get_or_create(session, Location, city=item['city'])
    hotel = get_or_create(session, Hotel, name=item['name'], location=location)

    rate = Rate()

    # check if already in database
    q = session.query(Rate).filter(Rate.hotel == hotel,
                                   Rate.arrive == arrive).first()

    # update if already exists
    if q:
        q.updated = datetime.utcnow()
        q.govt_rate = govt_rate
        q.commercial_rate = commercial_rate

        # update initial rates if not already
        if govt_rate and q.govt_rate_initial is None:
            q.govt_rate_initial = govt_rate
        elif commercial_rate and q.commercial_rate_initial is None:
            q.commercial_rate_initial = commercial_rate
    else:
        # new hotel rate
        rate.location = location
        rate.hotel = hotel
        rate.arrive = datetime.strptime(arrive, '%m/%d/%Y')
        rate.govt_rate = govt_rate
        rate.govt_rate_initial = govt_rate
        rate.commercial_rate_initial = commercial_rate
        rate.commercial_rate = commercial_rate
        rate.govt_link = govt_link
        rate.commercial_link = commercial_link
        session.add(rate)

    session.commit()
    session.close()