Skip to content

Commit

Permalink
Re-deploy the Foodgram due to a server reboot.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkoov committed May 18, 2024
1 parent 4feb9cc commit 1c8d6a0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 118 deletions.
26 changes: 7 additions & 19 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ def create_shopping_list_pdf(self, shoppings):
TTFSearchPath.append(str(settings.BASE_DIR) + "/data/fonts/")
buffer = io.BytesIO()
p = canvas.Canvas(buffer, pagesize=A4)
p.drawImage(
"fg_logo_for_shopping_list.png", 30, 790, width=20, height=20
)
p.drawImage("fg_logo_for_shopping_list.png", 30, 790, width=20, height=20)
pdfmetrics.registerFont(TTFont("DejaVuSans", "DejaVuSans.ttf"))
pdfmetrics.registerFont(
TTFont("DejaVuSansBold", "DejaVuSans-Bold.ttf")
)
pdfmetrics.registerFont(TTFont("DejaVuSansBold", "DejaVuSans-Bold.ttf"))
p.setFont("DejaVuSans", 12)
p.drawRightString(550, 800, "Shopping list, Foodgram")
p.setFont("DejaVuSansBold", 10)
Expand Down Expand Up @@ -195,9 +191,7 @@ def remove_recipe(model, user, recipe):

class BaseFavoriteShoppingCartViewSet(ModelViewSet):
model: type[Favorite] | type[ShoppingCart] | None
serializer_class: (
type[FavoriteSerializer] | type[ShoppingCartSerializer] | None
)
serializer_class: type[FavoriteSerializer] | type[ShoppingCartSerializer] | None

def create(self, request, **kwargs):
item_id = self.kwargs.get("id")
Expand All @@ -209,17 +203,13 @@ def create(self, request, **kwargs):
)
new_item = self.model(user=request.user, recipe=item)
new_item.save()
serializer = self.serializer_class(
new_item, context={"request": request}
)
serializer = self.serializer_class(new_item, context={"request": request})
return Response(serializer.data, status=status.HTTP_201_CREATED)

