-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
174 lines (144 loc) · 5.15 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding:utf-8 -*-
from flask import Flask,session,make_response,jsonify,url_for,redirect,request,abort,render_template,flash,send_from_directory
from forms import LoginForm,UploadForm,RichTextForm,NewNoteForm,EditNoteForm,DeleteNoteForm
from flask_ckeditor import CKEditor
from flask_sqlalchemy import SQLAlchemy
import os,uuid,click
app = Flask(__name__)
app.secret_key=os.getenv('SECRET_KEY','aeteadfASDF')
app.config['MAX_CONTENT_LENGTH']=3 * 1024 * 1024
app.config['UPLOAD_PATH'] = os.path.join(app.root_path,'uploads')
app.config['CKEDITOR_SERVE_LOCAL'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL','sqlite:///'+os.path.join(app.root_path,'data.db'))
ckeditor = CKEditor(app)
db = SQLAlchemy(app)
user = {'username' : 'hb Zh','bio':'A boy love movies'}
movies = [{'name' : 'The Big Short','year': '2015'},
{'name':'Too Big Too Fail','year' :'2011'},
{'name':'L\'Outsider','year':'2018'}]
@app.cli.command()
def initdb():
db.create_all()
click.echo ("init db")
class Note(db.Model):
id = db.Column(db.Integer,primary_key = True)
body = db.Column(db.Text)
def random_filename(filename):
ext = os.path.splitext(filename)[1]
new_filename = uuid.uuid4().hex + ext
return new_filename
@app.route('/')
def index():
return render_template('index.html')
@app.route('/hello')
def hello():
name = request.args.get('name')
if name is None:
name = request.cookies.get('name','human')
response ='<h1>hello %s </h1>' % name
if 'logged_in' in session:
response += '[authenicated]'
else:
abort(403)
return response
@app.route('/foo')
def foo():
return '<h1>foo</h1><a href="%s">test</a>' % url_for('do_something',next=request.full_path)
@app.route('/bar')
def bar():
return '<h1>bar</h1><a href="%s">test</a>' % url_for('do_something',next=request.full_path)
@app.route('/upload-image')
def show_images():
return render_template('uploaded.html')
@app.route('/set/<name>')
def set_cookie(name):
response = make_response(redirect(url_for('hello')))
response.set_cookie('name',name)
return response
@app.route('/login',methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
username = form.username.data
flash('Welcome %s' % username)
return redirect(url_for('index'))
#return render_template('login.html',form=form)
return render_template('bootstrap.html',form=form)
def redirect_back(default='hello',**kwargs):
for target in request.args.get('next'),request.referrer:
if target:
return redirect(target)
return redirect(url_for(default,**kwargs))
@app.route('/do_something_and_redirect')
def do_something():
return redirect_back()
@app.route('/watchlist')
def watchlist():
return render_template('watchlist.html',user=user,movies=movies)
@app.route('/flash')
def just_flash():
flash(u'你好,我是闪电')
return redirect(url_for('index'))
@app.route('/upload',methods=['GET','POST'])
def upload():
form = UploadForm()
if form.validate_on_submit():
f = form.photo.data
filename = random_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_PATH'],filename))
flash('Upload success')
session['filenames']=[filename]
return redirect(url_for('show_images'))
return render_template('upload.html',form=form)
@app.route('/upload/<path:filename>')
def get_file(filename):
return send_from_directory(app.config['UPLOAD_PATH'],filename)
@app.route('/ckeditor',methods=['GET','POST'])
def intergrate_ckeditor():
form = RichTextForm()
if form.validate_on_submit():
title = form.title.data
body = form.body.data
flash('Your post is published')
return render_template('post.html',title=title,body=body)
return render_template('ckeditor.html',form=form)
@app.errorhandler(404)
def page_not_found(e):
return render_template('errors/404.html'),404
@app.route('/new',methods=['GET','POST'])
def new_note():
form = NewNoteForm()
if form.validate_on_submit():
body = form.body.data
note = Note(body=body)
db.session.add(note)
db.session.commit()
return redirect(url_for('index'))
return render_template('new_note.html',form=form)
@app.route('/read')
def read_note():
notes = Note.query.all()
form = DeleteNoteForm()
return render_template('read_notes.html',notes=notes,form=form)
@app.route('/edit/<int:note_id>',methods=['GET','POST'])
def edit_note(note_id):
form = EditNoteForm()
note = Note.query.get(note_id)
if form.validate_on_submit():
note = Note.query.get(note_id)
note.body = form.body.data
db.session.commit()
return redirect(url_for('read_note'))
form.body.data = note.body
return render_template('edit_note.html',form=form)
@app.route('/delete/<int:note_id>',methods=['POST'])
def delete_note(note_id):
form = DeleteNoteForm()
if form.validate_on_submit():
note = Note.query.get(note_id)
db.session.delete(note)
db.session.commit()
flash('your note is delete')
else:
abort(400)
return redirect(url_for('index'))