diff --git a/.gitignore b/.gitignore index 91b02003..a6267286 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,8 @@ target/ venv/ +mediafiles/ + *.sqlite3 \.idea/workspace\.xml diff --git a/cookbook/forms.py b/cookbook/forms.py index 88e26abb..e7d0e4e4 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -33,7 +33,7 @@ class ExternalRecipeForm(forms.ModelForm): class InternalRecipeForm(forms.ModelForm): class Meta: model = Recipe - fields = ('name', 'instructions', 'time', 'keywords') + fields = ('name', 'instructions', 'image', 'time', 'keywords') labels = { 'name': _('Name'), diff --git a/cookbook/migrations/0006_recipe_image.py b/cookbook/migrations/0006_recipe_image.py new file mode 100644 index 00000000..10b41e09 --- /dev/null +++ b/cookbook/migrations/0006_recipe_image.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.1 on 2019-12-25 15:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0005_recipebook_recipebookentry'), + ] + + operations = [ + migrations.AddField( + model_name='recipe', + name='image', + field=models.ImageField(blank=True, null=True, upload_to='recipes/'), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 0e549f43..0504d9dc 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -1,4 +1,8 @@ +from io import BytesIO + +from PIL import Image from django.contrib.auth.models import User +from django.core.files import File from django.db import models @@ -53,6 +57,7 @@ class Keyword(models.Model): class Recipe(models.Model): name = models.CharField(max_length=128) instructions = models.TextField(blank=True) + image = models.ImageField(upload_to='recipes/', blank=True, null=True) storage = models.ForeignKey(Storage, on_delete=models.PROTECT, blank=True, null=True) file_uid = models.CharField(max_length=256, default="") file_path = models.CharField(max_length=512, default="") @@ -67,6 +72,14 @@ class Recipe(models.Model): def __str__(self): return self.name + def save(self, *args, **kwargs): + if self.image: + im = Image.open(self.image) + im_io = BytesIO() + im.save(im_io, 'JPEG', quality=70) + self.image = File(im_io, name=(str(self.pk)+'.jpeg')) + super().save(*args, **kwargs) + @property def all_tags(self): return ' '.join([(x.icon + x.name) for x in self.keywords.all()]) diff --git a/cookbook/templates/forms/edit_internal_recipe.html b/cookbook/templates/forms/edit_internal_recipe.html index 65f4d000..7019fb25 100644 --- a/cookbook/templates/forms/edit_internal_recipe.html +++ b/cookbook/templates/forms/edit_internal_recipe.html @@ -15,7 +15,7 @@