-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
151 lines (121 loc) · 3.87 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
from flask import Flask, render_template, request, g, redirect, url_for, jsonify
import sqlite3
import os, time
app = Flask(__name__)
def connect_db():
sql = sqlite3.connect('./fts_memo.db')
sql.row_factory = sqlite3.Row
return sql
def get_db():
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,
endpoint, filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
@app.route('/')
def index():
db = get_db()
memo_cur = db.execute('select rowid as id, title, content from memo order by rowid desc')
memos = memo_cur.fetchall()
return render_template('home.html', memos=memos)
@app.route('/write', methods=['POST'])
def write():
print(request.form)
db = get_db()
db.execute('insert into memo (title, content) values (?, ?)', [request.form['title'], request.form['content']])
db.commit()
return redirect(url_for('index'))
@app.route('/delete/<memo_id>', methods=['POST'])
def delete(memo_id):
db = get_db()
db.execute('delete from memo where rowid=?', [memo_id])
db.commit()
return jsonify({
'success': True
})
#return redirect(url_for('index'))
@app.route('/get_memo/<memo_id>')
def get_memo(memo_id):
print('get_memo, ', memo_id)
db = get_db()
memo_cur = db.execute('select rowid, title, content from memo where rowid=?', [memo_id])
memo = memo_cur.fetchone()
return jsonify({
'id': memo['rowid'],
'title': memo['title'],
'content': memo['content']
})
@app.route('/get_all_memos')
def get_all_memos():
db = get_db()
memo_cur = db.execute('select rowid, title, content from memo order by rowid desc')
memos = memo_cur.fetchall()
memos_list = []
for memo in memos:
print(memo)
memos_list.append({
'id': memo['rowid'],
'title': memo['title'],
'content': memo['content']
})
return jsonify(memos_list)
@app.route('/search/', defaults={'query_text': ''})
@app.route('/search/<query_text>', methods=['GET'])
def search(query_text):
print('query_text: ', query_text)
db = get_db()
if query_text == '':
memo_cur = db.execute('select rowid, title, content from memo')
else:
memo_cur = db.execute('select rowid, title, content from memo where memo match ?', [query_text + '*'])
memos = memo_cur.fetchall()
memos_list = []
for memo in memos:
memos_list.append({
'id': memo['rowid'],
'title': memo['title'],
'content': memo['content']
})
return jsonify(memos_list)
@app.route('/save', methods=['POST'])
def save_memo():
request_json = request.json
db = get_db()
db.execute('update memo set title=?, content=? where rowid=?', [request_json['title'], request_json['content'], request_json['id']])
db.commit()
return jsonify({
'success': True
})
@app.route('/create', methods=['PUT'])
def create_memo():
db = get_db()
cur = db.cursor()
cur.execute('insert into memo (title, content) values ("", "")')
inserted_id = cur.lastrowid
db.commit()
print(inserted_id)
return jsonify({
'success': True,
'id': inserted_id,
'title': '',
'content': ''
})
@app.route('/clear/<memo_id>', methods=['POST'])
def clear_memo(memo_id):
db = get_db()
db.execute('update memo set title=?, content=? where rowid=?', ['', '', memo_id])
db.commit()
return jsonify({
'success': True
})
if __name__ == '__main__':
app.run(debug=True)