diff --git a/solution/Dockerfile b/solution/Dockerfile index f41cea9..98e52b0 100644 --- a/solution/Dockerfile +++ b/solution/Dockerfile @@ -4,6 +4,7 @@ WORKDIR /app ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 +ENV SERVER_ADDRESS = 0.0.0.0 ENV SERVER_PORT=8080 ENV DJANGO_DEBUG = False @@ -15,4 +16,4 @@ WORKDIR /app 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"] diff --git a/solution/pulse/countries/__init__.py b/solution/pulse/countries/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/solution/pulse/countries/apps.py b/solution/pulse/countries/apps.py new file mode 100644 index 0000000..12ce5ef --- /dev/null +++ b/solution/pulse/countries/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class CountriesConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "countries" diff --git a/solution/pulse/countries/migrations/0001_initial.py b/solution/pulse/countries/migrations/0001_initial.py new file mode 100644 index 0000000..dea5fca --- /dev/null +++ b/solution/pulse/countries/migrations/0001_initial.py @@ -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()), + ], + ), + ] diff --git a/solution/pulse/countries/migrations/__init__.py b/solution/pulse/countries/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/solution/pulse/countries/models.py b/solution/pulse/countries/models.py new file mode 100644 index 0000000..b288235 --- /dev/null +++ b/solution/pulse/countries/models.py @@ -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 diff --git a/solution/pulse/countries/serializers.py b/solution/pulse/countries/serializers.py new file mode 100644 index 0000000..10cfd38 --- /dev/null +++ b/solution/pulse/countries/serializers.py @@ -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"] diff --git a/solution/pulse/countries/urls.py b/solution/pulse/countries/urls.py new file mode 100644 index 0000000..f47ea71 --- /dev/null +++ b/solution/pulse/countries/urls.py @@ -0,0 +1,12 @@ +from django.urls import path + +import countries.views + +urlpatterns = [ + path("", countries.views.CountryListView.as_view(), name="countries"), + path( + "/", + countries.views.CountryByAlpha2View.as_view(), + name="country_by_alpha2", + ), +] diff --git a/solution/pulse/countries/views.py b/solution/pulse/countries/views.py new file mode 100644 index 0000000..b9e4a84 --- /dev/null +++ b/solution/pulse/countries/views.py @@ -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" diff --git a/solution/pulse/pulse/settings.py b/solution/pulse/pulse/settings.py index e159905..62a52a8 100644 --- a/solution/pulse/pulse/settings.py +++ b/solution/pulse/pulse/settings.py @@ -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", - ], - } diff --git a/solution/pulse/pulse/urls.py b/solution/pulse/pulse/urls.py index 340ccd3..3d9904b 100644 --- a/solution/pulse/pulse/urls.py +++ b/solution/pulse/pulse/urls.py @@ -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: diff --git a/solution/requirements/prod.txt b/solution/requirements/prod.txt index 0d4898e..57680ab 100644 --- a/solution/requirements/prod.txt +++ b/solution/requirements/prod.txt @@ -1,3 +1,6 @@ django==4.2.10 djangorestframework==3.14.0 python-dotenv==1.0.1 +psycopg2-binary==2.9.9 +dj-database-url==2.1.0 +django-filter==23.5 diff --git a/solution/ruff.toml b/solution/ruff.toml index 0d3bab9..5334195 100644 --- a/solution/ruff.toml +++ b/solution/ruff.toml @@ -8,7 +8,7 @@ indent-width = 4 [lint] select = ["ALL"] -ignore = ["D", "ANN", "ARG"] +ignore = ["D", "ANN", "ARG", "ISC001", "COM812", "RUF012"] [format] quote-style = "double" diff --git a/solution/template.env b/solution/template.env index 20eedb5..7170c0a 100644 --- a/solution/template.env +++ b/solution/template.env @@ -1,3 +1,5 @@ DJANGO_DEBUG = False -SECRET_KEY = fake +SECRET_KEY = secret_key DJANGO_ALLOWED_HOSTS = 127.0.0.1 +INTERNAL_IPS = 127.0.0.1 +POSTGRES_CONN = postgres://login:pass@localhost:5432/postgres