-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
105 lines (88 loc) · 3.1 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
import logging, os, ConfigParser, sys
from flask import Flask, url_for, request
from quotes import Quote
from jinja2 import Environment, FileSystemLoader
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import simplejson as json
import random
# Setup Logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
filehandler = logging.FileHandler("hoggy-search.log")
filehandler.setLevel(logging.INFO)
streamhandler = logging.StreamHandler()
log.addHandler(filehandler)
log.addHandler(streamhandler)
# Read config
log.debug("Reading config")
env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')))
config = ConfigParser.RawConfigParser()
config.read("config.ini")
hoggy_config = ConfigParser.RawConfigParser()
hoggy_config.read(config.get('hoggy', 'location') + '/config.ini')
sys.path.append(config.get('hoggy', 'location'))
import actions
log.info("Config loaded")
# Setup DB
log.debug("Loading database")
config_folder = os.path.dirname(os.path.realpath(config.get('hoggy', 'location'))) + "/Application"
if hoggy_config.get('db','type') != 'mysql':
sqlite_file = os.path.join(config_folder, hoggy_config.get('db', 'file'))
engine = create_engine('sqlite:///%s' % sqlite_file)
log.info("Database loaded")
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
class Quote(Base):
__tablename__ = "quotes"
id = Column(Integer, primary_key=True)
body = Column(String(200))
@classmethod
def get(cls, id):
return session.query(cls).get(id)
@classmethod
def get_random(cls):
rand = random.randrange(1, session.query(cls).count());
return session.query(cls).get(rand)
@classmethod
def list(cls, search):
if search:
return session.query(cls).filter(cls.body.like('%' + search + '%')).all()
else:
return session.query(cls).all()
@classmethod
def search(cls, search):
return session.query(cls).filter(cls.body.like('%' + search + '%')).all()
app = Flask(__name__, static_folder='static')
app.debug = True
@app.route("/")
def index():
quote = Quote.get_random()
tmpl = env.get_template('main.html')
return tmpl.render(id=quote.id, body=quote.body)
@app.route("/search", methods=["GET", "POST"])
def search():
if request.method == "GET":
quotes = Quote.list(request.args.get('query'))
tmpl = env.get_template('search.html')
return tmpl.render(quotes=quotes)
else:
term = request.form['query']
results = Quote.search(term)
to_return = []
for result in results:
to_return.append({
"id":result.id,
"body": result.body
})
return json.dumps(to_return)
@app.route("/help")
def help():
classes = actions.Commander.actions
tmpl = env.get_template('help.html')
return tmpl.render(classes=classes)
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0', port=8090, debug=True)