Exemplo n.º 1
0
    def _populate_for_dates(cls, date_begin: datetime.datetime,
                            date_end: datetime.datetime,
                            currency: AbstractCurrency):
        """ Load list of currency rates from date_begin to date_end.
        
        :param date_begin: first day of rates
        :param date_end: last day of rates
        :param currency: see :class Currency:
        """
        raw_rates = get_dynamic_rates(date_req1=date_begin,
                                      date_req2=date_end,
                                      currency_id=currency.cbrf_id)

        for rate in raw_rates:
            with transaction.atomic():
                try:
                    cls.objects.create(
                        currency=currency,
                        date=str_to_date(rate.attrib['Date']).date(),
                        value=Decimal(
                            rate.findtext('Value').replace(',', '.')))
                except IntegrityError:
                    logger.warning(
                        "Rate {} for {} already in db. Skipped.".format(
                            currency.eng_name,
                            str_to_date(rate.attrib['Date'])))

        return cls.objects.filter(currency=currency,
                                  date__gte=date_begin,
                                  date__lte=date_end)
Exemplo n.º 2
0
    def __init__(self, date_1: datetime.datetime, date_2: datetime.datetime,
                 id_code: str):
        self._raw_dynamic_rates = get_dynamic_rates(date_req1=date_1,
                                                    date_req2=date_2,
                                                    currency_id=id_code)
        self.date_1 = str_to_date(self._raw_dynamic_rates.attrib['DateRange1'])
        self.date_2 = str_to_date(self._raw_dynamic_rates.attrib['DateRange2'])
        self.id = self._raw_dynamic_rates.attrib['ID']

        self.rates = list()

        for rate in self._raw_dynamic_rates:
            self.rates.append(DynamicCurrencyRecord(rate))
Exemplo n.º 3
0
    def __init__(
        self, date_1: datetime.datetime, date_2: datetime.datetime, id_code: str
    ):
        self._raw_dynamic_rates = get_dynamic_rates(
            date_req1=date_1, date_req2=date_2, currency_id=id_code
        )
        self.date_1 = str_to_date(self._raw_dynamic_rates.attrib["DateRange1"])
        self.date_2 = str_to_date(self._raw_dynamic_rates.attrib["DateRange2"])
        self.id = self._raw_dynamic_rates.attrib["ID"]

        self.rates = dict()

        for rate in self._raw_dynamic_rates:
            record = DynamicCurrencyRecord(rate)
            self.rates[record.date] = record
Exemplo n.º 4
0
    def _populate_for_date(cls,
                           currency: AbstractCurrency,
                           date: datetime.datetime = None):

        raw_rates = get_daily_rates(date)
        record = [
            rate for rate in raw_rates if rate.attrib['ID'] == currency.cbrf_id
        ]

        if record:
            actual_date = str_to_date(raw_rates.attrib['Date'])
            rate = record[0]
            with transaction.atomic():
                try:
                    return cls.objects.create(
                        currency=currency,
                        date=actual_date.date(),
                        value=Decimal(
                            rate.findtext('Value').replace(',', '.')))
                except IntegrityError:
                    logger.warning(
                        "Rate {} for {} already in db. Skipped.".format(
                            currency.eng_name, actual_date))
                    return

        raise ValueError("Error in parameters")
Exemplo n.º 5
0
    def __init__(self, date: datetime.datetime = None, lang: str = 'rus'):
        self._raw_daily_rates = get_daily_rates(date_req=date, lang=lang)
        self.date = str_to_date(self._raw_daily_rates.attrib['Date'])

        self.rates = list()

        for rate in self._raw_daily_rates:
            self.rates.append(DailyCurrencyRecord(rate))
Exemplo n.º 6
0
    def __init__(self, date: datetime.datetime = None, lang: str = "rus"):
        self._raw_daily_rates = get_daily_rates(date_req=date, lang=lang)
        self.date = str_to_date(self._raw_daily_rates.attrib["Date"])

        self.rates = dict()

        for rate in self._raw_daily_rates:
            record = DailyCurrencyRecord(rate)
            self.rates[record.id] = record
Exemplo n.º 7
0
 def _parse_dynamic_currency_record(self, elem: Element):
     self.id = elem.attrib["Id"]
     self.date = str_to_date(elem.attrib["Date"])
     self.denomination = int(elem.findtext("Nominal"))
     self.value = Decimal(elem.findtext("Value").replace(",", "."))
Exemplo n.º 8
0
 def _parse_dynamic_currency_record(self, elem: Element):
     self.id = elem.attrib['Id']
     self.date = str_to_date(elem.attrib['Date'])
     self.denomination = int(elem.findtext('Nominal'))
     self.value = Decimal(elem.findtext('Value').replace(',', '.'))