Skip to content

Commit

Permalink
Remove chapter5<RESTful API>
Browse files Browse the repository at this point in the history
  • Loading branch information
dongweiming committed Jul 20, 2016
1 parent 442d455 commit 65ec82f
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 720 deletions.
30 changes: 20 additions & 10 deletions chapter12/section4/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@
import atexit
import code

from traitlets.config.loader import Config
from IPython.terminal.prompts import Prompts, Token

try:
import IPython # noqa
has_ipython = True
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'In <\\#>: '
prompt_config.in2_template = ' .\\D.: '
prompt_config.out_template = 'Out<\\#>: '
except ImportError:
has_ipython = False


class MyPrompt(Prompts):
def in_prompt_tokens(self, cli=None): # default
return [
(Token.Prompt, 'In <'),
(Token.PromptNum, str(self.shell.execution_count)),
(Token.Prompt, '>: '),
]

def out_prompt_tokens(self):
return [
(Token.OutPrompt, 'Out<'),
(Token.OutPromptNum, str(self.shell.execution_count)),
(Token.OutPrompt, '>: '),
]


def hook_readline_hist():
try:
import readline
Expand Down Expand Up @@ -62,14 +73,13 @@ def plain_shell(user_ns):

def ipython_shell(user_ns):
from IPython.terminal.ipapp import TerminalIPythonApp
#from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.terminal.ptshell import TerminalInteractiveShell
from IPython.terminal.interactiveshell import TerminalInteractiveShell

class MyIPythonApp(TerminalIPythonApp):

def init_shell(self):
self.shell = TerminalInteractiveShell.instance(
config=cfg,
self.shell = TerminalInteractiveShell(
prompts_class=MyPrompt, highlighting_style='emacs',
display_banner=False, profile_dir=self.profile_dir,
ipython_dir=self.ipython_dir, banner1=get_banner(), banner2='')
self.shell.configurables.append(self)
Expand Down
30 changes: 0 additions & 30 deletions chapter4/section4/app.py

This file was deleted.

Empty file removed chapter5/section3/__init__.py
Empty file.
66 changes: 0 additions & 66 deletions chapter5/section3/api_server.py

This file was deleted.

140 changes: 20 additions & 120 deletions chapter5/section3/app.py
Original file line number Diff line number Diff line change
@@ -1,129 +1,29 @@
# coding=utf-8
import os
from flask import Flask, jsonify, request, render_template

from werkzeug import SharedDataMiddleware
from flask import abort, Flask, request, jsonify, redirect, send_file

from ext import db, mako, render_template
from models import PasteFile
from utils import get_file_path, humanize_bytes

ONE_MONTH = 60 * 60 * 24 * 30

app = Flask(__name__, template_folder='../../templates/r',
app = Flask(__name__, template_folder='../../templates',
static_folder='../../static')
app.config.from_object('config')

app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/i/': get_file_path()
})

mako.init_app(app)
db.init_app(app)


@app.route('/r/<img_hash>')
def rsize(img_hash):
w = request.args['w']
h = request.args['h']

old_paste = PasteFile.get_by_filehash(img_hash)
new_paste = PasteFile.rsize(old_paste, w, h)

return new_paste.url_i


@app.route('/d/<filehash>', methods=['GET'])
def download(filehash):
paste_file = PasteFile.get_by_filehash(filehash)

return send_file(open(paste_file.path, 'rb'),
mimetype='application/octet-stream',
cache_timeout=ONE_MONTH,
as_attachment=True,
attachment_filename=paste_file.filename.encode('utf-8'))


@app.route('/', methods=['GET', 'POST'])
@app.route('/')
def index():
if request.method == 'POST':
uploaded_file = request.files['file']
w = request.form.get('w')
h = request.form.get('h')
if not uploaded_file:
return abort(400)

if w and h:
paste_file = PasteFile.rsize(uploaded_file, w, h)
else:
paste_file = PasteFile.create_by_upload_file(uploaded_file)
db.session.add(paste_file)
db.session.commit()

return jsonify({
'url_d': paste_file.url_d,
'url_i': paste_file.url_i,
'url_s': paste_file.url_s,
'url_p': paste_file.url_p,
'filename': paste_file.filename,
'size': humanize_bytes(paste_file.size),
'time': str(paste_file.uploadtime),
'type': paste_file.type,
'quoteurl': paste_file.quoteurl
})
return render_template('index.html', **locals())


@app.after_request
def after_request(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
return response


@app.route('/j', methods=['POST'])
def j():
uploaded_file = request.files['file']

if uploaded_file:
paste_file = PasteFile.create_by_upload_file(uploaded_file)
db.session.add(paste_file)
db.session.commit()
width, height = paste_file.image_size

return jsonify({
'url': paste_file.url_i,
'short_url': paste_file.url_s,
'origin_filename': paste_file.filename,
'hash': paste_file.filehash,
'width': width,
'height': height
})

return abort(400)


@app.route('/p/<filehash>')
def preview(filehash):
paste_file = PasteFile.get_by_filehash(filehash)

if not paste_file:
filepath = get_file_path(filehash)
if not(os.path.exists(filepath) and (not os.path.islink(filepath))):
return abort(404)

paste_file = PasteFile.create_by_old_paste(filehash)
db.session.add(paste_file)
db.session.commit()

return render_template('success.html', p=paste_file)


@app.route('/s/<symlink>')
def s(symlink):
paste_file = PasteFile.get_by_symlink(symlink)

return redirect(paste_file.url_p)
return render_template('chapter5/section3/signin_fetch.html')


@app.route('/signin', methods=['POST'])
def signin():
username = request.form['username']
password = request.form['password']
error = None
if len(username) < 5:
error = 'Password must be at least 5 characters'
if len(password) < 6:
error = 'Password must be at least 8 characters'
elif not any(c.isupper() for c in password):
error = 'Your password needs at least 1 capital'
if error is not None:
return jsonify({'r': 1, 'error': error})
return jsonify({'r': 0, 'rs': 'Ok'})


if __name__ == '__main__':
Expand Down
5 changes: 0 additions & 5 deletions chapter5/section3/config.py

This file was deleted.

6 changes: 0 additions & 6 deletions chapter5/section3/ext.py

This file was deleted.

Loading

0 comments on commit 65ec82f

Please sign in to comment.