This commit is contained in:
michigg 2017-12-14 15:19:42 +01:00
parent e6fc1ed465
commit eb5337898e
6 changed files with 113 additions and 7 deletions

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-12-07 11:45
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('donar', '0009_auto_20171204_1648'),
]
operations = [
migrations.AlterField(
model_name='lecture_terms',
name='starttime',
field=models.TimeField(default=datetime.datetime(2017, 12, 7, 11, 45, 42, 534016, tzinfo=utc)),
),
]

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-12-07 11:47
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('donar', '0010_auto_20171207_1245'),
]
operations = [
migrations.AlterField(
model_name='lecture_terms',
name='starttime',
field=models.TimeField(default=datetime.datetime(2017, 12, 7, 11, 47, 6, 785241, tzinfo=utc)),
),
]

View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-12-07 11:45
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('food', '0005_auto_20171204_1634'),
]
operations = [
migrations.AddField(
model_name='userfoodimage',
name='thumbnail',
field=models.ImageField(blank=True, upload_to='food/thumbs/%Y/%m/%W'),
),
migrations.AlterField(
model_name='menu',
name='menu',
field=models.ManyToManyField(related_name='foods', to='food.SingleFood'),
),
migrations.AlterField(
model_name='userfoodimage',
name='image',
field=models.ImageField(blank=True, upload_to='food/originals/%Y/%m/%W'),
),
]

View File

@ -1,8 +1,15 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from django.utils import timezone
from django.db import models import os
from io import BytesIO
from PIL import Image
from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import models
from django.utils import timezone
MAX_LENGTH = 60 MAX_LENGTH = 60
@ -72,10 +79,37 @@ class UserFoodImage(models.Model):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, unique=False) user = models.ForeignKey(User, on_delete=models.CASCADE, unique=False)
food = models.ForeignKey(SingleFood) food = models.ForeignKey(SingleFood)
image = models.ImageField(upload_to='food/%Y/%m/%W', blank=True) image = models.ImageField(upload_to='food/originals/%Y/%m/%W', blank=True)
thumbnail = models.ImageField(upload_to='food/thumbs/%Y/%m/%W', blank=True)
class Meta: class Meta:
unique_together = ('user', 'food') unique_together = ('user', 'food')
def __str__(self): def __str__(self):
return "User: %s - Rating: %s" % (self.user.username, str(self.image)) return "User: %s - Image: %s" % (self.user.username, str(self.image))
def save(self, force_update=False, force_insert=False, thumb_size=(640, 480)):
image = Image.open(self.image)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image.thumbnail(thumb_size, Image.ANTIALIAS)
# save the thumbnail to memory
temp_handle = BytesIO()
image.save(temp_handle, 'jpeg')
temp_handle.seek(0) # rewind the file
# save to the thumbnail field
suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
temp_handle.read(),
content_type='image/jpg')
self.thumbnail.save('%s_thumbnail.%s' % (self.food.name, 'jpg'), suf, save=False)
# save the image object
self.image.name = "%s" % self.food.name
super(UserFoodImage, 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.thumbnail.name))
super(UserFoodImage, self).delete()

View File

@ -4,7 +4,6 @@ from __future__ import unicode_literals
import datetime import datetime
import os import os
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render from django.shortcuts import render
@ -138,7 +137,7 @@ def pic_upload(request, id):
try: try:
old_user_pic = UserFoodImage.objects.get(user=request.user, food=id) old_user_pic = UserFoodImage.objects.get(user=request.user, food=id)
old_user_pic.delete() old_user_pic.delete()
os.remove(os.path.join(settings.MEDIA_ROOT, old_user_pic.image.name))
except ObjectDoesNotExist: except ObjectDoesNotExist:
pass pass
userPic = form.save(commit=False) userPic = form.save(commit=False)

View File

@ -40,7 +40,6 @@
{# ===== Body ===== #} {# ===== Body ===== #}
<body> <body>
{% block body %} {% block body %}
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">