Fix image hack
This commit is contained in:
parent
fed22f53a1
commit
b87f6ac56b
@ -2,15 +2,11 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import datetime
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpResponseForbidden
|
||||
from django.shortcuts import render
|
||||
from pprint import pprint
|
||||
|
||||
from apps.food.models import Menu, SingleFood, UserFoodImage
|
||||
from apps.food.models import SingleFood, UserFoodImage
|
||||
|
||||
|
||||
def food_picture_management(request):
|
||||
@ -31,7 +27,7 @@ def food_picture_save(request, id):
|
||||
if (request.user.is_superuser):
|
||||
chosen_pic = UserFoodImage.objects.get(id=id)
|
||||
food = SingleFood.objects.get(id=chosen_pic.food.id)
|
||||
food.image.set([chosen_pic, ])
|
||||
food.image = chosen_pic.image
|
||||
food.save()
|
||||
|
||||
return food_picture_management(request)
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
from django import forms
|
||||
from apps.food.models import UserFoodImage
|
||||
from apps.food.models import FoodImage
|
||||
|
||||
|
||||
class UploadImageForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = UserFoodImage
|
||||
model = FoodImage
|
||||
fields = ['image']
|
||||
|
||||
42
ofu_app/apps/food/migrations/0005_auto_20180113_0023.py
Normal file
42
ofu_app/apps/food/migrations/0005_auto_20180113_0023.py
Normal file
@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.7 on 2018-01-12 23:23
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('food', '0004_auto_20180111_1246'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='FoodImage',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('image', models.ImageField(blank=True, upload_to='food/originals/%Y/%m/%W')),
|
||||
('thumb', models.ImageField(blank=True, upload_to='food/thumbs/%Y/%m/%W')),
|
||||
],
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='userfoodimage',
|
||||
name='thumb',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='singlefood',
|
||||
name='image',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='userfoodimage',
|
||||
name='image',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='food.FoodImage'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='singlefood',
|
||||
name='image',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='food.FoodImage'),
|
||||
),
|
||||
]
|
||||
25
ofu_app/apps/food/migrations/0006_auto_20180113_0121.py
Normal file
25
ofu_app/apps/food/migrations/0006_auto_20180113_0121.py
Normal file
@ -0,0 +1,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.7 on 2018-01-13 00:21
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('food', '0005_auto_20180113_0023'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='foodimage',
|
||||
name='image',
|
||||
field=models.ImageField(blank=True, null=True, upload_to='food/originals/%Y/%m/%W'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='foodimage',
|
||||
name='thumb',
|
||||
field=models.ImageField(blank=True, null=True, upload_to='food/thumbs/%Y/%m/%W'),
|
||||
),
|
||||
]
|
||||
@ -10,7 +10,6 @@ from django.contrib.auth.models import User
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django.utils.encoding import smart_str
|
||||
|
||||
MAX_LENGTH = 256
|
||||
|
||||
@ -42,7 +41,7 @@ class SingleFood(models.Model):
|
||||
price_student = models.CharField(max_length=10, blank=True, null=True)
|
||||
price_employee = models.CharField(max_length=10, blank=True, null=True)
|
||||
price_guest = models.CharField(max_length=10, blank=True, null=True)
|
||||
image = models.ManyToManyField("UserFoodImage", blank=True, null=True)
|
||||
image = models.ForeignKey('FoodImage', on_delete=models.PROTECT, blank=True, null=True)
|
||||
rating = models.FloatField(default=0)
|
||||
allergens = models.ManyToManyField("Allergene", blank=True)
|
||||
|
||||
@ -88,8 +87,7 @@ class UserFoodImage(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
user = models.ForeignKey(User, on_delete=models.PROTECT, unique=False)
|
||||
food = models.ForeignKey(SingleFood, on_delete=models.PROTECT)
|
||||
image = models.ImageField(upload_to='food/originals/%Y/%m/%W', blank=True)
|
||||
thumb = models.ImageField(upload_to='food/thumbs/%Y/%m/%W', blank=True)
|
||||
image = models.ForeignKey('FoodImage', on_delete=models.PROTECT)
|
||||
|
||||
class Meta:
|
||||
unique_together = ('user', 'food')
|
||||
@ -97,6 +95,12 @@ class UserFoodImage(models.Model):
|
||||
def __str__(self):
|
||||
return "User: %s - Image: %s" % (self.user.username, str(self.image))
|
||||
|
||||
|
||||
class FoodImage(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
image = models.ImageField(upload_to='food/originals/%Y/%m/%W', blank=True, null=True)
|
||||
thumb = models.ImageField(upload_to='food/thumbs/%Y/%m/%W', blank=True, null=True)
|
||||
|
||||
def save(self, force_update=False, force_insert=False, thumb_size=(640, 480)):
|
||||
image = Image.open(self.image)
|
||||
|
||||
@ -114,13 +118,12 @@ class UserFoodImage(models.Model):
|
||||
temp_handle.read(),
|
||||
content_type='image/jpg')
|
||||
|
||||
str_food = smart_str(self.food.name, encoding='utf-8')
|
||||
self.thumb.save('%s_%s_thumbnail.%s' % (self.food.id, self.user.id, 'jpg'), suf, save=False)
|
||||
self.thumb.save('%s_thumbnail.%s' % (self.id, 'jpg'), suf, save=False)
|
||||
# save the image object
|
||||
self.image.name = "%s_%s_original.%s" % (self.food.id, self.user.id, 'jpg')
|
||||
super(UserFoodImage, self).save(force_update, force_insert)
|
||||
self.image.name = "%s_original.%s" % (self.id, 'jpg')
|
||||
super(FoodImage, self).save(force_update, force_insert)
|
||||
|
||||
def delete(self, using=None, keep_parents=False):
|
||||
os.remove(os.path.join(settings.MEDIA_ROOT, self.image.name))
|
||||
os.remove(os.path.join(settings.MEDIA_ROOT, self.thumb.name))
|
||||
super(UserFoodImage, self).delete()
|
||||
super(FoodImage, self).delete()
|
||||
|
||||
@ -2,15 +2,12 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import datetime
|
||||
import os
|
||||
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from pprint import pprint
|
||||
|
||||
from apps.food.forms import UploadImageForm
|
||||
from apps.food.models import Menu, HappyHour, SingleFood, UserRating, UserFoodImage
|
||||
from apps.food.models import Menu, HappyHour, SingleFood, UserRating, UserFoodImage, FoodImage
|
||||
|
||||
|
||||
# Create your views here.
|
||||
@ -134,17 +131,10 @@ def food_image(request):
|
||||
def pic_upload(request, id):
|
||||
form = UploadImageForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
try:
|
||||
food = SingleFood.objects.get(id=id)
|
||||
food.image.clear
|
||||
old_user_pic = UserFoodImage.objects.get(user=request.user, food=id)
|
||||
old_user_pic.delete()
|
||||
|
||||
except ObjectDoesNotExist:
|
||||
pass
|
||||
userPic = form.save(commit=False)
|
||||
pic = form.save(commit=True)
|
||||
userPic, success = UserFoodImage.objects.get_or_create(user_id=request.user)
|
||||
userPic.image = pic
|
||||
userPic.food = SingleFood.objects.get(id=id)
|
||||
userPic.user = request.user
|
||||
userPic.save()
|
||||
return True
|
||||
else:
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
<div class="col-6">
|
||||
<a href="{{ url('foodpicture-save', kwargs={'id':picture.id}) }}">
|
||||
<h3>{{ picture.food.name }}</h3>
|
||||
<img src="{{ picture.image.url }}" style="width: 50%">
|
||||
<img src="{{ picture.image.image.url }}" style="width: 50%">
|
||||
</a>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
{% for image in images %}
|
||||
<div class="col-12 mt-2">
|
||||
<p>{{ image.food.name }}</p>
|
||||
<img src="{{ image.image.url }}" style="width:50%">
|
||||
<img src="{{ image.image.image.url }}" style="width:50%">
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
@ -60,13 +60,11 @@
|
||||
<li data-food="{{ single_food.id }}" data-rating="{{ single_food.rating }}" class="food-item media mb-2">
|
||||
<div class="mr-2 media-left media-middle">
|
||||
{# TODO: without many to many #}
|
||||
{% if single_food.image.all() %}
|
||||
{% for image in single_food.image.all() %}
|
||||
<a href="{{ image.image.url }}" data-lightbox="{{ title }}"
|
||||
data-title="{{ single_food.name }}">
|
||||
<img src="{{ image.image.url }}" class="media-object" alt="Bild" width="80px">
|
||||
</a>
|
||||
{% endfor %}
|
||||
{% if single_food.image %}
|
||||
<a href="{{ single_food.image.image.url }}" data-lightbox="{{ title }}"
|
||||
data-title="{{ single_food.name }}">
|
||||
<img src="{{ single_food.image.image.url }}" class="media-object" alt="Bild" width="80px">
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="/media/food/default/default.jpg" data-lightbox="{{ title }}"
|
||||
data-title="{{ single_food.name }}">
|
||||
|
||||
Reference in New Issue
Block a user