Skip to content

Commit

Permalink
Formating.
Browse files Browse the repository at this point in the history
  • Loading branch information
EwokJedi committed Nov 9, 2022
1 parent 82afad9 commit 9ee2da7
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 61 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.linting.enabled": true,
"python.linting.flake8Enabled": true
}
2 changes: 1 addition & 1 deletion app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT,
)
)
5 changes: 3 additions & 2 deletions app/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class UserAdmin(BaseUserAdmin):
'is_active',
'is_staff',
'is_superuser',

)
}),
)


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)
4 changes: 3 additions & 1 deletion app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class User(AbstractBaseUser, PermissionsMixin):

USERNAME_FIELD = 'email'


class Recipe(models.Model):
"""Recipe object."""
user = models.ForeignKey(
Expand Down Expand Up @@ -85,6 +86,7 @@ class Tag(models.Model):
def __str__(self):
return self.name


class Ingredient(models.Model):
"""Ingredient for recipes."""
name = models.CharField(max_length=255)
Expand All @@ -94,4 +96,4 @@ class Ingredient(models.Model):
)

def __str__(self):
return self.name
return self.name
3 changes: 1 addition & 2 deletions app/core/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Test for the Django admin modifications.
"""
from re import L
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.urls import reverse
Expand Down Expand Up @@ -45,4 +44,4 @@ def test_create_user_page(self):
url = reverse('admin:core_user_add')
res = self.client.get(url)

self.assertEqual(res.status_code, 200)
self.assertEqual(res.status_code, 200)
5 changes: 3 additions & 2 deletions app/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from core import models


def create_user(email='user@example.com', password='testpass123'):
"""Create and return a new user."""
return get_user_model().objects.create_user(email, password)
Expand Down Expand Up @@ -40,7 +41,7 @@ def test_new_user_email_normalized(self):
for email, expected in sample_emails:
user = get_user_model().objects.create_user(email, 'sample123')
self.assertEqual(user.email, expected)

def test_new_user_without_email_raises_error(self):
"""Test that creating a user without an email raises a ValueError."""
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -96,4 +97,4 @@ def test_recipe_file_name_uuid(self, mock_uuid):
mock_uuid.return_value = uuid
file_path = models.recipe_image_file_path(None, 'example.jpg')

self.assertEqual(file_path, f'uploads/recipe/{uuid}.jpg')
self.assertEqual(file_path, f'uploads/recipe/{uuid}.jpg')
29 changes: 16 additions & 13 deletions app/recipe/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
Ingredient,
)


class IngredientSerializer(serializers.ModelSerializer):
"""Serializer for ingredients."""

class Meta:
model = Ingredient
fields = ['id', 'name']
Expand All @@ -29,17 +30,18 @@ class Meta:
class RecipeSerializer(serializers.ModelSerializer):
"""Serializer for recipe"""
tags = TagSerializer(many=True, required=False)
ingredients = IngredientSerializer(many=True, required=False)
ingredients = IngredientSerializer(many=True, required=False)

class Meta:
model = Recipe
fields = ['id',
'title',
'time_minutes',
'price',
'link',
'tags',
'ingredients',
fields = [
'id',
'title',
'time_minutes',
'price',
'link',
'tags',
'ingredients',
]
read_only_fields = ['id']

Expand All @@ -64,8 +66,8 @@ def _get_or_create_ingredients(self, ingredients, recipe):
recipe.ingredients.add(ingredient_obj)

def create(self, validated_data):
tags = validated_data.pop('tags',[])
ingredients = validated_data.pop('ingredients',[])
tags = validated_data.pop('tags', [])
ingredients = validated_data.pop('ingredients', [])
recipe = Recipe.objects.create(**validated_data)
self._get_or_create_tags(tags, recipe)
self._get_or_create_ingredients(ingredients, recipe)
Expand All @@ -85,10 +87,11 @@ def update(self, instance, validated_data):

for attr, value in validated_data.items():
setattr(instance, attr, value)

instance.save()
return instance


class RecipeDetailSerializer(RecipeSerializer):
"""Serializer for recipe detail view"""

Expand All @@ -103,4 +106,4 @@ class Meta:
model = Recipe
fields = ['id', 'image']
read_only_fields = ['id']
extra_kwargs = {'image': {'required': 'True'}}
extra_kwargs = {'image': {'required': 'True'}}
11 changes: 6 additions & 5 deletions app/recipe/tests/test_ingredients_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

from recipe.serializers import IngredientSerializer
Expand All @@ -25,6 +25,7 @@ def detail_url(ingredient_id):
"""Create and return an ingredient detail URL."""
return reverse('recipe:ingredient-detail', args=[ingredient_id])


def create_user(email='user@example.com', password='testpass123'):
"""Create and return user."""
return get_user_model().objects.create_user(email=email, password=password)
Expand All @@ -45,7 +46,7 @@ def test_auth_required(self):

class PrivateIngredientsAPITests(TestCase):
"""Test authenticated API requests."""

def setUp(self):
self.user = create_user()
self.client = APIClient()
Expand Down Expand Up @@ -75,7 +76,7 @@ def test_ingredients_limited_to_user(self):
self.assertEqual(len(res.data), 1)
self.assertEqual(res.data[0]['name'], ingredient.name)
self.assertEqual(res.data[0]['id'], ingredient.id)

def test_update_ingredient(self):
"""Test updating an ingredient."""
ingredient = Ingredient.objects.create(user=self.user, name="Cilantro")
Expand Down Expand Up @@ -111,7 +112,7 @@ def test_filter_ingredients_assigned_to_recipes(self):
)
recipe.ingredients.add(in1)

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

s1 = IngredientSerializer(in1)
s2 = IngredientSerializer(in2)
Expand Down Expand Up @@ -139,4 +140,4 @@ def test_filtered_ingredients_unique(self):

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

self.assertEqual(len(res.data), 1)
self.assertEqual(len(res.data), 1)
23 changes: 15 additions & 8 deletions app/recipe/tests/test_recipe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@

RECIPES_URL = reverse('recipe:recipe-list')


def detail_url(recipe_id):
"""Create and return a recipe detail URL."""
return reverse('recipe:recipe-detail', args=[recipe_id])


def image_upload_url(recipe_id):
"""Create and return an image upload URL."""
return reverse('recipe:recipe-upload-image', args=[recipe_id])


def create_recipe(user, **params):
"""Create and return a sample recipe"""
default = {
Expand All @@ -49,6 +52,7 @@ def create_recipe(user, **params):
recipe = Recipe.objects.create(user=user, **default)
return recipe


def create_user(**params):
"""Create and return a new user."""
return get_user_model().objects.create_user(**params)
Expand Down Expand Up @@ -134,10 +138,10 @@ def test_partial_update(self):
recipe = create_recipe(
user=self.user,
title='Sample recipe title',
link = original_link,
link=original_link,
)

payload = {'title':'New recipe title'}
payload = {'title': 'New recipe title'}
url = detail_url(recipe.id)
res = self.client.patch(url, payload)

Expand Down Expand Up @@ -196,7 +200,10 @@ def test_delete_recipe(self):

def test_recipe_other_users_recipe_error(self):
"""Test trying to delete another users recipe gives error."""
new_user = create_user(email='user2@example.com', password="password123")
new_user = create_user(
email='user2@example.com',
password="password123"
)
recipe = create_recipe(user=new_user)

url = detail_url(recipe.id)
Expand Down Expand Up @@ -284,7 +291,7 @@ def test_clear_recipe_tags(self):
recipe = create_recipe(user=self.user)
recipe.tags.add(tag)

payload = {'tags':[]}
payload = {'tags': []}
url = detail_url(recipe.id)
res = self.client.patch(url, payload, format='json')

Expand All @@ -297,7 +304,7 @@ def create_recipe_with_new_ingredient(self):
'title': 'Cauliflower Tacos',
'time_minutes': 60,
'price': Decimal('4.30'),
'ingredients': [{'name':'Cauliflower'},{'name': 'Salt'}],
'ingredients': [{'name': 'Cauliflower'}, {'name': 'Salt'}],
}
res = self.client.post(RECIPES_URL, payload, format='json')

Expand Down Expand Up @@ -416,10 +423,10 @@ def test_filter_by_ingredients(self):
self.assertIn(s2.data, res.data)
self.assertNotIn(s3.data, res.data)


class ImageUploadTests(TestCase):
"""Tests for the image upload API."""

def setUp(self):
self.client = APIClient()
self.user = get_user_model().objects.create_user(
Expand Down Expand Up @@ -453,4 +460,4 @@ def test_upload_image_bad_request(self):
payload = {'image': 'notanimage'}
res = self.client.post(url, payload, format='multipart')

self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
8 changes: 4 additions & 4 deletions app/recipe/tests/test_tags_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tests 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
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_tags_limited_to_user(self):
def test_update_tag(self):
"""Test updating tag."""
tag = Tag.objects.create(user=self.user, name='After dinner')

payload = {'name': 'Dessert'}
url = detail_url(tag.id)
res = self.client.patch(url, payload)
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_filtered_tags_unique(self):
)
recipe1.tags.add(tag)
recipe2.tags.add(tag)

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

self.assertEqual(len(res.data), 1)
self.assertEqual(len(res.data), 1)
2 changes: 1 addition & 1 deletion app/recipe/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

urlpatterns = [
path('', include(router.urls))
]
]
Loading

0 comments on commit 9ee2da7

Please sign in to comment.