-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
143 lines (103 loc) · 3.54 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
import os
import hmac
from hashlib import sha1
from flask import Flask, g, render_template, session, abort, request
from werkzeug.security import safe_str_cmp
import flask_sijax
import markdown
from markdown import Markdown
from markdown_checklist.extension import ChecklistExtension
from mdx_unimoji import UnimojiExtension
app = Flask(__name__)
app.secret_key = os.urandom(128)
app.config["SIJAX_STATIC_PATH"] = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/')
app.config["SIJAX_JSON_URI"] = '/static/js/sijax/json2.js'
flask_sijax.Sijax(app)
@app.template_global('csrf_token')
def csrf_token():
"""
Generate a token string from bytes arrays. The token in the session is user
specific.
"""
if "_csrf_token" not in session:
session["_csrf_token"] = os.urandom(128)
return hmac.new(app.secret_key, session["_csrf_token"],
digestmod=sha1).hexdigest()
@app.before_request
def check_csrf_token():
"""Checks that token is correct, aborting if not"""
if request.method in ("GET",): # not exhaustive list
return
token = request.form.get("csrf_token")
if token is None:
app.logger.warning("Expected CSRF Token: not present")
abort(400)
if not safe_str_cmp(token, csrf_token()):
app.logger.warning("CSRF Token incorrect")
abort(400)
# Setup for markdown extensions
MARKDOWN_EXTENSIONS = [
ChecklistExtension(),
UnimojiExtension(),
'fenced_code',
'codehilite',
'extra',
'tables',
'admonition',
'abbr',
'attr_list',
'def_list',
'footnotes',
'legacy_em',
'nl2br',
'sane_lists',
'smarty',
'toc',
]
# Initialize markdown object
md = Markdown(extensions=MARKDOWN_EXTENSIONS)
class SijaxHandler(object):
@staticmethod
def save_message(obj_response, message):
message = message.strip()
if message == '':
return obj_response.alert("Empty texts are not allowed!")
# Save message to database or whatever..
import time, hashlib
time_txt = time.strftime("%H:%M:%S", time.gmtime(time.time()))
message_id = 'message_mkdown'
mkd = md.convert(message)
message = """
<div id="%s" style="opacity: 1;">
%s
</div>
""" % (message_id, mkd)
obj_response.html('#viewMarkdown', message)
# Clear the textbox and give it focus in case it has lost it
# obj_response.attr('#mkdown', 'value', '')
# obj_response.script("$('#mkdown').focus();")
obj_response.script("$('#viewMarkdown').attr('scrollTop', $('#viewMarkdown').attr('scrollHeight'));")
# Make the new message appear in 400ms
obj_response.script("$('#%s').animate({opacity: 1}, 400);" % message_id)
@staticmethod
def clear_messages(obj_response):
# Delete all messages from the database
# Clear the messages container
obj_response.html('#viewMarkdown', '')
# Clear the textbox
obj_response.attr('#mkdown', 'value', '')
# Ensure the texbox has focus
obj_response.script("$('#mkdown').focus();")
# views here
@flask_sijax.route(app, "/")
def home():
# if request.method == "POST":
# mkd = md.convert(request.form['simplemarkdown'])
# return render_template('home.html', mkd=mkd)
# mkd = ''
if g.sijax.is_sijax_request:
g.sijax.register_object(SijaxHandler)
return g.sijax.process_request()
return render_template('home.html')
if __name__ == '__main__':
app.run(debug=True)