crop,resized, and save picture at the right place

This commit is contained in:
Pierre-antoine Comby 2020-03-06 20:07:15 +01:00
parent 08b97e722b
commit 38feb693f3
3 changed files with 21 additions and 8 deletions

2
.gitignore vendored
View File

@ -37,7 +37,7 @@ coverage
# Local data # Local data
secrets.py secrets.py
*.log *.log
media/
# Virtualenv # Virtualenv
env/ env/
venv/ venv/

View File

@ -12,11 +12,12 @@ from django.urls import reverse_lazy
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.db.models import Q from django.db.models import Q
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.conf import settings
from django_tables2.views import SingleTableView from django_tables2.views import SingleTableView
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from dal import autocomplete from dal import autocomplete
from PIL import Image from PIL import Image
import io
from note.models import Alias, NoteUser from note.models import Alias, NoteUser
from note.models.transactions import Transaction from note.models.transactions import Transaction
@ -230,19 +231,30 @@ class ProfilePictureUpdateView(LoginRequiredMixin, FormMixin, DetailView):
return self.form_invalid(form) return self.form_invalid(form)
def form_valid(self,form): def form_valid(self,form):
image_file_field = form.cleaned_data['image'] image_field = form.cleaned_data['image']
x = form.cleaned_data['x'] x = form.cleaned_data['x']
y = form.cleaned_data['y'] y = form.cleaned_data['y']
w = form.cleaned_data['width'] w = form.cleaned_data['width']
h = form.cleaned_data['height'] h = form.cleaned_data['height']
with Image.open(image_file_field.name) as image: # image crop and resize
image = image.crop((x, y, w+x, h+y)) image_file = io.BytesIO(image_field.read())
image.thumbnail((256, 256), Image.ANTIALIAS) ext = image_field.name.split('.')[-1]
image.save(image_file_field.name,format=image.format) image = Image.open(image_file)
self.object.note.display_image = image_file_field image = image.crop((x, y, x+w, y+h))
image_clean = image.resize((settings.PIC_WIDTH,
settings.PIC_RATIO*settings.PIC_WIDTH),
Image.ANTIALIAS)
image_file = io.BytesIO()
image_clean.save(image_file,ext)
image_field.file = image_file
# renaming
filename = "{}_pic.{}".format(self.object.note.pk, ext)
image_field.name = filename
self.object.note.display_image = image_field
self.object.note.save() self.object.note.save()
return super().form_valid(form) return super().form_valid(form)
class ManageAuthTokens(LoginRequiredMixin, TemplateView): class ManageAuthTokens(LoginRequiredMixin, TemplateView):
""" """
Affiche le jeton d'authentification, et permet de le regénérer Affiche le jeton d'authentification, et permet de le regénérer

View File

@ -45,6 +45,7 @@ class Note(PolymorphicModel):
max_length=255, max_length=255,
blank=False, blank=False,
null=False, null=False,
upload_to='pic/',
default='pic/default.png' default='pic/default.png'
) )
created_at = models.DateTimeField( created_at = models.DateTimeField(