mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
added recipe images
This commit is contained in:
@@ -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'),
|
||||
|
||||
18
cookbook/migrations/0006_recipe_image.py
Normal file
18
cookbook/migrations/0006_recipe_image.py
Normal file
@@ -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/'),
|
||||
),
|
||||
]
|
||||
@@ -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()])
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
<h3>{% trans 'Edit Recipe' %}</h3>
|
||||
|
||||
<form action="." method="post">
|
||||
<form action="." method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
|
||||
{% for field in form %}
|
||||
|
||||
@@ -45,8 +45,9 @@
|
||||
<br/>
|
||||
{% endif %}
|
||||
|
||||
{% if ingredients %}
|
||||
<div class="row">
|
||||
|
||||
<div class="row">
|
||||
{% if ingredients %}
|
||||
<div class="col col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
@@ -89,7 +90,15 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endif %}
|
||||
{% if recipe.image %}
|
||||
<div class="col col-md-6">
|
||||
<img class="img img-fluid rounded" src="{{ recipe.image.url }}">
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if recipe.ingredients or recipe.image %}
|
||||
<br/>
|
||||
<br/>
|
||||
{% endif %}
|
||||
|
||||
@@ -40,13 +40,18 @@ def internal_recipe_update(request, pk):
|
||||
recipe_instance = get_object_or_404(Recipe, pk=pk)
|
||||
|
||||
if request.method == "POST":
|
||||
form = InternalRecipeForm(request.POST)
|
||||
form = InternalRecipeForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
recipe = recipe_instance
|
||||
recipe.name = form.cleaned_data['name']
|
||||
recipe.instructions = form.cleaned_data['instructions']
|
||||
recipe.time = form.cleaned_data['time']
|
||||
|
||||
if form.cleaned_data['image']:
|
||||
recipe.image = form.cleaned_data['image']
|
||||
else:
|
||||
recipe.image = None
|
||||
|
||||
recipe.save()
|
||||
|
||||
form_ingredients = json.loads(form.data['ingredients'])
|
||||
|
||||
Reference in New Issue
Block a user