-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
103 lines (81 loc) · 3.28 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
__author__ = "Huy Phan <dachuy@gmail.com>"
__license__ = "WTFPL"
__version__ = "0.1"
__maintainer__ = "Huy Phan"
__email__ = "dachuy@gmail.com"
__status__ = "Development"
from flask import Flask, request, abort, make_response, jsonify
from whoosh.query import Every
from whoosh.qparser import QueryParser, MultifieldParser
import whoosh.index
import json
import uuid
import config
index = whoosh.index.open_dir(config.INDEX_DIR)
app = Flask(__name__, static_url_path='/static')
@app.errorhandler(500)
def internal_error(error):
return jsonify(error=500, message=str(error)), 500
@app.route('/')
def main():
return app.send_static_file('index.html')
def get_snippet_by_id(snippet_id):
return index.searcher().document(id=snippet_id)
@app.route('/snippets', methods=['GET'])
def search_snippet():
query = request.args.get('query', None)
page = int(request.args.get('page', 1))
if query:
qp = MultifieldParser(["title", "content", "tag"], schema=index.schema)
q = qp.parse(query)
else:
q = Every()
response = {"results":[], "total": 0}
with index.searcher() as searcher:
results = searcher.search_page(q, page, pagelen=config.SEARCH_PAGINATION, sortedby="title")
for snippet in results:
response["results"].append({'id': snippet['id'], 'title': snippet['title']})
response["total_snippets"] = len(results)
response["total_pages"] = (len(results) - 1) / config.SEARCH_PAGINATION + 1
response["current_page"] = page
return json.dumps(response)
return json.dumps(response)
@app.route('/snippets', methods=['POST'])
def add_snippet():
title = request.form['title'].strip()
content = request.form['content'].strip()
tag = request.form['tag']
language = request.form['language']
snippet_id = unicode(uuid.uuid4())
if not title or not content:
raise Exception("Empty title or snippet content")
writer = index.writer()
writer.update_document(id=snippet_id, content=content, tag=tag, title=title, language=language)
writer.commit()
return '{"success": true, "message": "Snippet added successfully", "snippet_id": "%s"}' % snippet_id
@app.route('/snippets/<snippet_id>', methods=['GET'])
def get_snippet(snippet_id):
snippet = get_snippet_by_id(snippet_id)
if not snippet:
abort(404)
return json.dumps(snippet)
@app.route('/snippets/<snippet_id>', methods=['PUT'])
def update_snippet(snippet_id):
title = request.form['title']
content = request.form['content']
language = request.form['language']
tag = request.form['tag']
if not title or not content:
raise Exception("Empty title or content")
if not get_snippet_by_id(snippet_id):
abort(make_response('{"message": "The snippet you are trying to update doesn\'t exist"}', 404))
writer = index.writer()
writer.update_document(id=snippet_id, content=content, tag=tag, title=title, language=language)
writer.commit()
return '{"success": true, "message": "Snippet added successfully", "snippet_id": "%s"}' % snippet_id
@app.route('/snippets/<snippet_id>', methods=['DELETE'])
def delete_snippet(snippet_id):
writer = index.writer()
writer.delete_by_term("id", snippet_id)
writer.commit()
return '{"message": "Snippet deleted successfully"}'