-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
87 lines (74 loc) · 3.07 KB
/
common.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
import tornado.web
import itertools
from queries import SQLqueries
query_obj = SQLqueries()
import redis
import json
import MySQLdb
from mysql_configure import MYSQLConfiguration
r = redis.StrictRedis(host='localhost',db=4)
db_obj = MYSQLConfiguration()
class WriteData():
db = MySQLdb.connect(
# "localhost", "root", "root", "quizycash", charset='utf8', use_unicode=True)
db_obj.host, db_obj.user, db_obj.password, db_obj.db, charset='utf8', use_unicode=True)
def get_user_data(self, id):
game_cursor = self.db.cursor()
game_show_sql = query_obj.show_user_data % int(id)
game_cursor.execute(game_show_sql)
game_desc = game_cursor.description
column_names = [col[0] for col in game_desc]
data = [dict(itertools.izip(column_names, row))
for row in game_cursor.fetchall()]
if data:
return data
else:
return None
def append_player_balance(self,id,balance):
game_cursor = self.db.cursor()
#game_show_sql = query_obj.show_active_games % int(id)
sql_query = "UPDATE `quizycash`.`user` SET `virtual_money`=%f WHERE `id`=%d " %(balance,id)
game_cursor.execute(sql_query)
self.db.commit()
class CommonBaseHandler(tornado.web.RequestHandler):
@property
def db(self):
return self.application.db
def set_headers(self):
self.set_header('Content-Type', 'application/json')
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods', 'POST, GET')
def get_game_data(self,id):
game_cursor = self.db.cursor()
game_show_sql = query_obj.show_active_games % int(id)
game_cursor.execute(game_show_sql)
game_desc = game_cursor.description
column_names = [col[0] for col in game_desc]
data = [dict(itertools.izip(column_names, row))
for row in game_cursor.fetchall()]
if data:
return data
else:
return None
def get_user_data(self, id):
game_cursor = self.db.cursor()
game_show_sql = query_obj.show_user_data % int(id)
game_cursor.execute(game_show_sql)
game_desc = game_cursor.description
column_names = [col[0] for col in game_desc]
data = [dict(itertools.izip(column_names, row))
for row in game_cursor.fetchall()]
if data:
return data
else:
return None
def append_player_balance(self,id,balance):
game_cursor = self.db.cursor()
#game_show_sql = query_obj.show_active_games % int(id)
sql_query = "UPDATE `quizycash`.`user` SET `virtual_money`=%f WHERE `id`=%d " %(balance,id)
game_cursor.execute(sql_query)
self.db.commit()
def redis_session_check(self, user_name):
return json.loads(r.get("user_session").decode('utf-8'))