From 7b1c2cdd56bed763da996a53ecd2f34fae1500a2 Mon Sep 17 00:00:00 2001 From: alireza Date: Tue, 17 Dec 2024 20:25:31 +0330 Subject: [PATCH] fix: country apis --- apps/api/urls.py | 5 +++-- apps/api/views.py | 9 +++++++++ utils/countries.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 utils/countries.py diff --git a/apps/api/urls.py b/apps/api/urls.py index dc60af2..d240486 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -1,9 +1,10 @@ from django.urls import path -from .views import HomeView +from .views import HomeView, CountryView urlpatterns = [ - path('', HomeView.as_view()) + path('', HomeView.as_view()), + path('countries/', CountryView.as_view()), ] diff --git a/apps/api/views.py b/apps/api/views.py index b044e94..7ddd4a3 100644 --- a/apps/api/views.py +++ b/apps/api/views.py @@ -9,6 +9,9 @@ from apps.account.models import User class HomeSerializer(serializers.Serializer): token = serializers.CharField() +from utils.countries import countries + + # test class generate token class HomeView(GenericAPIView): serializer_class = HomeSerializer @@ -31,3 +34,9 @@ class HomeView(GenericAPIView): token, created = Token.objects.get_or_create(user=user) return Response({'token': token.key}) + +class CountryView(GenericAPIView): + + def get(self, request): + return Response(countries, status=200) + diff --git a/utils/countries.py b/utils/countries.py new file mode 100644 index 0000000..dc7eb8b --- /dev/null +++ b/utils/countries.py @@ -0,0 +1,32 @@ +countries = [ + {"id": 1, "name": "Иран"}, + {"id": 2, "name": "Ирак"}, + {"id": 3, "name": "Сирия"}, + {"id": 4, "name": "Турция"}, + {"id": 5, "name": "Саудовская Аравия"}, + {"id": 6, "name": "Иордания"}, + {"id": 7, "name": "Ливан"}, + {"id": 8, "name": "Египет"}, + {"id": 9, "name": "Объединённые Арабские Эмираты"}, + {"id": 10, "name": "Катар"}, + {"id": 11, "name": "Кувейт"}, + {"id": 12, "name": "Бахрейн"}, + {"id": 13, "name": "Оман"}, + {"id": 14, "name": "Йемен"}, + {"id": 15, "name": "Афганистан"}, + {"id": 16, "name": "Пакистан"}, + {"id": 17, "name": "Азербайджан"}, + {"id": 18, "name": "Армения"}, + {"id": 19, "name": "Грузия"}, + {"id": 20, "name": "Кувейт"}, + {"id": 21, "name": "Ливан"}, + {"id": 22, "name": "Израиль"}, + {"id": 23, "name": "Египет"}, + {"id": 24, "name": "Палестина"}, + {"id": 25, "name": "Ливия"}, +] + +def get_country_with_id(country_id: int): + country_name = next((country['name'] for country in countries if country['id'] == country_id), None) + return city_name if city_name else None +