Skip to content

Commit

Permalink
Close #18 Create update_recipe function for authenticated users
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-townsend committed Nov 26, 2022
1 parent 35b23fe commit a33cae5
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 16 deletions.
1 change: 1 addition & 0 deletions recipes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ class CommentAdmin(admin.ModelAdmin):

def approve_comments(self, request, queryset):
queryset.update(approved=True)

21 changes: 21 additions & 0 deletions recipes/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .models import Comment, Recipe
from django import forms
from django_summernote.widgets import SummernoteWidget


class CommentForm(forms.ModelForm):
Expand All @@ -9,3 +10,23 @@ class Meta:
fields = ('body',)


class RecipeForm(forms.ModelForm):

class Meta:
model = Recipe
fields = (
'title',
'prep_time',
'cook_time',
'ingredients',
'method',
'notes',
'image',
'status',
)

widgets = {
'ingredients': SummernoteWidget(),
'method': SummernoteWidget(),
'notes': SummernoteWidget(),
}
18 changes: 18 additions & 0 deletions recipes/migrations/0005_alter_recipe_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.16 on 2022-11-26 14:06

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('recipes', '0004_alter_recipe_like_recipe'),
]

operations = [
migrations.AlterField(
model_name='recipe',
name='status',
field=models.IntegerField(choices=[(0, 'Draft'), (1, 'Published')], default=1),
),
]
2 changes: 1 addition & 1 deletion recipes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Recipe(models.Model):
method = models.TextField()
notes = models.TextField()
image = CloudinaryField('image', default='placeholder')
status = models.IntegerField(choices=STATUS, default=0)
status = models.IntegerField(choices=STATUS, default=1)
like_recipe = models.ManyToManyField(
User, related_name='recipe_likes', default=None, blank=True)

Expand Down
3 changes: 2 additions & 1 deletion recipes/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path
from . import views
from .views import RecipeListHome, AddRecipeView
from .views import RecipeListHome, AddRecipeView, UpdateRecipeView

"""URL Patterns"""

Expand All @@ -10,5 +10,6 @@
path('recipe_detail/<slug:slug>/', views.RecipeDetail.as_view(), name='recipe_detail'),
path('like/<slug:slug>/', views.RecipeLike.as_view(), name='recipe_like'),
path('add_recipe/', AddRecipeView.as_view(), name='add_recipe'),
path('update_recipe/<int:recipe_id>/', UpdateRecipeView.as_view(), name='update_recipe'),

]
49 changes: 45 additions & 4 deletions recipes/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.shortcuts import render, get_object_or_404, reverse
from django.shortcuts import render, get_object_or_404, reverse, redirect
from django.views import generic, View
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Recipe, Comment
from .forms import CommentForm
from .forms import CommentForm, RecipeForm
from django.contrib.auth.models import User



class Home(generic.TemplateView):
Expand Down Expand Up @@ -95,7 +98,45 @@ def post(self, request, slug, *args, **kwargs):
return HttpResponseRedirect(reverse('recipe_detail', args=[slug]))


class AddRecipeView(generic.CreateView):
class AddRecipeView(LoginRequiredMixin, generic.CreateView):
model = Recipe
form_class = RecipeForm
template_name = 'add_recipe.html'
fields = '__all__'

def post(self, request):
recipe_form = RecipeForm(data=request.POST)

if recipe_form.is_valid():
recipe = recipe_form.save(commit=False)
recipe.author = User.objects.get(id=request.user.id)
recipe.save()

return redirect(reverse('all_recipes'))


class UpdateRecipeView(LoginRequiredMixin, View):

model = Recipe
form_class = RecipeForm
template_name = 'update_recipe.html'

def get(self, request, recipe_id):
recipe = Recipe.objects.get(id=recipe_id)
print(recipe)
return render(
request,
"update_recipe.html",
{
"form": RecipeForm(instance=recipe),
"recipe": recipe
},
)

def post(self, request, recipe_id):
recipe = Recipe.objects.get(id=recipe_id)
recipe_form = RecipeForm(request.POST, instance=recipe)

if recipe_form.is_valid():
recipe_form.save()

return redirect(reverse('all_recipes'))
6 changes: 3 additions & 3 deletions templates/add_recipe.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

<div class="container">
<h1 class="page-title">Add Recipe</h1>
<hr class="home-break">
<hr>
<br>
<div class="row justify-content-center ">
{% if user.is_authenticated %}
{% load cloudinary %}
<div class="col-11 col-md-8 col-lg-6 mt-3">
<!-- Add Recipe Form -->
<form method="post" class="add-recipe mb-5" enctype="multipart/form-data">
<form method="post" class="add-recipe mb-5" enctype="multipart/form-data">
{{ form | crispy }}
{% csrf_token %}
<a class="btn btn-signup" href="{% url 'all_recipes' %}">Cancel</a>
<a class="btn btn-signup" href="{% url 'add_recipe' %}">Cancel</a>
<button type="submit" class="btn btn-signup">Add recipe</button>
</form>
</div>
Expand Down
9 changes: 5 additions & 4 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'home' %}"
<li class="nav-item">
<a class="nav-link active" href="{% url 'home' %}"
>Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
Expand Down Expand Up @@ -94,15 +94,16 @@
<!-- Bootstrap scripts -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>



<!-- Javascript to automatically close alerts after 2.8 seconds -->
<script>
setTimeout(function() {
let messages = document.getElementById("msg");
let alert = new bootstrap.Alert(messages);
alert.close();
}, 2800);


</script>
</body>

15 changes: 12 additions & 3 deletions templates/recipe_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ <h1 class="page-title text-left">{{ recipe.title }}</h1>
<br>

</span>
{% if request.user.username == recipe.author.username %}
<span class="comment-btn">
<a href=" {% url 'update_recipe' recipe.id %}" aria-label="edit comment"><i
class="fas fa-edit"></i></a>
<a href="#" aria-label="delete comment"><i
class="fas fa-trash-alt"></i></a>
</span>
{% endif %}
<strong>
{% if user.is_authenticated %}

<form class=" d-inline" action="{% url 'recipe_like' recipe.slug %}" method="POST">
{% csrf_token %}
{% if liked %}
Expand Down Expand Up @@ -132,9 +141,9 @@ <h4 class="card-header custom-recipe-detail-header">Comments</h4>
class="fas fa-edit"></i></a>
<a href="#" aria-label="delete comment"><i
class="fas fa-trash-alt"></i></a>
</span>
{% endif %}
<p class="font-weight-normal">
</span>
{% endif %}
<p class="font-weight-normal">
{{ comment.created_date }}
<span class=" text-muted font-weight-bold">
{{ comment.name | safe }}
Expand Down
29 changes: 29 additions & 0 deletions templates/update_recipe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends 'base.html' %}
{% block title %} Kitchen Tales | Update Recipe {{ recipe.title }}{% endblock %}





{% block content %}

<div class="container">
<h1 class="page-title">Add Recipe</h1>
<hr>
<br>
<div class="row justify-content-center ">
{% load cloudinary %}
<div class="col-11 col-md-8 col-lg-6 mt-3">
<!-- Add Recipe Form -->
<form method="post" action="{% url 'update_recipe' recipe.id %}" class="add-recipe mb-5" enctype="multipart/form-data">
{{ form }}
{% csrf_token %}
<a class="btn btn-signup" href="{% url 'update_recipe' recipe.id %}" >Cancel</a>
<input type="submit" class="btn btn-signup" value="Update Recipe" />
</form>
</div>
</div>
</div>


{% endblock content %}

0 comments on commit a33cae5

Please sign in to comment.