-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconbadge_app.py
56 lines (46 loc) · 1.78 KB
/
conbadge_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (c) Weasyl LLC
# See COPYING for details.
import os.path
from flask import Flask, abort, redirect, render_template, request, url_for
from PIL import Image
from conbadge import AvatarFetchError, weasyl_badge, weasyl_sysname
# Valid upscaling options for resizing a user's avatar.
upscaling = {
'antialias': Image.ANTIALIAS,
'bilinear': Image.BILINEAR,
'bicubic': Image.BICUBIC,
'nearest': Image.NEAREST,
}
app = Flask(__name__)
def badge_path(sysname):
"""Returns a path pointing to the badge for a given sysname."""
return os.path.join('static', 'badges', sysname + '.png')
def badge_url(sysname):
"""Returns a URL pointing to the badge for a given sysname."""
return url_for('static', filename=os.path.join('badges', sysname + '.png'))
@app.route('/')
def index():
"""Serves the rendered index template."""
return render_template('index.html')
@app.route('/', methods=['POST'])
def generate_badge():
"""Renders a badge for a user and redirects to a page displaying it."""
username = request.form['username']
upscale_method = upscaling.get(
request.form.get('upscaling'), Image.ANTIALIAS)
try:
badge = weasyl_badge(username, upscale_method)
except AvatarFetchError, e:
error = 'Weasyl reported an error: %(text)s' % e.args[0]
return render_template('index.html', error=error)
sysname = weasyl_sysname(username)
badge.save(badge_path(sysname))
return redirect(url_for('show_badge', sysname=sysname))
@app.route('/badge/<sysname>')
def show_badge(sysname):
"""Show the given user's badge."""
path = badge_path(sysname)
if not os.path.exists(path):
abort(404)
return render_template('show-badge.html',
sysname=sysname, badge_url=badge_url(sysname))