|
1 | 1 | # coding=utf-8
|
2 |
| -import os |
| 2 | +from flask import Flask, jsonify, request, render_template |
3 | 3 |
|
4 |
| -from werkzeug import SharedDataMiddleware |
5 |
| -from flask import abort, Flask, request, jsonify, redirect, send_file |
6 |
| - |
7 |
| -from ext import db, mako, render_template |
8 |
| -from models import PasteFile |
9 |
| -from utils import get_file_path, humanize_bytes |
10 |
| - |
11 |
| -ONE_MONTH = 60 * 60 * 24 * 30 |
12 |
| - |
13 |
| -app = Flask(__name__, template_folder='../../templates/r', |
| 4 | +app = Flask(__name__, template_folder='../../templates', |
14 | 5 | static_folder='../../static')
|
15 |
| -app.config.from_object('config') |
16 |
| - |
17 |
| -app.wsgi_app = SharedDataMiddleware(app.wsgi_app, { |
18 |
| - '/i/': get_file_path() |
19 |
| -}) |
20 |
| - |
21 |
| -mako.init_app(app) |
22 |
| -db.init_app(app) |
23 |
| - |
24 |
| - |
25 |
| -@app.route('/r/<img_hash>') |
26 |
| -def rsize(img_hash): |
27 |
| - w = request.args['w'] |
28 |
| - h = request.args['h'] |
29 |
| - |
30 |
| - old_paste = PasteFile.get_by_filehash(img_hash) |
31 |
| - new_paste = PasteFile.rsize(old_paste, w, h) |
32 | 6 |
|
33 |
| - return new_paste.url_i |
34 | 7 |
|
35 |
| - |
36 |
| -@app.route('/d/<filehash>', methods=['GET']) |
37 |
| -def download(filehash): |
38 |
| - paste_file = PasteFile.get_by_filehash(filehash) |
39 |
| - |
40 |
| - return send_file(open(paste_file.path, 'rb'), |
41 |
| - mimetype='application/octet-stream', |
42 |
| - cache_timeout=ONE_MONTH, |
43 |
| - as_attachment=True, |
44 |
| - attachment_filename=paste_file.filename.encode('utf-8')) |
45 |
| - |
46 |
| - |
47 |
| -@app.route('/', methods=['GET', 'POST']) |
| 8 | +@app.route('/') |
48 | 9 | def index():
|
49 |
| - if request.method == 'POST': |
50 |
| - uploaded_file = request.files['file'] |
51 |
| - w = request.form.get('w') |
52 |
| - h = request.form.get('h') |
53 |
| - if not uploaded_file: |
54 |
| - return abort(400) |
55 |
| - |
56 |
| - if w and h: |
57 |
| - paste_file = PasteFile.rsize(uploaded_file, w, h) |
58 |
| - else: |
59 |
| - paste_file = PasteFile.create_by_upload_file(uploaded_file) |
60 |
| - db.session.add(paste_file) |
61 |
| - db.session.commit() |
62 |
| - |
63 |
| - return jsonify({ |
64 |
| - 'url_d': paste_file.url_d, |
65 |
| - 'url_i': paste_file.url_i, |
66 |
| - 'url_s': paste_file.url_s, |
67 |
| - 'url_p': paste_file.url_p, |
68 |
| - 'filename': paste_file.filename, |
69 |
| - 'size': humanize_bytes(paste_file.size), |
70 |
| - 'time': str(paste_file.uploadtime), |
71 |
| - 'type': paste_file.type, |
72 |
| - 'quoteurl': paste_file.quoteurl |
73 |
| - }) |
74 |
| - return render_template('index.html', **locals()) |
75 |
| - |
76 |
| - |
77 |
| -@app.after_request |
78 |
| -def after_request(response): |
79 |
| - response.headers['Access-Control-Allow-Origin'] = '*' |
80 |
| - response.headers['Access-Control-Allow-Headers'] = 'Content-Type' |
81 |
| - return response |
82 |
| - |
83 |
| - |
84 |
| -@app.route('/j', methods=['POST']) |
85 |
| -def j(): |
86 |
| - uploaded_file = request.files['file'] |
87 |
| - |
88 |
| - if uploaded_file: |
89 |
| - paste_file = PasteFile.create_by_upload_file(uploaded_file) |
90 |
| - db.session.add(paste_file) |
91 |
| - db.session.commit() |
92 |
| - width, height = paste_file.image_size |
93 |
| - |
94 |
| - return jsonify({ |
95 |
| - 'url': paste_file.url_i, |
96 |
| - 'short_url': paste_file.url_s, |
97 |
| - 'origin_filename': paste_file.filename, |
98 |
| - 'hash': paste_file.filehash, |
99 |
| - 'width': width, |
100 |
| - 'height': height |
101 |
| - }) |
102 |
| - |
103 |
| - return abort(400) |
104 |
| - |
105 |
| - |
106 |
| -@app.route('/p/<filehash>') |
107 |
| -def preview(filehash): |
108 |
| - paste_file = PasteFile.get_by_filehash(filehash) |
109 |
| - |
110 |
| - if not paste_file: |
111 |
| - filepath = get_file_path(filehash) |
112 |
| - if not(os.path.exists(filepath) and (not os.path.islink(filepath))): |
113 |
| - return abort(404) |
114 |
| - |
115 |
| - paste_file = PasteFile.create_by_old_paste(filehash) |
116 |
| - db.session.add(paste_file) |
117 |
| - db.session.commit() |
118 |
| - |
119 |
| - return render_template('success.html', p=paste_file) |
120 |
| - |
121 |
| - |
122 |
| -@app.route('/s/<symlink>') |
123 |
| -def s(symlink): |
124 |
| - paste_file = PasteFile.get_by_symlink(symlink) |
125 |
| - |
126 |
| - return redirect(paste_file.url_p) |
| 10 | + return render_template('chapter5/section3/signin_fetch.html') |
| 11 | + |
| 12 | + |
| 13 | +@app.route('/signin', methods=['POST']) |
| 14 | +def signin(): |
| 15 | + username = request.form['username'] |
| 16 | + password = request.form['password'] |
| 17 | + error = None |
| 18 | + if len(username) < 5: |
| 19 | + error = 'Password must be at least 5 characters' |
| 20 | + if len(password) < 6: |
| 21 | + error = 'Password must be at least 8 characters' |
| 22 | + elif not any(c.isupper() for c in password): |
| 23 | + error = 'Your password needs at least 1 capital' |
| 24 | + if error is not None: |
| 25 | + return jsonify({'r': 1, 'error': error}) |
| 26 | + return jsonify({'r': 0, 'rs': 'Ok'}) |
127 | 27 |
|
128 | 28 |
|
129 | 29 | if __name__ == '__main__':
|
|
0 commit comments