Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions db_api/db_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@

app = Flask(__name__)
api = Api(app)
try:
# Open database connection
db = MySQLdb.connect("localhost", "root", "zrada", "zrada", charset='utf8', use_unicode = True )
# prepare a cursor object using cursor() method
cursor = db.cursor()
except Exception as err:
print(err)
# disconnect from server
db.close()
app.config.update(SERVER_NAME='casper.thehappinessatworkproject.com:5050')

@app.route('/db_api/topenactments', methods=['get'])
@app.route('/db_api/topenactments', methods=['GET'])
@cross_origin()
def get_top_enactments():
try:
sql = "SELECT * FROM enactments where reiting LIKE 1"
connection = MySQLdb.connect("localhost", "voter", "deputany", "zrada", charset='utf8', use_unicode = True)
cursor = connection.cursor()
#sql = "SELECT * FROM enactments where reiting LIKE 1"
sql = "SELECT * FROM enactments"
cursor.execute(sql)
enactments = cursor.fetchall()
list_enactments = []
Expand All @@ -36,49 +31,44 @@ def get_top_enactments():
"time": enactment[2],
"description": enactment[3],
"result": enactment[4]})

connection.close()
return jsonify({"enactments": list_enactments})

@app.route('/db_api/process', methods=['post'])
@app.route('/db_api/process', methods=['POST'])
@cross_origin()
def process_user_votes():
data = request.get_json()
#print(data)
result = process_voting(data)
connection = MySQLdb.connect("localhost", "voter", "deputany", "zrada", charset='utf8', use_unicode = True)
result = process_voting(data, connection)
connection.close()
return result, 201


def process_voting(data):

sql = "SELECT * FROM deputats WHERE candidate <> 0"
def process_voting(data, connection):
cursor = connection.cursor()
sql = "SELECT * FROM deputats"
cursor.execute(sql)
deputats = cursor.fetchall()
voting_matches = {}
print(len(deputats))
for element in data:
sql = """SELECT * FROM voting WHERE enactment = "%s" AND vote = "'%s'";""" % (element['enactment'], element['vote'],)
cursor.execute(sql)
list_voting = list(cursor.fetchall())
for deputat in deputats:
for tupl in list_voting:
if tupl[0] == "'"+deputat[0]+"'":

for deputat in deputats:
sql = """SELECT * FROM voting WHERE person = "'%s'" AND enactment = "%s" AND vote = "'%s'";""" % (deputat[0], element['enactment'], element['vote'],)
print(sql)
cursor.execute(sql)
match = cursor.fetchone()
print(match)
if(match):
if deputat[0] in voting_matches:
voting_matches[deputat[0]]['matches']+=1

else:
voting_matches[deputat[0]]={'name': deputat[1], 'matches': 1}
if deputat[0] in voting_matches:
voting_matches[deputat[0]]['matches']+=1

else:
voting_matches[deputat[0]]={'name': deputat[1], 'matches': 1}

sorted_result = sorted(voting_matches.items(), key=lambda x: x[1], reverse=True)
print(sorted_result)
return jsonify(sorted_result)



if __name__ == '__main__':
app.run(debug=True)
context = ('cert/server.crt', 'cert/server.key')
app.run(debug=False, ssl_context=context)


db.close()
14 changes: 14 additions & 0 deletions deputany.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=deputany service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=daemon
WorkingDirectory=/opt/deputany/db_api
ExecStart=/usr/bin/python /opt/deputany/db_api/db_api.py

[Install]
WantedBy=multi-user.target
Loading