Added countries app with get views (filter view and get by alpha2 view)

This commit is contained in:
ITQ
2024-02-27 21:56:49 +03:00
parent 607f6de88c
commit a2253a12c5
14 changed files with 108 additions and 16 deletions
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CountriesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "countries"
@@ -0,0 +1,24 @@
# Generated by Django 4.2.10 on 2024-02-27 18:38
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Country',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('alpha2', models.CharField(max_length=2)),
('alpha3', models.CharField(max_length=3)),
('region', models.CharField()),
],
),
]
+11
View File
@@ -0,0 +1,11 @@
from django.db import models
class Country(models.Model):
name = models.CharField(max_length=100)
alpha2 = models.CharField(max_length=2)
alpha3 = models.CharField(max_length=3)
region = models.CharField()
def __str__(self):
return self.name
+9
View File
@@ -0,0 +1,9 @@
from rest_framework import serializers
from countries.models import Country
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = ["name", "alpha2", "alpha3", "region"]
+12
View File
@@ -0,0 +1,12 @@
from django.urls import path
import countries.views
urlpatterns = [
path("", countries.views.CountryListView.as_view(), name="countries"),
path(
"<str:alpha2>/",
countries.views.CountryByAlpha2View.as_view(),
name="country_by_alpha2",
),
]
+16
View File
@@ -0,0 +1,16 @@
from rest_framework.generics import ListAPIView, RetrieveAPIView
from countries.models import Country
from countries.serializers import CountrySerializer
class CountryListView(ListAPIView):
queryset = Country.objects.all().order_by("alpha2")
filterset_fields = ["region"]
serializer_class = CountrySerializer
class CountryByAlpha2View(RetrieveAPIView):
queryset = Country.objects.all()
serializer_class = CountrySerializer
lookup_field = "alpha2"
+16 -9
View File
@@ -1,6 +1,7 @@
import os
from pathlib import Path
import dj_database_url
from dotenv import load_dotenv
load_dotenv()
@@ -13,6 +14,8 @@ DEBUG = os.getenv("DJANGO_DEBUG", "true").lower() in ("true", "1", "yes", "y")
ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "*").split(" ")
INTERNAL_IPS = os.getenv("DJANGO_INTERNAL_IPS", "127.0.0.1").split(" ")
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
@@ -22,8 +25,10 @@ INSTALLED_APPS = [
"django.contrib.staticfiles",
# Third-party apps
"rest_framework",
"django_filters",
# Developed apps
"ping.apps.PingConfig",
"countries.apps.CountriesConfig",
]
MIDDLEWARE = [
@@ -56,10 +61,12 @@ TEMPLATES = [
WSGI_APPLICATION = "pulse.wsgi.application"
POSTGRES_CONN = os.getenv("POSTGRES_CONN")
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
"ENGINE": "django.db.backends.postgresql",
**dj_database_url.parse(POSTGRES_CONN),
},
}
@@ -99,12 +106,12 @@ STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
REST_FRAMEWORK = {
"DEFAULT_FILTER_BACKENDS": [
"django_filters.rest_framework.DjangoFilterBackend"
]
}
if DEBUG:
INSTALLED_APPS.append("debug_toolbar")
INSTALLED_APPS.insert(0, "debug_toolbar")
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
else:
REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": [
"rest_framework.renderers.JSONRenderer",
],
}
+5 -4
View File
@@ -6,14 +6,15 @@ urlpatterns = [
# Built-in urls
path("admin/", admin.site.urls),
path(
"api-auth/", include(
"rest_framework.urls", namespace="rest_framework",
"api-auth/",
include(
"rest_framework.urls",
namespace="rest_framework",
),
),
# API
path("api/ping/", include("ping.urls")),
path("api/countries/", include("countries.urls")),
]
if settings.DEBUG: