def get_for_date(cls,
                     currency: AbstractCurrency,
                     date: datetime.datetime = None,
                     force: bool = False):

        currency = get_cbrf_model('Currency').objects.get(
            cbrf_id=currency.cbrf_id)
        if force:
            rate = cls._populate_for_date(currency, date)
        else:
            rate = cls.objects.filter(currency=currency, date=date).all()
            rate = rate.first() if rate else cls._populate_for_date(
                currency, date)

        return rate
    def get_for_dates(cls,
                      date_begin: datetime.datetime,
                      date_end: datetime.datetime,
                      currency: AbstractCurrency,
                      force: bool = False):
        """ Try to get rates from local DB. If response is empty -> try to get from CBR API """

        currency = get_cbrf_model('Currency').objects.get(
            cbrf_id=currency.cbrf_id)

        if force:
            rates = cls._populate_for_dates(date_begin, date_end, currency)
        else:
            rates = cls.objects.filter(currency=currency,
                                       date__gte=date_begin,
                                       date__lte=date_end)
            if not rates:
                rates = cls._populate_for_dates(date_begin, date_end, currency)

        return rates
Beispiel #3
0
    def handle(self, *args, **options):
        Currency = get_cbrf_model('Currency')

        force = options.get('force', False)

        if not force:
            try:
                Currency.populate()
            except IntegrityError:
                logger.error(
                    'Abort. Looks like Currencies already populated. '
                    'To force populate use "python manage.py load_currencies --force"'
                )
                raise IntegrityError(
                    'Currencies already populated. '
                    'To force populate use "python manage.py load_currencies --force"'
                )
        else:
            Currency.populate(force=True)

        logger.info('Done. Currencies was populated.')
Beispiel #4
0
import logging
from datetime import datetime
from decimal import Decimal

from django.core.management import call_command
from django.db import IntegrityError
from django.test import TestCase

from django_cbrf import settings
from django_cbrf.utils import get_cbrf_model

Currency = get_cbrf_model('Currency')
Record = get_cbrf_model('Record')


class CBRFManagementCommandsTestCase(TestCase):
    def setUp(self):
        logging.disable(logging.CRITICAL)

    def test_load_currencies(self):
        """ Try to populate Currencies """

        self.assertEqual(len(Currency.objects.all()), 0)
        call_command('load_currencies')
        self.assertEqual(len(Currency.objects.all()), 61)

        with self.assertRaisesMessage(IntegrityError,
                                      'Currencies already populated. '
                                      'To force populate use "python manage.py '
                                      'load_currencies --force"'):
            call_command('load_currencies')