Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starring system #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion comments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions recipes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
admin.site.register(Nutrition)
admin.site.register(Tip)


5 changes: 3 additions & 2 deletions recipes/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions recipes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', )
Expand Down
8 changes: 8 additions & 0 deletions recipes/templates/recipes/recipe_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ <h1 class="display-4">Recipe Details</h1>
<p>Cook-time: {{ recipe.cook_time }}</p>
<p>Servings: {{ recipe.servings }}</p>
<p>Difficulty: {{ recipe.difficulty }}</p>
<p>Stars: {{ recipe.stars.count }}<br></p>

{% if request.user in recipe.stars.all %}
<a href={% url 'recipes:star-toggle' recipe.pk %}>Star down</a><br>
{% else %}
<a href={% url 'recipes:star-toggle' recipe.pk %}>Star up</a><br>
{% endif %}

{% if recipe.user == user or perms.recipes.change_recipe %}
<a class="btn btn-outline-secondary" href="{% url "recipes:update" recipe.pk %}">edit</a>
{% endif %}
Expand Down
2 changes: 2 additions & 0 deletions recipes/templates/recipes/recipe_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ <h1>Recipes</h1>
<div class="float-md-right">
{% if user.is_authenticated %}
{{ user.username }}
<a href="{% url "recipes:starred" %}">My Saved Recipes</a>
<a href="{% url "account_logout" %}">Logout</a>
{% else %}
<a href="{% url "account_login" %}">Login</a>
Expand All @@ -18,6 +19,7 @@ <h1>Recipes</h1>
<li class="list-group-item">
<h4>{{ recipe.title }}</h4><br>
<a href="{% url "recipes:detail" recipe.pk %}">view</a>
<p>Stars: {{ recipe.stars.count }}</p>
</li>
<br>
{% endfor %}
Expand Down
5 changes: 3 additions & 2 deletions recipes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -12,6 +12,7 @@
path('create/', RecipeCreateView.as_view(), name='create'),
path('update/<int:pk>/', RecipeUpdateView.as_view(), name='update'),
path('delete/<int:pk>/', RecipeDeleteView.as_view(), name='delete'),
path('detail/<int:pk>/star/', RecipeStarToggle.as_view(), name='star-toggle'),
path('detail/<int:pk>/', RecipeDetailView.as_view(), name='detail'),

path('starred/', MyStarredRecipes.as_view(), name='starred'),
]
35 changes: 33 additions & 2 deletions recipes/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down