forked from cs50/help50-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
43 lines (39 loc) · 1.34 KB
/
model.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
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from manage import Output, Input
from pytz import timezone
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import os
uri = "mysql://" + os.environ["MYSQL_USERNAME"] + ":" + os.environ["MYSQL_PASSWORD"] + "@" + os.environ["MYSQL_HOST"] + "/" + os.environ["MYSQL_DATABASE"]
engine = create_engine(uri)
Session = sessionmaker(bind=engine)
session = Session()
# logs input (and any helpful output)
def log(cmd, username, script, output):
output_id = None
if (output):
help_out = Output(output=output)
session.add(help_out)
session.commit()
output_id = help_out.id
help_in = Input(cmd=cmd, username=username, script=script, output_id=output_id, created=datetime.utcnow())
session.add(help_in)
try:
session.commit()
except:
session.rollback()
raise
# gets unreviewed inputs with no output
def unreviewed_matchless():
return session.query(Input).filter(Input.output_id==None, Input.reviewed==False).order_by(Input.created).all()
# mark an input as reviewed
def mark_reviewed(input_id):
row = session.query(Input).filter(Input.id==input_id).first()
if (row != None):
row.reviewed = True
try:
session.commit()
except:
session.rollback()
raise