diff --git a/comments/migrations/0001_initial.py b/comments/migrations/0001_initial.py index dae5d3b..bd352ea 100644 --- a/comments/migrations/0001_initial.py +++ b/comments/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.4 on 2019-10-08 22:39 +# Generated by Django 2.2.8 on 2020-05-28 18:37 from django.conf import settings from django.db import migrations, models diff --git a/recipes/admin.py b/recipes/admin.py index c4e050a..3d84e6e 100644 --- a/recipes/admin.py +++ b/recipes/admin.py @@ -11,3 +11,4 @@ admin.site.register(Nutrition) admin.site.register(Tip) + diff --git a/recipes/migrations/0001_initial.py b/recipes/migrations/0001_initial.py index de1dcf6..0cc4264 100644 --- a/recipes/migrations/0001_initial.py +++ b/recipes/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.4 on 2019-10-08 22:39 +# Generated by Django 2.2.8 on 2020-05-28 18:37 import common.utils import datetime @@ -50,11 +50,12 @@ class Migration(migrations.Migration): ('max_servings', models.PositiveIntegerField(default=1)), ('difficulty', models.SmallIntegerField(choices=[(1, 'easy'), (2, 'medium'), (3, 'hard')])), ('ingredients', models.ManyToManyField(related_name='in_recipes', to='recipes.Ingredient')), + ('stars', models.ManyToManyField(blank=True, related_name='recipe_stars', to=settings.AUTH_USER_MODEL)), ('user', models.ForeignKey(on_delete=models.SET(common.utils.get_system_user), related_name='recipes', to=settings.AUTH_USER_MODEL)), ('users_who_made_this', models.ManyToManyField(related_name='made_recipes', to=settings.AUTH_USER_MODEL)), ], options={ - 'abstract': False, + 'ordering': ('-created_at',), }, ), migrations.CreateModel( diff --git a/recipes/models.py b/recipes/models.py index b02a499..0a75b06 100644 --- a/recipes/models.py +++ b/recipes/models.py @@ -28,6 +28,7 @@ class Recipe(BaseModel): max_servings = models.PositiveIntegerField(default=1) difficulty = models.SmallIntegerField(choices=DIFFICULTY_CHOICES) votes = models.ManyToManyField(AUTH_USER_MODEL, related_name='voted_recipes', through='recipes.Vote') + stars = models.ManyToManyField(AUTH_USER_MODEL, blank=True, related_name='recipe_stars') class Meta: ordering = ('-created_at', ) diff --git a/recipes/templates/recipes/recipe_detail.html b/recipes/templates/recipes/recipe_detail.html index 6dce169..bd4664a 100644 --- a/recipes/templates/recipes/recipe_detail.html +++ b/recipes/templates/recipes/recipe_detail.html @@ -16,6 +16,14 @@

Recipe Details

Cook-time: {{ recipe.cook_time }}

Servings: {{ recipe.servings }}

Difficulty: {{ recipe.difficulty }}

+

Stars: {{ recipe.stars.count }}

+ + {% if request.user in recipe.stars.all %} + Star down
+ {% else %} + Star up
+ {% endif %} + {% if recipe.user == user or perms.recipes.change_recipe %} edit {% endif %} diff --git a/recipes/templates/recipes/recipe_list.html b/recipes/templates/recipes/recipe_list.html index 0020592..9d6afc3 100644 --- a/recipes/templates/recipes/recipe_list.html +++ b/recipes/templates/recipes/recipe_list.html @@ -7,6 +7,7 @@

Recipes

{% if user.is_authenticated %} {{ user.username }} + My Saved Recipes Logout {% else %} Login @@ -18,6 +19,7 @@

Recipes

  • {{ recipe.title }}


    view +

    Stars: {{ recipe.stars.count }}


  • {% endfor %} diff --git a/recipes/urls.py b/recipes/urls.py index 6e85909..c954937 100644 --- a/recipes/urls.py +++ b/recipes/urls.py @@ -3,7 +3,7 @@ from django_filters.views import FilterView from recipes.views import (RecipeCreateView, RecipeDetailView, RecipeListView, - RecipeUpdateView, RecipeDeleteView) + RecipeUpdateView, RecipeDeleteView, RecipeStarToggle, MyStarredRecipes) from recipes.filters import RecipeFilter recipe_patterns = [ @@ -12,6 +12,7 @@ path('create/', RecipeCreateView.as_view(), name='create'), path('update//', RecipeUpdateView.as_view(), name='update'), path('delete//', RecipeDeleteView.as_view(), name='delete'), + path('detail//star/', RecipeStarToggle.as_view(), name='star-toggle'), path('detail//', RecipeDetailView.as_view(), name='detail'), - + path('starred/', MyStarredRecipes.as_view(), name='starred'), ] diff --git a/recipes/views.py b/recipes/views.py index 88069e4..41a2f33 100644 --- a/recipes/views.py +++ b/recipes/views.py @@ -1,7 +1,8 @@ from django.contrib.auth.mixins import LoginRequiredMixin -from django.urls import reverse_lazy +from django.urls import reverse_lazy, reverse +from django.shortcuts import get_object_or_404 from django.views.generic import (CreateView, DetailView, DeleteView, - ListView, UpdateView) + ListView, UpdateView, RedirectView) from recipes.mixins import AdminOrOwnerPermissionMixin from recipes.models import Recipe @@ -42,6 +43,36 @@ def get_context_data(self, **kwargs): return context +class RecipeStarToggle(RedirectView): + + """ + Lets user give and remove a star to a recipe + """ + + def get_redirect_url(self, *args, **kwargs): + pk = self.kwargs.get("pk") + recipe = get_object_or_404(Recipe, pk=pk) + user = self.request.user + if user.is_authenticated: + if user in recipe.stars.all(): + recipe.stars.remove(user) + else: + recipe.stars.add(user) + return reverse('recipes:detail', args=[recipe.pk]) + + +class MyStarredRecipes(ListView): + + """ + View that shows a user their starred(favorite) recipes + """ + + model = Recipe + + def get_queryset(self): + return Recipe.objects.filter(stars__username=self.request.user.username) + + class RecipeUpdateView(AdminOrOwnerPermissionMixin, UpdateView): """ View to delete a recipe