From 349b9629f85da4b78ae25e837db5073a90c1c3a6 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 2 May 2020 14:15:56 +0200 Subject: [PATCH] added sharing to recipe books --- cookbook/forms.py | 5 ++--- cookbook/migrations/0039_recipebook_shared.py | 20 +++++++++++++++++++ cookbook/models.py | 1 + cookbook/views/views.py | 3 ++- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 cookbook/migrations/0039_recipebook_shared.py diff --git a/cookbook/forms.py b/cookbook/forms.py index ecccd9d82..52eb993ed 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -237,9 +237,8 @@ class ImportRecipeForm(forms.ModelForm): class RecipeBookForm(forms.ModelForm): class Meta: model = RecipeBook - fields = ('name', 'icon', 'description') - - widgets = {'icon': EmojiPickerTextInput} + fields = ('name', 'icon', 'description', 'shared') + widgets = {'icon': EmojiPickerTextInput, 'shared': MultiSelectWidget} class MealPlanForm(forms.ModelForm): diff --git a/cookbook/migrations/0039_recipebook_shared.py b/cookbook/migrations/0039_recipebook_shared.py new file mode 100644 index 000000000..ea3144942 --- /dev/null +++ b/cookbook/migrations/0039_recipebook_shared.py @@ -0,0 +1,20 @@ +# Generated by Django 3.0.5 on 2020-05-02 12:04 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('cookbook', '0038_auto_20200502_1259'), + ] + + operations = [ + migrations.AddField( + model_name='recipebook', + name='shared', + field=models.ManyToManyField(blank=True, related_name='shared_with', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 8e3240aa6..1b48f46e5 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -197,6 +197,7 @@ class RecipeBook(models.Model): name = models.CharField(max_length=128) description = models.TextField(blank=True) icon = models.CharField(max_length=16, blank=True, null=True) + shared = models.ManyToManyField(User, blank=True, related_name='shared_with') created_by = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): diff --git a/cookbook/views/views.py b/cookbook/views/views.py index 1c7fb2b8c..36d01251a 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -4,6 +4,7 @@ from datetime import datetime, timedelta from django.contrib import messages from django.contrib.auth import update_session_auth_hash from django.contrib.auth.forms import PasswordChangeForm +from django.db.models import Q from django.http import HttpResponseRedirect from django.shortcuts import render, get_object_or_404 from django.urls import reverse_lazy @@ -86,7 +87,7 @@ def recipe_view(request, pk): def books(request): book_list = [] - books = RecipeBook.objects.filter(created_by=request.user).all() + books = RecipeBook.objects.filter(Q(created_by=request.user) | Q(shared=request.user)).all() for b in books: book_list.append({'book': b, 'recipes': RecipeBookEntry.objects.filter(book=b).all()})