diff --git a/cookbook/forms.py b/cookbook/forms.py index ecccd9d8..52eb993e 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 00000000..ea314494 --- /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 8e3240aa..1b48f46e 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 1c7fb2b8..36d01251 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()})