This commit is contained in:
vabene1111
2019-11-14 10:06:51 +01:00
parent a6ce4edc93
commit 9a22c37862
6 changed files with 124 additions and 18 deletions

View File

@@ -46,6 +46,19 @@ class InternalRecipeForm(forms.ModelForm):
widgets = {'keywords': MultiSelectWidget}
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('text', )
labels = {
'text': _('Add your comment: '),
}
widgets = {
'text': forms.Textarea(attrs={'rows': 2, 'cols': 15}),
}
class KeywordForm(forms.ModelForm):
class Meta:
model = Keyword

View File

@@ -0,0 +1,27 @@
# Generated by Django 2.2.7 on 2019-11-14 08:49
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('cookbook', '0005_recipeingredients_amount'),
]
operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('recipe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cookbook.Recipe')),
],
),
]

View File

@@ -1,3 +1,4 @@
from django.contrib.auth.models import User
from django.db import models
@@ -77,6 +78,14 @@ class RecipeIngredients(models.Model):
ingredient = models.ForeignKey(Ingredients, models.PROTECT)
class Comment(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
text = models.TextField()
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class RecipeImport(models.Model):
name = models.CharField(max_length=128)
storage = models.ForeignKey(Storage, on_delete=models.PROTECT)

View File

@@ -29,4 +29,27 @@
{{ recipe.instructions | markdown | safe }}
<br/>
<br/>
<h5>{% trans 'Comments' %}</h5>
<form method="POST" class="post-form">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Submit" class="btn btn-success">
</form>
{% for c in comments %}
<div class="card">
<div class="card-body">
<small class="card-title">{{ c.updated_at }} {% trans 'by' %} {{ c.created_by.username }}</small> <br/>
{{ c.text }}
</div>
<br/>
</div>
{% endfor %}
{% endblock %}

View File

@@ -1,6 +1,10 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django_tables2 import RequestConfig
from django.utils.translation import gettext as _
from cookbook.filters import RecipeFilter
from cookbook.forms import *
@@ -23,8 +27,26 @@ def index(request):
def recipe_view(request, pk):
recipe = get_object_or_404(Recipe, pk=pk)
ingredients = RecipeIngredients.objects.filter(recipe=recipe)
comments = Comment.objects.filter(recipe=recipe)
return render(request, 'recipe_view.html', {'recipe': recipe, 'ingredients': ingredients})
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = Comment()
comment.recipe = recipe
comment.text = form.cleaned_data['text']
comment.created_by = request.user
comment.save()
messages.add_message(request, messages.SUCCESS, _('Comment saved!'))
return HttpResponseRedirect(reverse('view_recipe', args=[pk]))
else:
messages.add_message(request, messages.ERROR, _('There was an error saving this comment!'))
else:
form = CommentForm()
return render(request, 'recipe_view.html', {'recipe': recipe, 'ingredients': ingredients, 'comments': comments, 'form': form})
def test(request):