Skip to content

Commit

Permalink
getting code ready for production
Browse files Browse the repository at this point in the history
  • Loading branch information
bcg0006 committed May 28, 2024
1 parent 18716c7 commit c8ccada
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_NAME = db_name
DB_USER = db_user
DB_PASS = change_me
DJANGO_SECRET_KEY = change_me
DJANGO_ALLOWED_HOSTS = localhost
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ENV PYTHONUNBUFFERED 1
# Copy the requirements files and the app code into the container
COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements.dev.txt /tmp/requirements.dev.txt
COPY ./scripts /scripts
COPY ./app /app

# Set the working directory to /app
Expand All @@ -28,7 +29,7 @@ RUN python -m venv /py && \
/py/bin/pip install --upgrade pip && \
apk add --update --no-cache postgresql-client jpeg-dev && \
apk add --update --no-cache --virtual .tmp-build-deps \
build-base postgresql-dev musl-dev zlib zlib-dev &&\
build-base postgresql-dev musl-dev zlib zlib-dev linux-headers &&\
/py/bin/pip install -r /tmp/requirements.txt && \
if [ $DEV = "true" ]; \
then /py/bin/pip install -r /tmp/requirements.dev.txt; \
Expand All @@ -42,10 +43,13 @@ RUN python -m venv /py && \
mkdir -p /vol/web/media && \
mkdir -p /vol/web/static && \
chown -R django-user:django-user /vol && \
chmod -R 755 /vol
chmod -R 755 /vol && \
chmod -R +x /scripts

# Add the Python binary path to the container's PATH environment variable
ENV PATH="/py/bin:$PATH"
ENV PATH="/scripts:/py/bin:$PATH"

# Set the user to run the container as
USER django-user
USER django-user

CMD ["run.sh"]
10 changes: 0 additions & 10 deletions app/app/calc.py

This file was deleted.

18 changes: 13 additions & 5 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-^&1#np6re4&e%g^sbe_$woefi7*#wa_+%ta*kc)$)31y#fv97v'
SECRET_KEY = os.environ.get('SECRET_KEY', 'change_me')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['0.0.0.0']
DEBUG = bool(int(os.environ.get('DEBUG', 0)))

ALLOWED_HOSTS = []
ALLOWED_HOSTS.extend(
filter(
None,
os.environ.get('ALLOWED_HOSTS', '').split(','),
)
)

# Application definition

