Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API feature completed #36

Merged
merged 10 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
FROM python:3.11
ENV PYTHONUNBUFFERED=1 \
POETRY_VIRTUALENVS_CREATE=false
ENV PYTHONUNBUFFERED=1
WORKDIR /app

RUN pip install poetry

COPY ../poetry.lock /app
COPY ../pyproject.toml /app
COPY . .

RUN poetry install --no-root
RUN poetry install

COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
CMD ["poetry", "run", "python", "manage.py", "runserver", "0.0.0.0:8000"]
46 changes: 27 additions & 19 deletions food_helper/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import os
from datetime import timedelta
from pathlib import Path

from .env import env
Expand All @@ -30,7 +31,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("DEBUG")

ALLOWED_HOSTS = []
ALLOWED_HOSTS = [] # if running project from docker add "0.0.0.0"


INSTALLED_APPS = [
Expand Down Expand Up @@ -91,26 +92,26 @@
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

if env("ENVIRONMENT") == "ci":
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "cidb",
}
}
else:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("DB_NAME"),
"USER": env("DB_USER"),
"PASSWORD": env("DB_PASSWORD"),
"HOST": env("DB_HOST"), # localhost db
# "HOST": "db", docker db
"PORT": env("DB_PORT"),
}

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("DB_NAME"),
"USER": env("DB_USER"),
"PASSWORD": env("DB_PASSWORD"),
"HOST": env("DB_HOST"), # localhost db
# "HOST": "db", # docker db
"PORT": env("DB_PORT"),
}
}

REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": ("rest_framework.renderers.JSONRenderer",),
"DEFAULT_PARSER_CLASSES": ("rest_framework.parsers.JSONParser",),
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework_simplejwt.authentication.JWTAuthentication"
],
}

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
Expand Down Expand Up @@ -158,3 +159,10 @@
LOGIN_REDIRECT_URL = "home-page"

LOGIN_URL = "login-page"

SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=120),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
"ROTATE_REFRESH_TOKENS": True,
"BLACKLIST_AFTER_ROTATION": True,
}
17 changes: 1 addition & 16 deletions food_helper/urls.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
"""
URL configuration for food_helper project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path

Expand All @@ -24,4 +8,5 @@
path("", include("products.urls")),
path("", include("user_ingredients.urls")),
path("", include("recipe_ingredients.urls")),
path("", include("ingredients.urls")),
]
Empty file added ingredients/api/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions ingredients/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from ..models import Ingredient


class IngredientSerializer(serializers.ModelSerializer):
class Meta:
model = Ingredient
fields = "__all__"
validators = [
UniqueTogetherValidator(
queryset=Ingredient.objects.all(),
fields=("product", "quantity_type"),
message="Ingredient exists - use get method with parameters",
)
]
11 changes: 11 additions & 0 deletions ingredients/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from ingredients.api.serializers import IngredientSerializer
from ingredients.models import Ingredient


class IngredientViewSet(viewsets.ModelViewSet):
serializer_class = IngredientSerializer
queryset = Ingredient.objects.all()
permission_classes = [IsAuthenticated]
5 changes: 4 additions & 1 deletion ingredients/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2024-01-11 21:33
# Generated by Django 4.2.5 on 2024-06-24 16:24

import django.db.models.deletion
from django.db import migrations, models
Expand Down Expand Up @@ -46,5 +46,8 @@ class Migration(migrations.Migration):
),
),
],
options={
"unique_together": {("product", "quantity_type")},
},
),
]
3 changes: 3 additions & 0 deletions ingredients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ class Ingredient(models.Model):
choices=AMOUNT_TYPE_CHOICES,
)

class Meta:
unique_together = ["product", "quantity_type"]

def __str__(self):
return f"{self.product.name}, {self.quantity_type}"
9 changes: 9 additions & 0 deletions ingredients/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import include, path
from rest_framework.routers import SimpleRouter

from .api.views import IngredientViewSet

router = SimpleRouter()
router.register("ingredients", IngredientViewSet)

urlpatterns = [path("api/", include(router.urls))]
Loading
Loading