Added countries app with get views (filter view and get by alpha2 view)
This commit is contained in:
+2
-1
@@ -4,6 +4,7 @@ WORKDIR /app
|
|||||||
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE 1
|
ENV PYTHONDONTWRITEBYTECODE 1
|
||||||
ENV PYTHONUNBUFFERED 1
|
ENV PYTHONUNBUFFERED 1
|
||||||
|
ENV SERVER_ADDRESS = 0.0.0.0
|
||||||
ENV SERVER_PORT=8080
|
ENV SERVER_PORT=8080
|
||||||
ENV DJANGO_DEBUG = False
|
ENV DJANGO_DEBUG = False
|
||||||
|
|
||||||
@@ -15,4 +16,4 @@ WORKDIR /app
|
|||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
CMD ["sh", "-c", "cd pulse && exec python3 manage.py runserver 0.0.0.0:$SERVER_PORT"]
|
CMD ["sh", "-c", "cd pulse && exec python3 manage.py runserver $SERVER_ADDRESS:$SERVER_PORT"]
|
||||||
|
|||||||
@@ -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()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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
|
||||||
@@ -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"]
|
||||||
@@ -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",
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -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"
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import dj_database_url
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
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(" ")
|
ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "*").split(" ")
|
||||||
|
|
||||||
|
INTERNAL_IPS = os.getenv("DJANGO_INTERNAL_IPS", "127.0.0.1").split(" ")
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
"django.contrib.admin",
|
"django.contrib.admin",
|
||||||
"django.contrib.auth",
|
"django.contrib.auth",
|
||||||
@@ -22,8 +25,10 @@ INSTALLED_APPS = [
|
|||||||
"django.contrib.staticfiles",
|
"django.contrib.staticfiles",
|
||||||
# Third-party apps
|
# Third-party apps
|
||||||
"rest_framework",
|
"rest_framework",
|
||||||
|
"django_filters",
|
||||||
# Developed apps
|
# Developed apps
|
||||||
"ping.apps.PingConfig",
|
"ping.apps.PingConfig",
|
||||||
|
"countries.apps.CountriesConfig",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@@ -56,10 +61,12 @@ TEMPLATES = [
|
|||||||
|
|
||||||
WSGI_APPLICATION = "pulse.wsgi.application"
|
WSGI_APPLICATION = "pulse.wsgi.application"
|
||||||
|
|
||||||
|
POSTGRES_CONN = os.getenv("POSTGRES_CONN")
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
"default": {
|
"default": {
|
||||||
"ENGINE": "django.db.backends.sqlite3",
|
"ENGINE": "django.db.backends.postgresql",
|
||||||
"NAME": BASE_DIR / "db.sqlite3",
|
**dj_database_url.parse(POSTGRES_CONN),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,12 +106,12 @@ STATIC_URL = "static/"
|
|||||||
|
|
||||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||||
|
|
||||||
if DEBUG:
|
|
||||||
INSTALLED_APPS.append("debug_toolbar")
|
|
||||||
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
|
|
||||||
else:
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
"DEFAULT_RENDERER_CLASSES": [
|
"DEFAULT_FILTER_BACKENDS": [
|
||||||
"rest_framework.renderers.JSONRenderer",
|
"django_filters.rest_framework.DjangoFilterBackend"
|
||||||
],
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if DEBUG:
|
||||||
|
INSTALLED_APPS.insert(0, "debug_toolbar")
|
||||||
|
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
|
||||||
|
|||||||
@@ -6,14 +6,15 @@ urlpatterns = [
|
|||||||
# Built-in urls
|
# Built-in urls
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
path(
|
path(
|
||||||
"api-auth/", include(
|
"api-auth/",
|
||||||
"rest_framework.urls", namespace="rest_framework",
|
include(
|
||||||
|
"rest_framework.urls",
|
||||||
|
namespace="rest_framework",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
# API
|
# API
|
||||||
|
|
||||||
path("api/ping/", include("ping.urls")),
|
path("api/ping/", include("ping.urls")),
|
||||||
|
path("api/countries/", include("countries.urls")),
|
||||||
]
|
]
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
django==4.2.10
|
django==4.2.10
|
||||||
djangorestframework==3.14.0
|
djangorestframework==3.14.0
|
||||||
python-dotenv==1.0.1
|
python-dotenv==1.0.1
|
||||||
|
psycopg2-binary==2.9.9
|
||||||
|
dj-database-url==2.1.0
|
||||||
|
django-filter==23.5
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ indent-width = 4
|
|||||||
|
|
||||||
[lint]
|
[lint]
|
||||||
select = ["ALL"]
|
select = ["ALL"]
|
||||||
ignore = ["D", "ANN", "ARG"]
|
ignore = ["D", "ANN", "ARG", "ISC001", "COM812", "RUF012"]
|
||||||
|
|
||||||
[format]
|
[format]
|
||||||
quote-style = "double"
|
quote-style = "double"
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
DJANGO_DEBUG = False
|
DJANGO_DEBUG = False
|
||||||
SECRET_KEY = fake
|
SECRET_KEY = secret_key
|
||||||
DJANGO_ALLOWED_HOSTS = 127.0.0.1
|
DJANGO_ALLOWED_HOSTS = 127.0.0.1
|
||||||
|
INTERNAL_IPS = 127.0.0.1
|
||||||
|
POSTGRES_CONN = postgres://login:pass@localhost:5432/postgres
|
||||||
|
|||||||
Reference in New Issue
Block a user