def add_test(): json_data = request.json if request.method == 'POST': DataBase.add_test(json_data['text'], json_data['uid'], json_data['name']) return jsonify([{"result": True}]) else: return abort(404)
def remove_test(id): if request.method == 'DELETE': DataBase.remove_test(id) return jsonify([{"result": True}]) elif request.method == 'GET': data = DataBase.get_one_test(id) result = jsonify(data) result.status_code = 200 return result else: return abort(404)
def user_stats(id): if request.method == "POST": json_data = request.json DataBase.add_stat(id, json_data['test_id'], json_data['test_time'], json_data['err_count'], json_data['pos']) return jsonify([{"result": True}]) elif request.method == "GET": data = DataBase.get_result(id) result = jsonify(data) return result else: return abort(404)
def login_as_user(): json_data = request.json if request.method == 'POST': get_id = DataBase.add_user(json_data['first_name'], json_data['last_name']) return jsonify(get_id) else: return abort(404)
def get_all_stats(): if request.method == 'GET': data = DataBase.admin_user_results() result = jsonify(data) result.status_code = 200 return result else: return abort(404)
# -*- coding: utf-8 -*- from mysql import DataBase import datetime USER_NAMES = { 'Fenng': '*****@*****.**', '刘彬': '*****@*****.**', '叫兽易小星': '*****@*****.**', '乐嘉': '*****@*****.**', '新浪NBA': '*****@*****.**', '想去': '*****@*****.**', 'windylcx': '*****@*****.**' } HOST, USER, PASSWD, DB, PORT = ('localhost', 'root', 'zenxds', 'weibo', 3306) db = DataBase(host=HOST, user=USER, passwd=PASSWD, db=DB, port=PORT) # for name, email in USER_NAMES.items(): # sql = "INSERT INTO user(nikename, email, password, last_login_time, create_time) VALUES (%s, %s, %s, %s, %s)" # params = (name, email, '242bf4a64db2a4007aba66761c8d0bd1', datetime.datetime.now(), datetime.datetime.now()) # r = db.execute(sql, params) # db.commit() USER_IDS = range(43, 50) sql = "SELECT * FROM weibo" r = db.execute(sql) # for i in USER_IDS: # for weibo in r: # weibo['author'] = i
from mysql import DataBase from redisConn import r as redis HOST, USER, PASSWD, DB, PORT = ('localhost', 'root', 'zenxds', 'weibo', 3306) db = DataBase(host=HOST, user=USER, passwd=PASSWD, db=DB, port=PORT) sql = "SELECT * FROM weibo WHERE type=0" weibos = db.execute(sql) def get_score(forward_count, comment_count, collect_count): forward_count = int(forward_count) collect_count = int(collect_count) collect_count = int(collect_count) return forward_count*2 + comment_count*1 + collect_count*2 for weibo in weibos: weibo_id = weibo['id'] key = "weibo:%s:forwardCount" % weibo_id; forward_count = redis.get(key) if redis.get(key) else 0 sql = "SELECT count(*) as count FROM comment WHERE weibo_id=%s" params = (weibo_id,) r = db.execute(sql, params, is_fetchone=True) comment_count = r['count'] sql = "SELECT count(*) as count FROM collect WHERE weibo_id=%s" params = (weibo_id,) r = db.execute(sql, params, is_fetchone=True)
def user_last_stat(id): data = DataBase.get_last_result(id) result = jsonify(data) return result
def show_test(id): data = DataBase.get_test(id) result = jsonify(data) result.status_code = 200 return result
def show_all_tests(): data = DataBase.all_tests() result = jsonify(data) result.status_code = 200 return result