def retrive_and_insert_rate_from_provider(currency: Text, date: datetime) -> Tuple[Rate, bool]: created = False rate = None currency = currency.upper() prov = provider(settings.CURRENCY_PROVIDER, base=settings.CURRENCY_BASE) result, amount, milestone = prov.rates(currency=currency, date=date) logger.info("{} {} {}".format(result, amount, milestone)) if result: currency_object, _ = Currency.objects.get_or_create(code=currency) base_object, _ = Currency.objects.get_or_create(code=settings.CURRENCY_BASE) try: rate = Rate.objects.get( base=base_object, currency=currency_object, milestone=milestone, amount=amount, provider=settings.CURRENCY_PROVIDER ) created = False except Rate.DoesNotExist: rate = Rate( base=base_object, currency=currency_object, milestone=milestone, amount=amount, provider=settings.CURRENCY_PROVIDER ) created = True rate.save() return rate, created
def test_rate_object_is_serialized(self, serializer, currency, table): rate = Rate(currency=currency, rate=Decimal('12.34'), table=table) rate.save() assert serializer(rate).data == { 'currency': currency.code, 'rate': '{0:.4f}'.format(rate.rate), 'name': currency.name, 'date': table.date, 'relative_change': '0.0000', 'country': CODE_TO_COUNTRY[currency.code] }
def home(request): context = {} links = getLinks() for link in links: # 1.9558 BGN = 1 EUR 2019-03-12 ECB Reference rate <- this is simple output of getRate(), we need currency rate and currency name, so we split the output splited = getRate(link).split() key = splited[1] context[key] = float(splited[0]) rate = Rate(currency=key, cost=context[key]) rate.save() #After we created dictonairy of up-to-date currency rates we can parse it to our render function. return render(request, 'home.html', {"context": context})
def celery_update_currency_rate(): """ Every work day at 18:00 CET or 16:00 UTC is update currencies rates """ response = requests.get(settings.SOURCE_EXCHANGE_URL) data = response.json() rates = data.get('rates') rates_count = Rate.objects.filter(date=data.get("date")).count() # Insert new rates if rates_count == 0: currencies = Currency.objects.filter() instances = [] for currency in currencies: if currency.is_base: rate = 1 else: rate = rates.get(currency.code) instances.append( Rate(rate=rate, currency=currency, date=data.get("date"))) Rate.objects.bulk_create(instances) return True
def test_rate_object_is_serialized(self, serializer, currency): rate = Rate(currency=currency, rate=Decimal('12.34')) assert serializer(rate).data == { 'currency': 'GBP', 'rate': '12.3400', 'name': 'Test Currency', 'country': CODE_TO_COUNTRY[currency.code] }
def save(self, **kwargs): """Override save.""" validated_data = dict( list(self.validated_data.items()) + list(kwargs.items()) ) obj, _ = Inventory.objects.get_or_create( date=validated_data.get('date') ) rate = Rate( price=validated_data['price'], room_id=validated_data['room'].get('id'), inventory=obj, name=validated_data['name'] ) rate.save() return rate
def create_rate(ccy_from=None, ccy_to=None, rate_value=Decimal('0'), last_rate=None): """ Create a rate instance if there is no currency pair in database :param ccy_from: string (max_length=3) :param ccy_to: string (max_length=3) :param rate_value: Decimal :param last_rate: datetime :return: """ rate = Rate() rate.ccy_from = Currency.objects.get(symbol=ccy_from) rate.ccy_to = Currency.objects.get(symbol=ccy_to) rate.rate = rate_value rate.last_rate = datetime.datetime.now() rate.save() return rate
def currency(request, curr, to=''): if request.method == 'PUT': curr_from = Currency.objects.get(name=curr) curr_to = Currency.objects.get(name=to) rate = request.GET.get('rate') try: curr_rate = Rate.objects.get(curr_from=curr_from, curr_to=curr_to) curr_rate.value = float(rate) curr_rate.save() except ObjectDoesNotExist: Rate(curr_from=curr_from, curr_to=curr_to, value=rate).save() return HttpResponse("PUT currency: %s %s" % (curr, to)) elif request.method == 'POST': Currency(name=curr).save() return HttpResponse("POST currency: %s" % (curr, )) elif request.method == 'GET': result = get_object_or_404(Currency, name=curr) return HttpResponse(json.dumps(result.to_dict())) elif request.method == 'DELETE': Currency.objects.get(name=curr).delete() return HttpResponse("DELETE currency: %s" % (curr, ))
def saved_rate(self, saved_currency, saved_table): rate = Rate(rate=Decimal('12.34'), table=saved_table, currency=saved_currency) rate.save() return rate