def test_get_rate_for_sms_and_email(notify_db, notify_db_session): letter_rate = LetterRate(start_date=datetime(2017, 12, 1), rate=Decimal(0.33), crown=True, sheet_count=1, post_class='second') dao_create_letter_rate(letter_rate) sms_rate = Rate(valid_from=datetime(2017, 12, 1), rate=Decimal(0.15), notification_type=SMS_TYPE) db.session.add(sms_rate) email_rate = Rate(valid_from=datetime(2017, 12, 1), rate=Decimal(0), notification_type=EMAIL_TYPE) db.session.add(email_rate) non_letter_rates = [(r.notification_type, r.valid_from, r.rate) for r in Rate.query.order_by(desc(Rate.valid_from)).all()] letter_rates = [(r.start_date, r.crown, r.sheet_count, r.rate) for r in LetterRate.query.order_by(desc(LetterRate.start_date)).all()] rate = get_rate(non_letter_rates, letter_rates, SMS_TYPE, datetime(2018, 1, 1)) assert rate == Decimal(0.15) rate = get_rate(non_letter_rates, letter_rates, EMAIL_TYPE, datetime(2018, 1, 1)) assert rate == Decimal(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
def setUpTestData(cls): cls.manufacturer = Manufacturer(name="Volkswagen") cls.manufacturer.save() cls.car = Car(manufacturer=cls.manufacturer, name="Golf") cls.second_car = Car(manufacturer=cls.manufacturer, name="Passat") cls.car.save() cls.second_car.save() # Adding 3 rates for golf and 2 rates for passat for _ in range(3): cls.rate = Rate(car=cls.car, rating=4) cls.rate.save() for _ in range(2): cls.rate = Rate(car=cls.second_car, rating=2) cls.rate.save()
def setUp(self): self.current_rate = Rate( from_currency=Currency(code="BTC"), to_currency=Currency(code="USD"), rate=Decimal("2"), rate_open=Decimal("11"), last_trade_at=datetime(2019, 3, 8, 11, 10, 0), ) self.new_rate = Rate( from_currency=Currency(code="BTC"), to_currency=Currency(code="USD"), rate=Decimal("1"), last_trade_at=datetime(2019, 3, 9, 12, 11, 0), )
def create_rate(start_date, value, notification_type): rate = Rate(id=uuid.uuid4(), valid_from=start_date, rate=value, notification_type=notification_type) db.session.add(rate) db.session.commit() return rate
def fill_rate_open(new_rate: Rate, current_rate: Rate or None) -> Rate: if new_rate.rate_open: logging.debug( "rate_open provided by exchange: %d, pair: %d-%d", new_rate.exchange_id, new_rate.from_currency.id, new_rate.to_currency.id, ) return new_rate if not current_rate: if new_rate.last_trade_at.hour == 0: new_rate.rate_open = new_rate.rate logging.info( "Set new rate_open for exchange: %d, pair: %d-%d", new_rate.exchange_id, new_rate.from_currency.id, new_rate.to_currency.id, ) else: if new_rate.last_trade_at.date() == current_rate.last_trade_at.date(): new_rate.rate_open = current_rate.rate_open logging.debug( "Set existed rate_open for exchange: %d, pair: %d-%d", new_rate.exchange_id, new_rate.from_currency.id, new_rate.to_currency.id, ) elif new_rate.last_trade_at.hour == 0: new_rate.rate_open = new_rate.rate logging.info( "Set new rate_open for exchange: %d, pair: %d-%d", new_rate.exchange_id, new_rate.from_currency.id, new_rate.to_currency.id, ) else: logging.info( "Reset rate_open for exchange: %d, pair: %d-%d", new_rate.exchange_id, new_rate.from_currency.id, new_rate.to_currency.id, ) return new_rate
async def calculate_expenses(self, payd_events, partner, country, project): try: rate = await self.request.app.objects.get( Rate.select(Rate).join(ProjectRate).join(Project).switch( Rate).where( Rate.country == country, Rate.partner == partner, Project.name == project, )) except Rate.DoesNotExist: return 0 return rate.value * payd_events
def post_rate(): form = PostRateForm() if form.validate_on_submit(): rate = Rate(equipment_type=form.equipment_type.data, hazardous_freight=form.hazardous_freight.data, origin=form.origin.data, destination=form.destination.data, brokered_load=form.brokered_load.data, weather=form.weather.data, rate_per_mile=form.rate_per_mile.data, deadhead=form.dead_head.data, author=current_user) db.session.add(rate) db.session.commit() flash(_('Your rate has been saved.')) return redirect(url_for('main.index')) return render_template('post_rate.html', title=_('Post Rate'), form=form)
def rate_from_pair_data(pair_data: PairData, exchange_id: int) -> Rate: db_session = Session() try: from_currency = (db_session.query(Currency).filter_by( is_active=True, code=pair_data.pair.from_currency).one()) to_currency = (db_session.query(Currency).filter_by( is_active=True, code=pair_data.pair.to_currency).one()) except NoResultFound: raise CurrencyNotSupportedException(pair_data.pair) return Rate( exchange_id=exchange_id, from_currency=from_currency, to_currency=to_currency, rate=pair_data.rate, rate_open=pair_data.rate_open, low24h=pair_data.low24h, high24h=pair_data.high24h, last_trade_at=pair_data.last_trade_at, )
def rates(): global filename, volume_path, allowed_ext, full_path if request.method == 'POST': rate_list = [] try: filename = request.form['file'] file_ext = filename.split('.')[-1] if file_ext not in allowed_ext: return Response( 'File type is not allowed ({})'.format(file_ext), status=400) except: return "No file name was given. Please mention wanted file's name inside the form." finally: full_path = volume_path + filename try: book = xlrd.open_workbook(full_path, on_demand=True) except: return Response("File ({}) not found in folder".format(filename), status=404) else: ## Case file was opend successfuly sheet = book.sheet_by_index( 0) ## ToDo: change to find sheet by name for rownum in range(1, sheet.nrows): temp_obj = {} temp_rate = {} for col in range(0, sheet.ncols): col_name = sheet.col_values(col)[0].lower() value = sheet.row_values( rownum)[col] if col_name != 'product' else str( sheet.row_values(rownum)[col]).split('.')[0] temp_obj[col_name] = value new_rate = Rate(product_id=temp_obj['product'], scope=temp_obj['scope'], rate=temp_obj['rate']) exist_rate = Rate.query.filter_by( product_id=new_rate.product_id, scope=new_rate.scope).first() if exist_rate is None: db.session.add(new_rate) temp_rate = new_rate.serialize else: exist_rate.rate = new_rate.rate temp_rate = exist_rate.serialize try: db.session.commit() except Exception as e: msg = 'Could not insert new rate({})\n{}\n'.format( temp_obj, e) return Response(msg, status=500) else: rate_list.append(temp_rate) book.release_resources() del book return Response(json.dumps(rate_list), status=200) elif request.method == 'GET': if full_path is None or full_path == '': return Response('There are no available files', status=404) try: return send_file( filename_or_fp=full_path, mimetype= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', cache_timeout=0, as_attachment=True) except FileNotFoundError as e: return Response(str(e), status=404)
def set_up_rate(notify_db, start_date, value): rate = Rate(id=uuid.uuid4(), valid_from=start_date, rate=value, notification_type=SMS_TYPE) notify_db.session.add(rate)
def test_insert_rate(self): today = date.today() rates = [Rate(day=today - timedelta(i), value=200 - i) for i in range(100)] db.session.add_all(rates) db.session.commit()
from app import db from app.models import Rate db.create_all() add_rate_gsm = Rate(type_connect='GSM', price_per_minute=15) add_rate_cdma = Rate(type_connect='CDMA', price_per_minute=20) add_rate_lte = Rate(type_connect='LTE', price_per_minute=25) db.session.add(add_rate_gsm) db.session.add(add_rate_cdma) db.session.add(add_rate_lte) db.session.commit()
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()