added recipe images

This commit is contained in:
vabene1111 2019-12-25 16:46:16 +01:00
parent e2301c0c3a
commit 3cb01d6332
10 changed files with 65 additions and 8 deletions

2
.gitignore vendored
View File

@ -61,6 +61,8 @@ target/
venv/
mediafiles/
*.sqlite3
\.idea/workspace\.xml

View File

@ -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'),

View 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/'),
),
]

View File

@ -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()])

View File

@ -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 %}

View File

@ -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 %}

View File

@ -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'])

View File

@ -52,6 +52,7 @@ INSTALLED_APPS = [
'crispy_forms',
'emoji_picker',
'rest_framework',
'django_cleanup.apps.CleanupConfig',
'cookbook.apps.CookbookConfig',
]
@ -79,6 +80,7 @@ TEMPLATES = [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},
@ -139,5 +141,7 @@ LANGUAGES = [
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")

View File

@ -13,7 +13,8 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.http import HttpResponseRedirect
from django.conf import settings
from django.conf.urls.static import static
from django.urls import include, path
from django.contrib import admin
@ -22,3 +23,6 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@ -1,10 +1,12 @@
django
Pillow
django-tables2
django-filter
django-crispy-forms
djangorestframework
django-autocomplete-light
django-emoji-picker
django-cleanup
six
requests
markdown