Skip to content

Commit

Permalink
Remove redundant styles
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Nov 30, 2021
1 parent cdd3a0f commit 5480d9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Expand Up @@ -35,6 +35,7 @@
"Keanu",
"Kombucha",
"LANCZOS",
"maga",
"Memegen",
"Minecraft",
"myapikey",
Expand Down
12 changes: 12 additions & 0 deletions app/tests/test_views_memes.py
Expand Up @@ -36,6 +36,18 @@ def it_returns_an_image_url(expect, client, as_json):
"url": "http://localhost:5000/images/iw/foo/bar.png"
}

def it_removes_redundant_styles(expect, client):
data = {
"template_id": "iw",
"text_lines[]": ["foo", "bar"],
"style[]": [" ", "test", "default"],
}
request, response = client.post("/images", data=data)
expect(response.status) == 201
expect(response.json) == {
"url": "http://localhost:5000/images/iw/foo/bar.png?style=default,test"
}

def it_returns_gif_when_animated(expect, client):
data = {
"template_id": "iw",
Expand Down
12 changes: 10 additions & 2 deletions app/views/templates.py
Expand Up @@ -116,23 +116,31 @@ async def generate_url(
if request.form:
payload = dict(request.form)
for key in list(payload.keys()):
if "lines" not in key:
if "lines" not in key and "style" not in key:
payload[key] = payload.pop(key)[0]
else:
payload = request.json or {}
with suppress(KeyError):
payload["text_lines"] = payload.pop("text_lines[]")
with suppress(KeyError):
payload["style"] = payload.pop("style[]")

if template_id_required:
try:
template_id = payload["template_id"]
except KeyError:
return response.json({"error": '"template_id" is required'}, status=400)

from sanic.log import logger

logger.critical(payload)

text_lines = utils.urls.arg(payload, [], "text_lines")
style: str = utils.urls.arg(payload, "", "style", "overlay", "alt")
if isinstance(style, list):
style = ",".join(style)
style = ",".join([(s.strip() or "default") for s in style])
while style.endswith(",default"):
style = style.removesuffix(",default")
background = utils.urls.arg(payload, "", "background", "image_url")
extension = utils.urls.arg(payload, "", "extension")

Expand Down

0 comments on commit 5480d9c

Please sign in to comment.