def create_online(): online = Online() online.title = '光棍节找妹子啦' static = Statistic() with db.auto_commit(): db.session.add(online) static.f_online_id = online.id with db.auto_commit(): db.session.add(static) return 'success', 201
def downloads_plus_by_online(**kwargs): oid = kwargs['oid'] mobile_race = kwargs['mobile_race'] params = {'f_online_id': oid} sqls = { MobileRaceEnum.android: { Statistic.android_downloads: Statistic.android_downloads + 1 }, MobileRaceEnum.other: { Statistic.other_downloads: Statistic.other_downloads + 1 }, MobileRaceEnum.ipad: { Statistic.ipad_downloads: Statistic.ipad_downloads + 1 }, MobileRaceEnum.iphone: { Statistic.iphone_downloads: Statistic.iphone_downloads + 1 }, } sql = sqls.get(mobile_race) with db.auto_commit(): count = Statistic.query.filter_by(**params)\ .update(sql) return count
def register_by_mobile(mobile, password): user = UserOrg() user.password = password user.mobile = mobile with db.auto_commit(): db.session.add(user) return user
def create_order(self, params_list): """ 创建有兑吧生成的订单(记录兑吧的订单数据)并扣除分数 :param params_list: 一组dict类型的数据 :return: """ # 将兑吧的App_Secret加入到校验字符串中 params_list['appSecret'] = current_app.config['DUIBA_APP_SECRET'] sign = params_list['sign'] # 去除字典中的sign,因为sign本身不属于被签名部分 del(params_list['sign']) valid = self.md5_check(params_list, sign) # 校验完后再加入,并存入到数据库中 params_list['sign'] = sign params_list['bizId'] = make_an_bizid() self.__adapter(params_list) if valid: # 先扣除积分再生成订单,如果某一步操作错误,需要回滚数据 with db.auto_commit(): success, left_credit = self.__deduct_credit(int(params_list['uid']), int(params_list['credits'])) if success: self.__add_one_order(params_list) # 注意下面的params_list['credits']需要取负数表示扣除 self.__add_a_credit_dynamic(params_list['uid'], -int(params_list['credits']), params_list['description'], '兑吧', left_credit) return success, left_credit, params_list['bizId'] else: return None
def __update_order(self, success, app_key, order_num, error_msg, timestamp): """更新订单状态""" order = OrderDuiBa.query.\ filter_by(appKey=app_key, orderNum=order_num).first() if order is None: return "strange order_number which we don't recognize" # we must confirm the order's status never be changed if order.success == 0: if success == 'true': # if success, mark the order status success order.success = 1 order.confirm_timestamp = timestamp db.session.commit() return 'ok' if success == 'false': # if failed, mark the order status failed # the whole process, must be in a transaction with db.auto_commit(): order.success = -1 order.error_message = error_msg order.confirm_timestamp = timestamp left_credit = self.__rollback_credit(order.uid, order.credits) self.__add_a_credit_dynamic(order.uid, order.credits, '兑吧出错,回滚积分', 'system', left_credit) return 'ok' return "f**k, no 'true' no 'false', what's this?" # if order.success != 0 means this order has been updated, # should never be updated again return "i have been updated the order's status, leave me in peace"
def reset_password_by_mobile(mobile, password): user = UserOrg.query.filter_by(mobile=mobile).first() if user is not None: with db.auto_commit(): user.update({UserOrg.password: password}) else: raise NotFound(error='user not found', error_code=2000) return user
def register_by_email(username, email, password): user = User(username=username, email=email) user.password = password user.role = User.ROLE_ACTIVE with db.auto_commit(): db.session.add(user) return user
def pv_added_1(oid): sql = {Statistic.pv: Statistic.pv + 1} with db.auto_commit(): count = Statistic.query.filter_by(f_online_id=oid)\ .update(sql) return count