-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
78 lines (57 loc) · 1.78 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
from flask import Flask, render_template, request
from flask_bootstrap import Bootstrap
from flask_script import Manager, Shell
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import MigrateCommand, Migrate
from flask_wtf.csrf import CsrfProtect
from config import Config
from uuid import uuid4
bootstrap = Bootstrap()
db = SQLAlchemy()
csrf = CsrfProtect()
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
csrf.init_app(app)
db.init_app(app)
bootstrap.init_app(app)
return app
app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
@app.route('/')
def main():
return render_template('app.html')
@app.route('/save_chords', methods=['POST'])
def save_chords():
raw_html = request.form.get('chords')
if not raw_html or len(raw_html) < 20:
return None
else:
url = uuid4().hex[:8]
new_chords = Content(html=raw_html, url=url)
db.session.add(new_chords)
return url
@app.route('/chords/<url>', methods=['GET'])
def get_chords(url):
col = Content.query.filter_by(url=url).first()
if col:
return render_template('chords.html', chords=col.html, url=col.url)
else:
return 'הדף המבוקש לא נמצא', 400
@app.route('/chords', methods=['POST', 'GET'])
def chords():
return render_template('chords.html')
# MODELS
class Content(db.Model):
html = db.Column(db.Text)
url = db.Column(db.String(9), primary_key=True, unique=True)
def __init__(self, html, url):
self.html = html
self.url = url
def make_shell_context():
return dict(app=app, db=db, Content=Content)
manager.add_command('db', MigrateCommand)
manager.add_command('shell', Shell(make_context=make_shell_context))
if __name__ == '__main__':
manager.run()