loadCurrencies.py 959 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
import csv
from decimal import Decimal
Florent Chehab's avatar
Florent Chehab committed
3
from os.path import abspath, join
Florent Chehab's avatar
Florent Chehab committed
4

5
from backend_app.load_data.shared import ASSETS_PATH
Florent Chehab's avatar
Florent Chehab committed
6
from backend_app.models.currency import Currency
Florent Chehab's avatar
Florent Chehab committed
7
from base_app.models import User
Florent Chehab's avatar
Florent Chehab committed
8
9
10

from .loadGeneric import LoadGeneric

Florent Chehab's avatar
Florent Chehab committed
11
12

class LoadCurrencies(LoadGeneric):
Florent Chehab's avatar
Florent Chehab committed
13
14
15
16
17
    """
        Load currencies in the app
    """

    def __init__(self, admin: User):
Florent Chehab's avatar
Florent Chehab committed
18
19
20
        self.admin = admin

    def load(self):
Florent Chehab's avatar
Florent Chehab committed
21
        currencies_file_loc = abspath(join(ASSETS_PATH, "currencies.csv"))
Florent Chehab's avatar
Florent Chehab committed
22
23
24
25
26
27
28
29
30

        with open(currencies_file_loc) as csvfile:
            reader = csv.reader(csvfile, quotechar='"')
            next(reader)
            for r in reader:
                currency = Currency(
                    code=r[0],
                    name=r[1],
                    symbol="",
31
                    one_EUR_in_this_currency=Decimal(r[2]),
Florent Chehab's avatar
Florent Chehab committed
32
33
                )
                currency.save()
34
                self.add_info_and_save(currency, self.admin)