INSTALLED_APPS = [

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand Down Expand Up @@ -146,4 +150,8 @@

REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

SPECTACULAR_SETTINGS = {
'COMPONENT_SPLIT_REQUEST': True,
}
24 changes: 0 additions & 24 deletions app/app/tests.py

This file was deleted.

2 changes: 1 addition & 1 deletion app/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ class UserAdmin(BaseUserAdmin):
admin.site.register(models.User, UserAdmin)
admin.site.register(models.Recipe)
admin.site.register(models.Tag)
admin.site.register(models.Ingredient)
admin.site.register(models.Ingredient)
2 changes: 1 addition & 1 deletion app/recipe/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class RecipeDetailSerializer(RecipeSerializer):
'''Serializer for recipe detail objects'''

class Meta(RecipeSerializer.Meta):
fields = RecipeSerializer.Meta.fields + ['description']
fields = RecipeSerializer.Meta.fields + ['description', 'image']
read_only_fields = ['id']

class RecipeImageSerializer(serializers.ModelSerializer):
Expand Down
56 changes: 54 additions & 2 deletions app/recipe/tests/test_ingredients_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Test for the ingredients API"""
from decimal import Decimal

from django.contrib.auth import get_user_model
from django.urls import reverse
from django.test import TestCase

from rest_framework import status
from rest_framework.test import APIClient

from core.models import Ingredient
from core.models import Ingredient, Recipe

from recipe.serializers import IngredientSerializer

Expand Down Expand Up @@ -88,4 +90,54 @@ def test_delete_ingredient_successful(self):
res = self.client.get(INGREDIENTS_URL)

self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertEqual(len(res.data), 0)
self.assertEqual(len(res.data), 0)

def test_filter_ingredients_assigned_to_recipe(self):
"""Test returning ingredients by those assigned to recipes"""
ingredient1 = Ingredient.objects.create(user=self.user, name='Apples')
ingredient2 = Ingredient.objects.create(user=self.user, name='Turkey')
recipe = Recipe.objects.create(
title='Apple crumble',
time_minutes=5,
price=10.00,
user=self.user,
)
recipe.ingredients.add(ingredient1)

res = self.client.get(
INGREDIENTS_URL,
{'assigned_only': 1}
)

serializer1 = IngredientSerializer(ingredient1)
serializer2 = IngredientSerializer(ingredient2)

self.assertIn(serializer1.data, res.data)
self.assertNotIn(serializer2.data, res.data)

def test_filter_ingredients_assigned_unique(self):
"""Test filtering tags by assigned returns unique items"""
ingredient = Ingredient.objects.create(user=self.user, name='Eggs')
Ingredient.objects.create(user=self.user, name='Cheese')
recipe1 = Recipe.objects.create(
title='Egg benedict',
time_minutes=30,
price=12.00,
user=self.user,
)
recipe1.ingredients.add(ingredient)
recipe2 = Recipe.objects.create(
title='Coriander eggs on toast',
time_minutes=20,
price=5.00,
user=self.user,
)
recipe2.ingredients.add(ingredient)

res = self.client.get(
INGREDIENTS_URL,
{'assigned_only': 1}
)

self.assertEqual(len(res.data), 1)
self.assertEqual(res.data[0]['name'], ingredient.name)
44 changes: 44 additions & 0 deletions app/recipe/tests/test_recipe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,50 @@ def test_clear_recipe_ingredients(self):
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertEqual(recipe.ingredients.count(), 0)

def test_filter_by_tags(self):
"""Test filtering recipe by tags."""
r1 = create_recipe(user= self.user, title="Thai Red Curry")
r2 = create_recipe(user= self.user, title="Spaghetti Aubergine Carbonara")
tags1 = Tag.objects.create(user=self.user, name='Vegan')
tags2 = Tag.objects.create(user=self.user, name='Vegetarian')
r1.tags.add(tags1)
r2.tags.add(tags2)
r3 = create_recipe(user= self.user, title="Fish and Chips")

params = {'tags': f'{tags1.id},{tags2.id}'}
rs = self.client.get(RECIPE_URL, params)

serializer1 = RecipeSerializer(r1)
serializer2 = RecipeSerializer(r2)
serializer3 = RecipeSerializer(r3)

self.assertEqual(rs.status_code, status.HTTP_200_OK)
self.assertIn(serializer1.data, rs.data)
self.assertIn(serializer2.data, rs.data)
self.assertNotIn(serializer3.data, rs.data)

def test_filter_by_ingredients(self):
"""Test filtering recipe by ingredients."""
r1 = create_recipe(user= self.user, title="Thai Red Curry")
r2 = create_recipe(user= self.user, title="Spaghetti Aubergine Carbonara")
ingredient1 = Ingredient.objects.create(user=self.user, name='Coconut Milk')
ingredient2 = Ingredient.objects.create(user=self.user, name='Aubergine')
r1.ingredients.add(ingredient1)
r2.ingredients.add(ingredient2)
r3 = create_recipe(user= self.user, title="Fish and Chips")

params = {'ingredients': f'{ingredient1.id},{ingredient2.id}'}
rs = self.client.get(RECIPE_URL, params)

serializer1 = RecipeSerializer(r1)
serializer2 = RecipeSerializer(r2)
serializer3 = RecipeSerializer(r3)

self.assertEqual(rs.status_code, status.HTTP_200_OK)
self.assertIn(serializer1.data, rs.data)
self.assertIn(serializer2.data, rs.data)
self.assertNotIn(serializer3.data, rs.data)

class RecipeImageUploadTests(TestCase):
def setUp(self):
self.client = APIClient()
Expand Down
54 changes: 52 additions & 2 deletions app/recipe/tests/test_tags_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Test for the tags API"""
from decimal import Decimal
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.test import TestCase

from rest_framework import status
from rest_framework.test import APIClient

from core.models import Tag
from core.models import Tag, Recipe

from recipe.serializers import TagSerializer

Expand Down Expand Up @@ -86,4 +87,53 @@ def test_delete_tag(self):
res = self.client.delete(url)

self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(Tag.objects.count(), 0)
self.assertEqual(Tag.objects.count(), 0)

def test_filter_tags_assigned_to_recipe(self):
""" Test filtering tags by those assigned to recipes """
tag1 = Tag.objects.create(user=self.user, name='Breakfast')
tag2 = Tag.objects.create(user=self.user, name='Lunch')
recipe = Recipe.objects.create(
title='Coriander eggs on toast',
time_minutes=10,
price=Decimal('5.00'),
user=self.user
)
recipe.tags.add(tag1)

res = self.client.get(
TAG_URL,
{'assigned_only': 1}
)

serializer1 = TagSerializer(tag1)
serializer2 = TagSerializer(tag2)

self.assertIn(serializer1.data, res.data)
self.assertNotIn(serializer2.data, res.data)

def test_filtered_tags_unique(self):
""" Test that filtered tags are unique """
tag = Tag.objects.create(user=self.user, name='Breakfast')
Tag.objects.create(user=self.user, name='Lunch')
recipe1 = Recipe.objects.create(
title='Pancakes',
time_minutes=5,
price=3.00,
user=self.user
)
recipe1.tags.add(tag)
recipe2 = Recipe.objects.create(
title='Porridge',
time_minutes=3,
price=2.00,
user=self.user
)
recipe2.tags.add(tag)

res = self.client.get(
TAG_URL,
{'assigned_only': 1}
)

self.assertEqual(len(res.data), 1)
Loading

0 comments on commit c8ccada

Please sign in to comment.