This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tests and responses for image states
- Loading branch information
Chris Sinchok
committed
Oct 13, 2014
1 parent
7338086
commit 8561a92
Showing
5 changed files
with
140 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import io | ||
import random | ||
|
||
from django.http import HttpResponse | ||
|
||
from PIL import Image, ImageDraw, ImageFont | ||
|
||
from betty.cropper.models import Ratio | ||
from betty.conf.app import settings | ||
|
||
|
||
EXTENSION_MAP = { | ||
"jpg": { | ||
"format": "jpeg", | ||
"mime_type": "image/jpeg" | ||
}, | ||
"png": { | ||
"format": "png", | ||
"mime_type": "image/png" | ||
}, | ||
} | ||
|
||
|
||
class PlaceholderResponse(HttpResponse): | ||
|
||
def __init__(self, ratio, width, extension, *args, **kwargs): | ||
super(PlaceholderResponse, self).__init__(*args, **kwargs) | ||
self.status_code = 200 | ||
|
||
if ratio.string == "original": | ||
ratio = Ratio(random.choice((settings.BETTY_RATIOS))) | ||
height = int(round((width * ratio.height / float(ratio.width)))) | ||
|
||
bg_fill = random.choice(settings.BETTY_PLACEHOLDER_COLORS) | ||
img = Image.new("RGB", (width, height), bg_fill) | ||
|
||
draw = ImageDraw.Draw(img) | ||
|
||
font = ImageFont.truetype(filename=settings.BETTY_PLACEHOLDER_FONT, size=45) | ||
text_size = draw.textsize(ratio.string, font=font) | ||
text_coords = ( | ||
int(round((width - text_size[0]) / 2.0)), | ||
int(round((height - text_size[1]) / 2) - 15), | ||
) | ||
draw.text(text_coords, ratio.string, font=font, fill=(256, 256, 256)) | ||
|
||
if extension == "jpg": | ||
self._container = [img.tobytes("jpeg", "RGB")] | ||
if extension == "png": | ||
# I apparently can't get an encoder for this | ||
tmp = io.BytesIO() | ||
img.save(tmp, format="png") | ||
self._container = [tmp.getvalue()] | ||
|
||
self["Cache-Control"] = "no-cache, no-store, must-revalidate" | ||
self["Pragma"] = "no-cache" | ||
self["Expires"] = "0" | ||
self["Content-Type"] = EXTENSION_MAP[extension]["mime_type"] | ||
|
||
|
||
class PendingResponse(HttpResponse): | ||
def __init__(self, ratio, width, extension, *args, **kwargs): | ||
super(PendingResponse, self).__init__(*args, **kwargs) | ||
self.status_code = 202 | ||
|
||
if ratio.string == "original": | ||
ratio = Ratio(settings.BETTY_RATIOS[0]) | ||
height = int(round((width * ratio.height / float(ratio.width)))) | ||
|
||
bg_fill = (211, 211, 211) | ||
img = Image.new("RGB", (width, height), bg_fill) | ||
|
||
if extension == "jpg": | ||
self._container = [img.tobytes("jpeg", "RGB")] | ||
if extension == "png": | ||
# I apparently can't get an encoder for this | ||
tmp = io.BytesIO() | ||
img.save(tmp, format="png") | ||
self._container = [tmp.getvalue()] | ||
|
||
self["Cache-Control"] = "no-cache, no-store, must-revalidate" | ||
self["Pragma"] = "no-cache" | ||
self["Expires"] = "0" | ||
self["Content-Type"] = EXTENSION_MAP[extension]["mime_type"] | ||
|
||
|
||
class FailureResponse(HttpResponse): | ||
def __init__(self, ratio, width, extension, *args, **kwargs): | ||
super(FailureResponse, self).__init__(*args, **kwargs) | ||
self.status_code = 410 | ||
|
||
if ratio.string == "original": | ||
ratio = Ratio(settings.BETTY_RATIOS[0]) | ||
height = int(round((width * ratio.height / float(ratio.width)))) | ||
|
||
bg_fill = (166, 18, 18) | ||
img = Image.new("RGB", (width, height), bg_fill) | ||
|
||
if extension == "jpg": | ||
self._container = [img.tobytes("jpeg", "RGB")] | ||
if extension == "png": | ||
# I apparently can't get an encoder for this | ||
tmp = io.BytesIO() | ||
img.save(tmp, format="png") | ||
self._container = [tmp.getvalue()] | ||
|
||
self["Content-Type"] = EXTENSION_MAP[extension]["mime_type"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters