From 19d891de37d303a7a72a4036078fa98a04e295df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madis=20V=C3=A4in?= Date: Thu, 28 Dec 2023 09:57:50 +0200 Subject: [PATCH] add currencies --- requirements.in | 1 + requirements.txt | 1 + vatcomply/tests/test_currencies.py | 13 +++++++++++++ vatcomply/urls.py | 3 ++- vatcomply/views.py | 13 +++++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 vatcomply/tests/test_currencies.py diff --git a/requirements.in b/requirements.in index d67ce79..c5b2bc3 100644 --- a/requirements.in +++ b/requirements.in @@ -1,4 +1,5 @@ apscheduler +babel django django-cors-headers gunicorn diff --git a/requirements.txt b/requirements.txt index 5d24ce3..73a36f3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ anyio==4.2.0 APScheduler==3.10.4 asgiref==3.7.2 attrs==23.1.0 +Babel==2.14.0 certifi==2023.11.17 charset-normalizer==3.3.2 click==8.1.7 diff --git a/vatcomply/tests/test_currencies.py b/vatcomply/tests/test_currencies.py new file mode 100644 index 0000000..59a15f4 --- /dev/null +++ b/vatcomply/tests/test_currencies.py @@ -0,0 +1,13 @@ +from django.conf import settings +from django.test import TestCase +from django.core.management import call_command + +from vatcomply.models import Country + + +class CurrenciesTest(TestCase): + def test_currencies_api(self): + response = self.client.get("/currencies") + self.assertEqual(response.status_code, 200) + self.assertIsInstance(response.json(), dict) + self.assertEqual(len(response.json()), len(settings.CURRENCY_SYMBOLS)) diff --git a/vatcomply/urls.py b/vatcomply/urls.py index 0e8a557..68db57a 100644 --- a/vatcomply/urls.py +++ b/vatcomply/urls.py @@ -1,13 +1,14 @@ # from django.contrib import admin from django.urls import path -from vatcomply.views import CountriesView, GeolocateView, RatesView, VATView +from vatcomply.views import CountriesView, CurrenciesView, GeolocateView, RatesView, VATView urlpatterns = [ # path("admin/", admin.site.urls), # API path("countries", CountriesView.as_view()), + path("currencies", CurrenciesView.as_view()), path("geolocate", GeolocateView.as_view()), path("vat", VATView.as_view()), path("rates", RatesView.as_view()), diff --git a/vatcomply/views.py b/vatcomply/views.py index 29e0d70..6b927e5 100644 --- a/vatcomply/views.py +++ b/vatcomply/views.py @@ -2,6 +2,7 @@ import pendulum import zeep +from babel.numbers import get_currency_name, get_currency_symbol from django.conf import settings from django.utils.decorators import method_decorator from django.views import View @@ -39,6 +40,18 @@ async def get(self, request): return JsonResponse(countries) +@method_decorator(csrf_exempt, name="dispatch") +class CurrenciesView(View): + async def get(self, request): + currencies = {} + for symbol in list(settings.CURRENCY_SYMBOLS): + currencies[symbol] = { + "name": get_currency_name(symbol, locale="en"), + "symbol": get_currency_symbol(symbol, locale="en"), + } + return JsonResponse(currencies) + + @method_decorator(csrf_exempt, name="dispatch") class GeolocateView(View): async def get(self, request):