532.1.28
by kaputtnik
fixed uploading profile images: changed StringIO to BytesIO |
1 |
from io import BytesIO |
429.2.1
by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous |
2 |
from django.db import models |
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
3 |
import logging |
433
by franku
fixed missing import |
4 |
from django.core.files.uploadedfile import SimpleUploadedFile |
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
5 |
|
438.1.6
by franku
run the script |
6 |
|
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
7 |
class ExtendedImageField(models.ImageField): |
438.1.6
by franku
run the script |
8 |
"""Extended ImageField that can resize image before saving it."""
|
9 |
||
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
10 |
def __init__(self, *args, **kwargs): |
11 |
self.width = kwargs.pop('width', None) |
|
12 |
self.height = kwargs.pop('height', None) |
|
13 |
super(ExtendedImageField, self).__init__(*args, **kwargs) |
|
14 |
||
15 |
def save_form_data(self, instance, data): |
|
325.1.14
by Shevonar
Login box with link only, new design for profiles, fixes |
16 |
if data is not None and data != self.default: |
17 |
if not data: |
|
18 |
data = self.default |
|
19 |
if instance.avatar != self.default: |
|
438.1.6
by franku
run the script |
20 |
instance.avatar.delete() |
325.1.14
by Shevonar
Login box with link only, new design for profiles, fixes |
21 |
else: |
22 |
if hasattr(data, 'read') and self.width and self.height: |
|
438.1.6
by franku
run the script |
23 |
content = self.resize_image( |
24 |
data.read(), width=self.width, height=self.height) |
|
25 |
data = SimpleUploadedFile( |
|
26 |
instance.user.username + '.png', content, 'image/png') |
|
62.1.7
by Holger Rapp
Added support for profiles. No gravatar yet |
27 |
super(ExtendedImageField, self).save_form_data(instance, data) |
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
28 |
|
29 |
def resize_image(self, rawdata, width, height): |
|
438.1.6
by franku
run the script |
30 |
"""Resize image to fit it into (width, height) box."""
|
359
by Holger Rapp
import Image -> from PIL import Image. |
31 |
from PIL import Image |
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
32 |
|
532.1.28
by kaputtnik
fixed uploading profile images: changed StringIO to BytesIO |
33 |
image = Image.open(BytesIO(rawdata)) |
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
34 |
try: |
35 |
oldw, oldh = image.size |
|
350
by Holger Rapp
Added support for PIL. Fixed some whitespace. Fixed tests for wlmaps. |
36 |
|
145
by Holger Rapp
Avatars can no longer be any size, but are limited to 80x80 pixels |
37 |
if oldw > width or oldh > height: |
38 |
if oldw >= oldh: |
|
39 |
x = int(round((oldw - oldh) / 2.0)) |
|
40 |
image = image.crop((x, 0, (x + oldh) - 1, oldh - 1)) |
|
41 |
else: |
|
42 |
y = int(round((oldh - oldw) / 2.0)) |
|
43 |
image = image.crop((0, y, oldw - 1, (y + oldw) - 1)) |
|
44 |
image = image.resize((width, height), resample=Image.ANTIALIAS) |
|
532.1.1
by franku
converted to python 3.6 using 2to3 script |
45 |
except Exception as err: |
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
46 |
logging.error(err) |
47 |
return '' |
|
48 |
||
532.1.28
by kaputtnik
fixed uploading profile images: changed StringIO to BytesIO |
49 |
string = BytesIO() |
22
by Holger Rapp
- Added my hacked version of pybb. Remerging new versions is very difficult at this point :( |
50 |
image.save(string, format='PNG') |
51 |
return string.getvalue() |