Skip to content

Commit

Permalink
Added: random recipe page
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Jul 27, 2023
1 parent 100e3d7 commit 1766980
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
15 changes: 15 additions & 0 deletions recipes/repositories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import random
import typing

import django.db.models as djmodels

import recipes.models as models


class RecipesRepository:
@classmethod
def get_random(cls) -> typing.Union[models.Recipe, None]:
max_id = models.Recipe.objects.aggregate(max_id=djmodels.Max("id"))["max_id"]
random_id = random.randint(1, max_id)

return models.Recipe.objects.filter(id__gte=random_id).first()
7 changes: 6 additions & 1 deletion recipes/templates/recipes/recipe_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
{% load static %}

{% block header %}
<h1>{% block title %}Рецепты{% endblock %}</h1>
<div class="ui menu">
<h1 class="ui header item">{% block title %}Рецепты{% endblock %}</h1>
<a class="item" href="{% url 'recipes:random' %}">
<i class="random icon"></i> Случайный рецепт
</a>
</div>
{% endblock %}

{% block content %}
Expand Down
1 change: 1 addition & 0 deletions recipes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
app_name = "recipes"
urlpatterns = [
path("", cache_page(60)(views.RecipesListView.as_view()), name="list"),
path("recipe/random", views.RandomRecipeView.as_view(), name="random"),
path(
"recipe/<int:pk>/",
cache_page(3600)(views.RecipeDetailsView.as_view()),
Expand Down
27 changes: 18 additions & 9 deletions recipes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
from typing import Any, Dict

from django.contrib.messages.views import SuccessMessageMixin
from django.http import (
HttpRequest,
HttpResponse,
HttpResponseBadRequest,
HttpResponseNotFound,
)
import django.http as http
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views import View
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView

from recipes import repositories

from . import forms, models


Expand Down Expand Up @@ -71,18 +69,29 @@ def get_object(self):
)


class RandomRecipeView(View):
def get(self, request: http.HttpRequest, *args, **kwargs) -> http.HttpResponse:
recipe = repositories.RecipesRepository.get_random()
if not recipe:
raise http.Http404()

return http.HttpResponseRedirect(
reverse("recipes:details", kwargs={"pk": recipe.pk})
)


class CommentAddView(SuccessMessageMixin, CreateView):
model = models.Comment
form_class = forms.CommentForm
success_message: str = "Комментарий добавлен. Он будет отображен после модерации."

def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
def post(self, request: http.HttpRequest, *args, **kwargs) -> http.HttpResponse:
if "recipe_id" not in kwargs:
return HttpResponseBadRequest()
return http.HttpResponseBadRequest()
recipe_id = kwargs["recipe_id"]
self.recipe = models.Recipe.objects.get(pk=recipe_id)
if not self.recipe:
return HttpResponseNotFound()
return http.HttpResponseNotFound()

return super().post(request, *args, **kwargs)

Expand Down

0 comments on commit 1766980

Please sign in to comment.