recipe books

This commit is contained in:
vabene1111 2019-12-25 12:53:09 +01:00
parent 8fcafcc25a
commit d90b012601
5 changed files with 85 additions and 17 deletions

View File

@ -45,6 +45,8 @@ class InternalRecipeForm(forms.ModelForm):
class CommentForm(forms.ModelForm):
prefix = 'comment'
class Meta:
model = Comment
fields = ('text',)
@ -82,6 +84,14 @@ class RecipeBookForm(forms.ModelForm):
fields = ('name',)
class RecipeBookEntryForm(forms.ModelForm):
prefix = 'bookmark'
class Meta:
model = RecipeBookEntry
fields = ('book',)
class SyncForm(forms.ModelForm):
class Meta:
model = Sync

View File

@ -102,7 +102,13 @@ class RecipeBook(models.Model):
name = models.CharField(max_length=128)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
class RecipeBookEntry(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
book = models.ForeignKey(RecipeBook, on_delete=models.CASCADE)
def __str__(self):
return self.recipe.name

View File

@ -29,13 +29,22 @@
<div class="row">
<div class="col col-md-12">
<div class="collapse" id="collapse_{{ b.book.pk }}">
Test
{% if b.recipes %}
<ul>
{% for r in b.recipes %}
{{ r }}
<li><a href="#" onClick='openRecipe({{r.recipe.pk}})'>{{ r.recipe.name }}</a></li>
{% endfor %}
</ul>
{% else %}
{% trans 'There are no recipes in this book yet.' %}
{% endif %}
</div>
</div>
</div>
<br/>
{% endfor %}
{% include 'include/recipe_open_modal.html' %}
{% endblock %}

View File

@ -12,8 +12,16 @@
{% endblock %}
{% block content %}
<div class="row">
<div class="col col-md-9">
<h3>{{ recipe.name }} <a href="{% url 'edit_recipe' recipe.pk %}"><i class="fas fa-pencil-alt"></i></a></h3>
</div>
<div class="col col-md-3" style="text-align: right">
<button class="btn btn-success" onclick="$('#bookmarkModal').modal({'show':true})"><i
class="fas fa-bookmark"></i></button>
</div>
</div>
{% if recipe.storage %}
<small>{% trans 'in' %} <a
href="{% url 'edit_storage' recipe.storage.pk %}">{{ recipe.storage.name }}</a></small><br/>
@ -125,7 +133,7 @@
<form method="POST" class="post-form">
{% csrf_token %}
<div class="input-group mb-3">
<textarea name="text" cols="15" rows="2" class="textarea form-control" required id="id_text"></textarea>
<textarea name="comment-text" cols="15" rows="2" class="textarea form-control" required id="comment-id_text"></textarea>
<div class="input-group-append">
<input type="submit" value="{% trans 'Comment' %}" class="btn btn-success">
</div>
@ -147,10 +155,36 @@
{% include 'include/recipe_open_modal.html' %}
{% endif %}
<!-- Bookmark Modal -->
<div class="modal fade" id="bookmarkModal" tabindex="-1" role="dialog" aria-labelledby="bookmarkModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="bookmarkModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form method="POST" class="post-form">
{% csrf_token %}
{{ bookmark_form|crispy }}
<input type="submit" value="{% trans 'Save' %}" class="btn btn-success">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function reloadIngredients() {
factor = Number($('#in_factor').val())
factor = Number($('#in_factor').val());
ingredients = {
{% for i in ingredients %}
{{ i.pk }}: {{ i.amount|unlocalize }},

View File

@ -30,24 +30,33 @@ def recipe_view(request, pk):
comments = Comment.objects.filter(recipe=recipe)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment_form = CommentForm(request.POST, prefix='comment')
if comment_form.is_valid():
comment = Comment()
comment.recipe = recipe
comment.text = form.cleaned_data['text']
comment.text = comment_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()
bookmark_form = RecipeBookEntryForm(request.POST, prefix='bookmark')
if bookmark_form.is_valid():
bookmark = RecipeBookEntry()
bookmark.recipe = recipe
bookmark.book = bookmark_form.cleaned_data['book']
bookmark.save()
messages.add_message(request, messages.SUCCESS, _('Bookmark saved!'))
comment_form = CommentForm()
bookmark_form = RecipeBookEntryForm()
return render(request, 'recipe_view.html',
{'recipe': recipe, 'ingredients': ingredients, 'comments': comments, 'form': form})
{'recipe': recipe, 'ingredients': ingredients, 'comments': comments, 'comment_form': comment_form,
'bookmark_form': bookmark_form})
@login_required()