def delete(self, request, **kwargs):
item_id = self.kwargs.get("id")
item = get_object_or_404(Recipe, id=item_id)
if not self.model.objects.filter(
user=request.user, recipe=item
).exists():
if not self.model.objects.filter(user=request.user, recipe=item).exists():
return Response(
_("No recipe to delete."),
status=status.HTTP_400_BAD_REQUEST,
Expand All @@ -238,10 +228,10 @@ class ShoppingCartViewSet(BaseFavoriteShoppingCartViewSet):
class TagViewSet(ReadOnlyModelViewSet):
serializer_class = TagSerializer
queryset = Tag.objects.all()
permission_classes = (permissions.AllowAny,)
filter_backends = (DjangoFilterBackend,)
filterset_fields = ("slug",)
pagination_class = None
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class IngredientViewSet(ReadOnlyModelViewSet):
Expand Down Expand Up @@ -287,9 +277,7 @@ def subscribe_user(request, i_d):
author = get_object_or_404(User, id=i_d)
if request.method == "DELETE":
try:
subscription = get_object_or_404(
Subscription, user=user, author=author
)
subscription = get_object_or_404(Subscription, user=user, author=author)
subscription.delete()
return Response(
{"success": _("Subscription deleted.")},
Expand Down
182 changes: 87 additions & 95 deletions backend/recipes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class RecipeTests(APITestCase):
}
default_images = [
"front-view-arrangement-healthy-breakfast-meal-with-yogurt.jpg",
"vertical-shot-delicious-vegetable-meatballs-with-creamy-sauce.resized."
"jpg",
"vertical-shot-delicious-vegetable-meatballs-with-creamy-sauce.resized." "jpg",
"korean-fish-cake-vegetable-soup-table.jpg",
"lunch.resized.jpg",
"dinner.resized.jpg",
Expand Down Expand Up @@ -124,7 +123,8 @@ def test_list_tags(self):
def test_tag_detail(self):
id_ = len(self.test_tags)
self.assertTrue(id_ >= 1)
response = self.client.get(f"{self.tags_url}{id_}/")
# For some reason, tag details are not controlled by the AllowAny perm
response = self.api_client.get(f"{self.tags_url}{id_}/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
tag = Tag.objects.get(id=id_)
self.assertEqual(
Expand All @@ -138,6 +138,7 @@ def test_tag_detail(self):
)
id_ = len(self.test_tags) + 1
response = self.api_client.get(f"{self.tags_url}{id_}/")
# print(json.loads(response.content))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_list_ingredients(self):
Expand All @@ -157,9 +158,7 @@ def test_list_ingredients(self):
)
for x in Ingredient.objects.all():
self.assertTrue(len(x.name) <= NUM_CHARS_INGREDIENT_NAME)
self.assertTrue(
len(x.measurement_unit) <= NUM_CHARS_MEASUREMENT_UNIT
)
self.assertTrue(len(x.measurement_unit) <= NUM_CHARS_MEASUREMENT_UNIT)
tmp_ingredients.append(x.name)
self.assertEqual(Ingredient.objects.count(), len(set(tmp_ingredients)))

Expand All @@ -170,9 +169,7 @@ def test_ingredient_search(self):
measurement_unit="shovel",
)
self.assertEqual(Ingredient.objects.count(), count_ini + 1)
response = self.client.get(
f"{self.ingredients_url}?name=find_me%20ingredient"
)
response = self.client.get(f"{self.ingredients_url}?name=find_me%20ingredient")
self.assertEqual(response.status_code, status.HTTP_200_OK)
if TEST_NUM_INGREDIENTS == 2000:
self.assertEqual(
Expand All @@ -188,9 +185,7 @@ def test_ingredient_search(self):
response = self.client.get(f"{self.ingredients_url}?name=Ingredient")
self.assertEqual(response.status_code, status.HTTP_200_OK)
if TEST_NUM_INGREDIENTS == 2000:
self.assertEqual(
len(json.loads(response.content)), TEST_NUM_INGREDIENTS
)
self.assertEqual(len(json.loads(response.content)), TEST_NUM_INGREDIENTS)

def test_ingredient_detail(self):
id_ = len(self.test_ingredients)
Expand Down Expand Up @@ -239,9 +234,7 @@ def test_create_recipe(self):
# deleted.
self.delete_tmp_images()
self.log_in_and_tokenize_user()
response = self.api_client.post(
self.recipes_url, recipe_data, format="json"
)
response = self.api_client.post(self.recipes_url, recipe_data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

self.assertEqual(Recipe.objects.count(), recipe_count_ini + 1)
Expand All @@ -251,15 +244,9 @@ def test_create_recipe(self):
tag_1 = Tag.objects.get(id=recipe_data["tags"][0])
tag_2 = Tag.objects.get(id=recipe_data["tags"][1])
first_test_user = User.objects.get(id=1)
ingredient_1 = Ingredient.objects.get(
id=recipe_data["ingredients"][0]["id"]
)
ingredient_2 = Ingredient.objects.get(
id=recipe_data["ingredients"][1]["id"]
)
ingredient_3 = Ingredient.objects.get(
id=recipe_data["ingredients"][2]["id"]
)
ingredient_1 = Ingredient.objects.get(id=recipe_data["ingredients"][0]["id"])
ingredient_2 = Ingredient.objects.get(id=recipe_data["ingredients"][1]["id"])
ingredient_3 = Ingredient.objects.get(id=recipe_data["ingredients"][2]["id"])
img_path = f"{TEST_SERVER_URL}/media/recipes/"
for image in os.listdir(MEDIA_ROOT / "recipes"):
if image not in self.default_images:
Expand Down Expand Up @@ -328,9 +315,7 @@ def test_create_recipe(self):
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Recipe.objects.count(), recipe_count_ini)
response = self.client.post(
self.recipes_url, self.recipe_data, format="json"
)
response = self.client.post(self.recipes_url, self.recipe_data, format="json")
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
self.assertEqual(Recipe.objects.count(), recipe_count_ini)

Expand Down Expand Up @@ -358,7 +343,8 @@ def test_recipe_patch(self):
self.assertTrue(id_ >= 1)
response = self.client.get(f"{self.recipes_url}{id_}/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
# self.log_in_and_tokenize_user()
self.assertIn("image", json.loads(response.content))
img_path = json.loads(response.content)["image"] # Keep unchanged
patch_data = {
"ingredients": [
{
Expand All @@ -375,81 +361,80 @@ def test_recipe_patch(self):
"text": "Patched cooking instructions now",
"cooking_time": 1,
}
for patcher in (self.client, self.api_client):
response = patcher.patch(
f"{self.recipes_url}{id_}/", patch_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

response = self.client.patch(
f"{self.recipes_url}{id_}/", patch_data, format="json"
f"where-did-you-get-this-url/{id_}/", patch_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

user = User.objects.get(id=1)
self.api_client.force_authenticate(user=user)
self.log_in_and_tokenize_user()
response = self.api_client.patch(
f"{self.recipes_url}{id_}/", patch_data, format="json"
)
self.log_out_and_detokenize_user()
self.assertEqual(response.status_code, status.HTTP_200_OK)
recipe = Recipe.objects.get(id=id_)
user = User.objects.get(id=recipe.author.id)

tags = []
for tag in patch_data["tags"]:
t = Tag.objects.get(id=tag)
tags.append({"id": t.id, "name": t.name, "color": t.color, "slug": t.slug})
ingredients = []
for ingredient in patch_data["ingredients"]:
i = Ingredient.objects.get(id=ingredient["id"])
ingredients.append(
{
"amount": ingredient["amount"],
"id": ingredient["id"],
"measurement_unit": i.measurement_unit,
"name": i.name,
}
)
self.assertEqual(
json.loads(response.content),
{
"id": id_,
"tags": tags,
"author": {
"email": user.email,
"id": user.id,
"username": user.username,
"first_name": user.first_name,
"last_name": user.last_name,
"is_subscribed": False,
},
"ingredients": ingredients,
"is_favorited": False,
"is_in_shopping_cart": False,
"name": patch_data["name"],
"image": img_path,
"text": patch_data["text"],
"cooking_time": patch_data["cooking_time"],
},
)
# 403
# response = self.client.patch(
# f"{self.recipes_url}{id_}/", patch_data, format="json"
# )
# self.assertEqual(
# response.status_code,
# status.HTTP_403_FORBIDDEN,
# "Must be the 403 status code!",
# )

self.api_client.logout()
self.log_in_and_tokenize_user()
patch_data["ingredients"] = None
patch_data["name"] = None
response = self.api_client.patch(
f"{self.recipes_url}{id_}/", patch_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

# self.api_client.logout()

#
# response = self.api_client_another.patch(
# f"{self.recipes_url}{id_}/", patch_data, format="json"
# )
# self.assertEqual(
# response.status_code,
# status.HTTP_403_FORBIDDEN,
# "Must be the 403 status code!",
# )
#
# for patcher in readers:
# patcher.logout()
# response = patcher.patch(
# f"{self.recipes_url}{id_}/", patch_data, format="json"
# )
# self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
#
# # response = self.api_client_another
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

# response = self.api_client.patch(
# f"where-did-you-get-this-url/{id_}/", patch_data, format="json"
# )
# self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
#
# response = self.api_client_another.patch(
# f"{self.recipes_url}{id_}/", patch_data, format="json"
# )
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
#
# patch_data["ingredients"] = None
# patch_data["name"] = None
# response = self.api_client.patch(
# f"{self.recipes_url}{id_}/", patch_data, format="json"
# )
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

# # Json structure response checked here?

#

#
# # 403
# patch_data["ingredients"] = [
# {
# "id": 55,
# "amount": 55,
# },
# ]
# patch_data["name"] = "Patched by a second user"
# response = self.api_client_another.patch(
# f"{self.recipes_url}{id_}/", patch_data, format="json"
# )
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
# # pprint(json.loads(response.content))
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_list_recipes(self):
response = self.client.get(self.recipes_url)
Expand Down Expand Up @@ -504,6 +489,15 @@ def create_test_user(cls):
assert response.status_code == status.HTTP_200_OK
cls.api_client.logout()

user_data = {
"email": "another@user.com",
"username": "another_user",
"first_name": "AnotherTest",
"last_name": "User",
"password": "wEva_$",
}
User.objects.create(**user_data)

@classmethod
def delete_tmp_images(cls):
for image in os.listdir(MEDIA_ROOT / "recipes"):
Expand Down Expand Up @@ -576,9 +570,7 @@ def log_in_and_tokenize_user(cls):
"password": cls.user_data["password"],
"email": cls.user_data["email"],
}
response = cls.api_client.post(
cls.login_url, login_data, format="json"
)
response = cls.api_client.post(cls.login_url, login_data, format="json")
assert "auth_token" in json.loads(response.content)
token = Token.objects.get(user__username=cls.user_data["username"])
cls.api_client.credentials(HTTP_AUTHORIZATION="Token " + token.key)
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1c8d6a0

Please sign in to comment.