Skip to content

Commit 65ec82f

Browse files
committed
Remove chapter5<RESTful API>
1 parent 442d455 commit 65ec82f

File tree

22 files changed

+135
-720
lines changed

22 files changed

+135
-720
lines changed

chapter12/section4/shell.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,31 @@
66
import atexit
77
import code
88

9-
from traitlets.config.loader import Config
9+
from IPython.terminal.prompts import Prompts, Token
1010

1111
try:
1212
import IPython # noqa
1313
has_ipython = True
14-
cfg = Config()
15-
prompt_config = cfg.PromptManager
16-
prompt_config.in_template = 'In <\\#>: '
17-
prompt_config.in2_template = ' .\\D.: '
18-
prompt_config.out_template = 'Out<\\#>: '
1914
except ImportError:
2015
has_ipython = False
2116

2217

18+
class MyPrompt(Prompts):
19+
def in_prompt_tokens(self, cli=None): # default
20+
return [
21+
(Token.Prompt, 'In <'),
22+
(Token.PromptNum, str(self.shell.execution_count)),
23+
(Token.Prompt, '>: '),
24+
]
25+
26+
def out_prompt_tokens(self):
27+
return [
28+
(Token.OutPrompt, 'Out<'),
29+
(Token.OutPromptNum, str(self.shell.execution_count)),
30+
(Token.OutPrompt, '>: '),
31+
]
32+
33+
2334
def hook_readline_hist():
2435
try:
2536
import readline
@@ -62,14 +73,13 @@ def plain_shell(user_ns):
6273

6374
def ipython_shell(user_ns):
6475
from IPython.terminal.ipapp import TerminalIPythonApp
65-
#from IPython.terminal.interactiveshell import TerminalInteractiveShell
66-
from IPython.terminal.ptshell import TerminalInteractiveShell
76+
from IPython.terminal.interactiveshell import TerminalInteractiveShell
6777

6878
class MyIPythonApp(TerminalIPythonApp):
6979

7080
def init_shell(self):
71-
self.shell = TerminalInteractiveShell.instance(
72-
config=cfg,
81+
self.shell = TerminalInteractiveShell(
82+
prompts_class=MyPrompt, highlighting_style='emacs',
7383
display_banner=False, profile_dir=self.profile_dir,
7484
ipython_dir=self.ipython_dir, banner1=get_banner(), banner2='')
7585
self.shell.configurables.append(self)

chapter4/section4/app.py

-30
This file was deleted.

chapter5/section3/__init__.py

Whitespace-only changes.

chapter5/section3/api_server.py

-66
This file was deleted.

chapter5/section3/app.py

+20-120
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,29 @@
11
# coding=utf-8
2-
import os
2+
from flask import Flask, jsonify, request, render_template
33

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',
145
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)
326

33-
return new_paste.url_i
347

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('/')
489
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'})
12727

12828

12929
if __name__ == '__main__':

chapter5/section3/config.py

-5
This file was deleted.

chapter5/section3/ext.py

-6
This file was deleted.

0 commit comments

Comments
 (0)