def create_user(self, **json): # with (yield self.db.db()) as db: Rules.rule_usercreate(**json) account = json['account'] #账户查重 cnk_SQL = SQL_refector.user_get(account) cnk_res = yield self.db.query_one(cnk_SQL) if cnk_res: raise RuntimeError(errors.ACCOUNT_ALREADY_EXIST) account_hash = static.get_hash(account) table_num = str(static.get_table_num(account_hash)) json['uid'] = account_hash json['tbn'] = static.USER_TABLE_BASE_NAME+'_'+str(table_num) #头像处理 portrait_base64=json.get('portrait', None) if portrait_base64: path = os.path.join(static.config.PORTRAIT_PATH, table_num) filename = str(account_hash)+'.png' photopathname = utils.load_picture(path, filename, portrait_base64) photourl = utils.path_to_url(static.config.DATA_PATH, photopathname) json['photo_url'] = photourl #为birth添加默认值 if len(json.get('birth', '')) < 3: json['birth']='1900-01-01' json['age'] = '0' SQL = SQL_refector.user_insert(**json) effect_row = yield self.db.execute(SQL) nickname = json.get('nickname', None) uid = json.get('uid', None) account = json.get('account', None) raise gen.Return((uid, nickname, account))
def user_getwithid(userid): tablenum = static.get_table_num(userid) SQL = """SELECT {fields} FROM {tbn} WHERE uid = \"{uid}\"""".format( fields=','.join(SQL_refector.User_fields), tbn=config.USER_TABLE_BASE_NAME + '_' + str(tablenum), uid=userid) return SQL
def user_incr_follow_fan(uid, followamount=0, fanamount=0): tablenum = static.get_table_num(uid) tablename = config.USER_TABLE_BASE_NAME + '_' + str(tablenum) followamount = str(followamount) fanamount = str(fanamount) SQL = """UPDATE {tbn} SET follow=follow+{_follow}, fan=fan+{_fan} WHERE uid={_uid}""".format(_fan=fanamount, \ _follow=followamount, _uid=uid, tbn=tablename) return SQL
def user_get(account): """ :param account: :return: 查找account是否存在的SQL,根据account的hash值查找 """ # 先不考虑分表分库的问题,只在一个库里查找 account_hash = static.get_hash(account) tablenum = static.get_table_num(account_hash) SQL = """SELECT {fields} FROM {tbn} WHERE uid = \"{acth}\"""".format( fields=','.join(SQL_refector.User_fields), tbn=config.USER_TABLE_BASE_NAME + '_' + str(tablenum), acth=account_hash) return SQL
def user_update(userid, **kwargs): """ :param userid: 用户id :param kwargs: 要更新的字段 :return: """ SQL = """UPDATE {tbn} SET """ setvalue = [] for key in SQL_refector.User_fields: if kwargs.get(key, None) is None: continue setvalue.append(key + '="{' + key + '}"') tablenum = static.get_table_num(userid) tablename = config.USER_TABLE_BASE_NAME + '_' + str(tablenum) kwargs['tbn'] = tablename SQL = ( SQL + ','.join(setvalue) + """ WHERE uid={uid}""".format(uid=userid)).decode('utf8').format( **kwargs) return SQL