Skip to content

Commit

Permalink
Redirect codes to links page
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jun 16, 2015
1 parent 9d198ee commit 5dbca5f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions memegen/routes/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@


@blueprint.route("<key>/<top>/<bottom>.<kind>")
def get_visible(key, top, bottom, kind):
def get(key, top, bottom, kind):
template = domain.Template(key)
text = domain.Text(top, bottom)
_path = app.image_service.create_image(template, text, kind)
return send_file(_path, mimetype='image/jpeg')


@blueprint.route("<code>.<kind>")
def get_hidden(code, kind):
def get_encoded(code, kind):
key, top, bottom = app.link_service.decode(code)
# TODO: maybe this shouldn't redirect
# url = url_for('.get_visible', key=key, top=top, bottom=bottom, kind=kind)
Expand Down
13 changes: 10 additions & 3 deletions memegen/routes/link.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import OrderedDict

from flask import Blueprint, current_app as app, url_for
from flask import Blueprint, current_app as app, url_for, redirect

from ..domain import Image

Expand All @@ -14,9 +14,16 @@ def get(**kwargs):
data['visible'] = OrderedDict()
data['hidden'] = OrderedDict()
for kind in Image.KINDS:
url = url_for('image.get_visible', kind=kind, _external=True, **kwargs)
url = url_for('image.get', kind=kind, _external=True, **kwargs)
data['visible'][kind] = url
code = app.link_service.encode(**kwargs)
url = url_for('image.get_hidden', kind=kind, _external=True, code=code)
url = url_for('image.get_encoded', kind=kind, _external=True, code=code)
data['hidden'][kind] = url
return data


@blueprint.route("<code>")
def get_encoded(code):
key, top, bottom = app.link_service.decode(code)
url = url_for('.get', key=key, top=top, bottom=bottom)
return redirect(url)
4 changes: 2 additions & 2 deletions memegen/services/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, template_store, **kwargs):
def encode(self, key, top, bottom):
slug = '\t'.join((key, top, bottom))
while len(slug) % 3:
slug += ' '
slug += '\t'
code = base64.urlsafe_b64encode(slug.encode('utf-8'))
return code

Expand All @@ -22,5 +22,5 @@ def decode(self, code):
except ValueError:
raise self.exceptions.bad_code
else:
key, top, bottom = slug.strip().split('\t')
key, top, bottom = slug.strip('\t').split('\t')
return key, top, bottom
2 changes: 1 addition & 1 deletion scent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def python_tests(*_):

for count, (command, title) in enumerate((
(('make', 'test-unit'), "Unit Tests"),
(('make', 'test-all'), "Combined Tests"),
(('make', 'test-all'), "Integration Tests"),
(('make', 'check'), "Static Analysis"),
(('make', 'doc'), None),
), start=1):
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from memegen.settings import get_config


def load(response):
def load(response, as_json=True):
"""Convert a response's binary data (JSON) to a dictionary."""
text = response.data.decode('utf-8')
if not as_json:
return text
if text:
data = json.loads(text)
else:
Expand Down
13 changes: 9 additions & 4 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_get_visible_jpg(self, client):
# assert response.mimetype == 'image/gif'

def test_get_hidden_jpg(self, client):
response = client.get("/aXcJaGVsbG8Jd29ybGQg.jpg")
response = client.get("/aXcJaGVsbG8Jd29ybGQJ.jpg")
assert response.status_code == 200
assert response.mimetype == 'image/jpeg'

Expand All @@ -36,11 +36,16 @@ class TestLink:
def test_get_links(self, client):
response = client.get("/iw/hello/world")
assert response.status_code == 200
assert load(response) == dict(
assert dict(
visible=dict(
jpg="http://localhost/iw/hello/world.jpg",
),
hidden=dict(
jpg="http://localhost/aXcJaGVsbG8Jd29ybGQg.jpg",
jpg="http://localhost/aXcJaGVsbG8Jd29ybGQJ.jpg",
),
)
) == load(response)

def test_get_links_redirect_hidden(self, client):
response = client.get("/aXcJaGVsbG8Jd29ybGQJ")
assert response.status_code == 302
assert '<a href="/iw/hello/world">' in load(response, as_json=False)

0 comments on commit 5dbca5f

Please sign in to comment.