-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
96 lines (81 loc) · 3.11 KB
/
__init__.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
import os
import sys
import json
from . import db
sys.path.append("Soara/")
from flask import Flask, jsonify, redirect, render_template, url_for
from flask_restful import Api, Resource
from config import Config
from Soara.auth import login_required
# from flask_sqlalchemy import SQLAlchemy
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__)
app.config.from_object(Config)
app.secret_key = os.getenv("SECRET_KEY")
api = Api(app)
db.init_app(app)
from . import auth, search
app.register_blueprint(auth.bp)
app.register_blueprint(search.bp)
app.add_url_rule('/', endpoint='index')
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# flask --app Soara init-db
# flask --app Soara --debug run
# @login_required
# @app.route('/index')
# def index():
# return render_template('index.html')
# restful implementation of Flask API
# class SearchResult(Resource):
# def get(self, q, random):
# cursor = db.get_db()
# select_query = 'SELECT COUNT(*) FROM corpus WHERE text LIKE ?;'
# count = cursor.execute(
# select_query,
# ('% '+q+' %',)).fetchone()
# if random==1:
# select_query = 'SELECT text FROM corpus WHERE text LIKE ? ORDER BY RANDOM() LIMIT 10;'
# else:
# select_query = 'SELECT text FROM corpus WHERE text LIKE ? LIMIT 10;'
# result = cursor.execute(
# select_query,
# ('% '+q+' %',)).fetchall()
# result = [tuple(row) for row in result]
# result.append(tuple(count))
# db.close_db()
# return jsonify(result)
# api.add_resource(SearchResult, "/search/q=<string:q>&r=<int:random>")
# NATIVE implementation of REST API
# @login_required
# @app.route('/search/q=<string:q>&r=<int:random>', methods=['GET'])
# # protect against injections: https://github.com/TryGhost/node-sqlite3/issues/57
# def search_corpus(q, random):
# cursor = db.get_db()
# select_query = 'SELECT COUNT(*) FROM corpus WHERE text LIKE ?;'
# count = cursor.execute(
# select_query,
# ('% '+q+' %',)).fetchone()
# if random==1:
# select_query = 'SELECT text FROM corpus WHERE text LIKE ? ORDER BY RANDOM() LIMIT 10;'
# else:
# select_query = 'SELECT text FROM corpus WHERE text LIKE ? LIMIT 10;'
# result = cursor.execute(
# select_query,
# ('% '+q+' %',)).fetchall()
# result = [tuple(row) for row in result]
# result.append(tuple(count))
# db.close_db()
# return jsonify(result)
# adding regexp implementation: https://github.com/thomasnield/oreilly_intermediate_sql_for_data/issues/5
return app