|
1
|
1 |
|
from collections import OrderedDict |
|
2
|
|
|
|
|
3
|
1 |
|
from flask import Blueprint, current_app, request, redirect |
|
|
|
|
|
|
4
|
1 |
|
from flask_api import exceptions |
|
|
|
|
|
|
5
|
1 |
|
from webargs import fields |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
1 |
|
from ..domain import Text |
|
8
|
|
|
|
|
9
|
1 |
|
from ._parser import parser |
|
10
|
1 |
|
from ._utils import route |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
1 |
|
blueprint = Blueprint('templates', __name__, url_prefix="/api/templates/") |
|
14
|
|
|
|
|
15
|
1 |
|
OPTIONS = { |
|
16
|
|
|
'top': fields.Str(missing=""), |
|
17
|
|
|
'bottom': fields.Str(missing=""), |
|
18
|
|
|
'_redirect': fields.Bool(load_from='redirect', missing=True), |
|
19
|
|
|
'_masked': fields.Bool(load_from='masked', missing=False), |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
|
|
23
|
|
|
@blueprint.route("") |
|
24
|
|
|
def get(): |
|
25
|
1 |
|
"""Get a list of all meme templates.""" |
|
26
|
1 |
|
data = OrderedDict() |
|
27
|
1 |
|
for template in sorted(current_app.template_service.all()): |
|
28
|
1 |
|
url = route('.create', key=template.key, _external=True) |
|
29
|
1 |
|
data[template.name] = url |
|
30
|
|
|
return data |
|
31
|
|
|
|
|
32
|
1 |
|
|
|
33
|
|
|
@blueprint.route("", methods=['POST']) |
|
34
|
1 |
|
def create_template(): |
|
35
|
|
|
raise exceptions.PermissionDenied(current_app.config['CONTRIBUTING_URL']) |
|
36
|
|
|
|
|
37
|
1 |
|
|
|
38
|
1 |
|
@blueprint.route("<key>", methods=['GET', 'POST'], endpoint='create') |
|
39
|
|
|
@parser.use_kwargs(OPTIONS) |
|
40
|
|
|
def create_meme(key, top, bottom, _redirect, _masked): |
|
41
|
1 |
|
"""Generate a meme from a template.""" |
|
42
|
1 |
|
if request.method == 'GET': |
|
43
|
1 |
|
template = current_app.template_service.find(key) |
|
44
|
1 |
|
if template.key != key: |
|
45
|
|
|
return redirect(route('.create', key=template.key)) |
|
46
|
1 |
|
|
|
47
|
1 |
|
data = OrderedDict() |
|
48
|
1 |
|
data['name'] = template.name |
|
49
|
1 |
|
data['description'] = template.link |
|
50
|
1 |
|
data['aliases'] = sorted(template.aliases + [template.key]) |
|
51
|
1 |
|
data['styles'] = template.styles |
|
52
|
|
|
data['example'] = route('links.get', key=key, |
|
53
|
1 |
|
path=template.sample_path, _external=True) |
|
54
|
|
|
return data |
|
55
|
1 |
|
|
|
56
|
1 |
|
elif request.method == 'POST': |
|
57
|
1 |
|
if top or bottom: |
|
58
|
|
|
text = Text([top, bottom], translate_spaces=False) |
|
59
|
1 |
|
else: |
|
60
|
|
|
text = Text("_") |
|
61
|
1 |
|
|
|
62
|
|
|
if _masked: |
|
63
|
1 |
|
code = current_app.link_service.encode(key, text.path) |
|
64
|
1 |
|
url = route('image.get_encoded', code=code, _external=True) |
|
65
|
|
|
else: |
|
66
|
1 |
|
url = route('image.get', key=key, path=text.path, _external=True) |
|
67
|
|
|
|
|
68
|
|
|
if _redirect: |
|
69
|
|
|
return redirect(url, 303) |
|
70
|
|
|
else: |
|
71
|
|
|
return dict(href=url) |
|
72
|
1 |
|
|
|
73
|
|
|
else: # pragma: no cover |
|
74
|
|
|
assert None |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
@blueprint.route("<key>/<path:path>") |
|
78
|
|
|
def get_meme_with_path(key, path): |
|
79
|
|
|
"""Redirect if any additional path is provided.""" |
|
80
|
|
|
template = current_app.template_service.find(key) |
|
81
|
|
|
return redirect("/{}/{}".format(template.key, path)) |
|
82
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.