loadCurrencies.py 945 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
import csv
Florent Chehab's avatar
Florent Chehab committed
2
import os
Florent Chehab's avatar
Florent Chehab committed
3
4
from decimal import Decimal

5
from base_app.models import User
Florent Chehab's avatar
Florent Chehab committed
6
7
8
9
10

from backend_app.models.currency import Currency

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):
21
        tmp = os.path.join(os.path.realpath(__file__), "../../assets/currencies.csv")
Florent Chehab's avatar
Florent Chehab committed
22
23
24
25
26
27
28
29
30
31
        currencies_file_loc = os.path.abspath(tmp)

        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="",
32
                    one_EUR_in_this_currency=Decimal(r[2]),
Florent Chehab's avatar
Florent Chehab committed
33
34
                )
                currency.save()
35
                self.add_info_and_save(currency, self.admin)