Skip to content

Commit

Permalink
Add template list, point to from root
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Jun 17, 2015
1 parent 5dbca5f commit 6299ad2
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ ifndef TRAVIS
endif

# Test settings
UNIT_TEST_COVERAGE := 53
UNIT_TEST_COVERAGE := 64
INTEGRATION_TEST_COVERAGE := 75
COMBINED_TEST_COVERAGE := 90
COMBINED_TEST_COVERAGE := 96

# System paths
PLATFORM := $(shell python -c 'import sys; print(sys.platform)')
Expand Down
4 changes: 3 additions & 1 deletion memegen/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ def register_services(app):


def register_blueprints(app):
app.register_blueprint(routes.link.blueprint)
app.register_blueprint(routes.root.blueprint)
app.register_blueprint(routes.templates.blueprint)
app.register_blueprint(routes.links.blueprint)
app.register_blueprint(routes.image.blueprint)
2 changes: 1 addition & 1 deletion memegen/domain/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Image:
"""Meme image generated from a template."""

# TODO: support more image types
KINDS = ('jpg',) # 'png', 'gif')
KINDS = ('JPG',) # 'PNG', 'GIF')

@classmethod
def from_template(cls, template, text, kind):
Expand Down
5 changes: 4 additions & 1 deletion memegen/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from . import link, image
from . import root
from . import templates
from . import links
from . import image
7 changes: 4 additions & 3 deletions memegen/routes/link.py → memegen/routes/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
from ..domain import Image


blueprint = Blueprint('link', __name__, url_prefix="/")
blueprint = Blueprint('links', __name__, url_prefix="/")


@blueprint.route("<key>/<top>/<bottom>")
def get(**kwargs):
"""Get links for generated images."""
data = OrderedDict()
data['visible'] = OrderedDict()
data['hidden'] = OrderedDict()
for kind in Image.KINDS:
url = url_for('image.get', kind=kind, _external=True, **kwargs)
url = url_for('image.get', kind=kind.lower(), _external=True, **kwargs)
data['visible'][kind] = url
code = app.link_service.encode(**kwargs)
url = url_for('image.get_encoded', kind=kind, _external=True, code=code)
url = url_for('image.get_encoded', kind=kind.lower(), _external=True, code=code)
data['hidden'][kind] = url
return data

Expand Down
16 changes: 16 additions & 0 deletions memegen/routes/root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from collections import OrderedDict

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

from ..domain import Image


blueprint = Blueprint('root', __name__, url_prefix="/")


@blueprint.route("")
def get(**kwargs):
"""Generate memes from templates."""
data = OrderedDict()
data['templates'] = url_for("templates.get", _external=True)
return data
28 changes: 28 additions & 0 deletions memegen/routes/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from collections import OrderedDict

from flask import Blueprint, current_app as app, request, url_for
from flask_api import exceptions


blueprint = Blueprint('templates', __name__, url_prefix="/templates/")


@blueprint.route("")
def get():
"""Get a list of all meme templates."""
data = OrderedDict()
data['Insanity Wolf'] = url_for(".create", key='iw', _external=True)
return data


@blueprint.route("<key>", methods=['GET', 'POST'])
def create(key):
"""Generate a meme from a templates."""
if request.method == 'GET':
return dict(sample=url_for("links.get", key=key, _external=True,
top="hello", bottom="world"))
elif request.method == 'POST':
# TODO: https://github.com/jacebrowning/memegen/issues/12
raise exceptions.PermissionDenied("Feature not implemented.")
else: # pragma: no cover
assert None
67 changes: 47 additions & 20 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,53 @@
from .conftest import load


class TestRoot:

def test_get_root(self, client):
response = client.get("/")
assert response.status_code == 200
assert dict(
templates="http://localhost/templates/",
) == load(response)


class TestTemplates:

def test_get_templates(self, client):
response = client.get("/templates/")
assert response.status_code == 200
data = load(response)
assert len(data) > 0 # TODO: check for a minimum number of memes
assert "http://localhost/templates/iw" == data['Insanity Wolf']

def test_get_template(self, client):
response = client.get("/templates/iw")
assert response.status_code == 200
assert dict(
sample="http://localhost/iw/hello/world",
) == load(response)


class TestLink:

def test_get_links(self, client):
response = client.get("/iw/hello/world")
assert response.status_code == 200
assert dict(
visible=dict(
JPG="http://localhost/iw/hello/world.jpg",
),
hidden=dict(
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)


class TestMeme:

def test_get_visible_jpg(self, client):
Expand Down Expand Up @@ -29,23 +76,3 @@ def test_get_hidden_jpg(self, client):
response = client.get("/aXcJaGVsbG8Jd29ybGQJ.jpg")
assert response.status_code == 200
assert response.mimetype == 'image/jpeg'


class TestLink:

def test_get_links(self, client):
response = client.get("/iw/hello/world")
assert response.status_code == 200
assert dict(
visible=dict(
jpg="http://localhost/iw/hello/world.jpg",
),
hidden=dict(
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 6299ad2

Please sign in to comment.