def fuzzy_get_agent_name(): dbg('fuzzy_get_agent_name') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() name = data['name'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) role_bit = int(current_user.role[5]) if role_bit != 2: raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) dbsession = dbapi.db.session Agent = dbapi.Agent agents = dbsession.query(Agent).filter(Agent.name.like("%%%s%%" % name)).\ filter(Agent.level==4).limit(5) data = [] for agent in agents: info = {'name': agent.name, 'id': agent.id} data.append(info) reply['data'] = {'names': data} return make_response(jsonify(reply), status_code)
def get_wallet_balance(): dbg('get_wallet_balance') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) user_id = g.oauth_user.user_id wallet = dbapi.get_wallet(role=0, user_id=user_id, agent_id=device.owner_agent_id) agent_setting = dbapi.get_agent_setting(agent_id=device.agent_id) wallet_pay_enable = 1 if agent_setting and agent_setting.wallet_pay_enable == 0: wallet_pay_enable = 0 reply['data'] = { 'balance': wallet.balance, 'wallet_pay_enable': wallet_pay_enable } return make_response(jsonify(reply), status_code)
def god_recycle_device(): dbg('god_recycle_device') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('update devices error', error.ERROR_PARAM) god = dbapi.get_god(openluat_user_id=current_user.id) if not god: raise ApiError('ERROR_GOD_NOT_FOUND', error.ERROR_GOD_NOT_FOUND) role_bit = int(current_user.role[5]) if role_bit not in (1, 2): raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if role_bit == 1: god_agents = dbapi.get_god_agent(god_id=god.id) g = (god_agent.agent_id for god_agent in god_agents) if device.agent_id not in g: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) update = {} update['agent_id'] = 0 update['address_id'] = 0 update['salesman_agent_id'] = 0 update['l4'] = 1 update['l3'] = 0 update['l2'] = 0 update['l1'] = 0 device = dbapi.update_device(device, **update) # device_distribution dbapi.delete_device_distribution_total(imei) # device_distribution_salesman dbapi.delete_device_distribution_salesman(imei) # device_product dbapi.delete_device_product(device.id) # pay dbapi.delete_pays_and_records(imei) try: db.session.commit() except: db.session.rollback() raise return make_response(jsonify(reply), status_code)
def register(): dbg('register') reply, status_code = {'ret': 0, 'msg': ''}, 200 if request.method == 'POST': try: data = request.get_json() username = data['username'] phone = data['phone'] password = data['password'] code = data['code'] dbg(data) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) data = auth_core.register(**data) if data: if isinstance(data, dict): reply['data'] = data else: return data return make_response(jsonify(reply), status_code)
def get_advertiser_info(): dbg('get_advertiser_info') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() advertiser_id = data['advertiser_id'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) advertiser = dbapi.get_advertiser(id=advertiser_id) if not advertiser: raise ApiError('ERROR_ADVERTISER_NOT_FOUND', error.ERROR_ADVERTISER_NOT_FOUND) reply['data'] = { 'id': advertiser.id, 'name': advertiser.name, 'phone': advertiser.phone, 'email': advertiser.email, 'desc': advertiser.desc, 'address': advertiser.address, 'remark': advertiser.remark } return make_response(jsonify(reply), status_code)
def add_advertiser(): dbg('add_advertiser') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() name = data['name'] email = data['email'] phone = data['phone'] address = data['address'] desc = data['desc'] remark = data['remark'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) target_advertiser = dbapi.get_advertiser(phone=phone) if target_advertiser: raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS) agent = dbapi.get_agent(phone=phone) if agent: raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS) openluat_user = dbapi.get_openluat_user(phone=phone) if not openluat_user: openluat_user = dbapi.make_new_openluat_user(name, email, phone) db.session.commit() agent_id = current_user.agent_id hook_agent_id = agent_id new_agent = dbapi.make_new_advertiser(openluat_user.id, hook_agent_id, name, phone, email, desc, address, remark) db.session.commit() return make_response(jsonify(reply), status_code)
def get_nopay_record(): dbg('get_nopay_record') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] page = request.args.get('page', 1, type=int) psize = request.args.get('psize', 10, type=int) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) data = [] count, records = dbapi.get_record_no_pay(imei=imei, page=page, psize=psize) if count: for record in records: info = { 'stime': int(record.stime.timestamp()), 'etime': int(record.etime.timestamp()), 'imei': record.imei } data.append(info) reply['data'] = {'count': count, 'records': data} return make_response(jsonify(reply), status_code)
def using_ads(): dbg('using_ads') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() ad_list = data['ad_list'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) agent_id = current_user.agent_id ads = dbapi.get_ad(agent_id=agent_id, using=1) for ad in ads: ad = dbapi.update_ad(ad, using=0) for ad_id in ad_list: ad = dbapi.get_ad(id=ad_id) if not ad: dbg(ad_id) raise ApiError('ERROR_AD_NOT_FOUND', error.ERROR_AD_NOT_FOUND) ad = dbapi.update_ad(ad, using=1) db.session.commit() return make_response(jsonify(reply), status_code)
def get_agent_sub_advertiser(): dbg('get_agent_sub_advertiser') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() agent_id = data['agent_id'] page = request.args.get('page', 1, type=int) psize = request.args.get('psize', 10, type=int) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) count, advertisers = dbapi.get_advertiser(hook_agent_id=agent_id, page=page, psize=psize) data = [] if count: for advertiser in advertisers: info = { 'id': advertiser.id, 'name': advertiser.name, 'phone': advertiser.phone, 'email': advertiser.email, 'desc': advertiser.desc, 'address': advertiser.address, 'remark': advertiser.remark } data.append(info) reply['data'] = { 'count': count, 'advertisers': data } return make_response(jsonify(reply), status_code)
def update_product(): dbg('update_product') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() product_id = data['product_id'] update = data['update'] keys = ('title', 'body', 'value', 'price') for k in update: assert (k in keys) if 'value' in update: assert (isinstance(update['value'], int) and update['value'] > 0) if 'price' in update: assert (isinstance(update['price'], int) and update['price'] > 0) except: print_exception_info() raise ApiError('update product error', error.ERROR_PARAM) product = dbapi.get_product(product_id=product_id) if not product: raise ApiError('update product', error.ERROR_PRODUCT_NOT_FOUND) product = dbapi.update_product(product, **update) db.session.add(product) db.session.commit() return make_response(jsonify(reply), status_code)
def get_nopay_count(): dbg('get_nopay_count') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) if not device.nopay: raise ApiError('ERROR_DEVICE_NOT_SUP_NOPAY', error.ERROR_DEVICE_NOT_SUP_NOPAY) user_id = g.oauth_user.user_id user_start_counter = dbapi.get_user_start_counter(user_id=user_id, imei=imei) if not user_start_counter: user_start_counter = dbapi.make_new_user_start_counter(user_id, imei) db.session.commit() user_start_counters = dbapi.get_user_start_counter( user_id=user_id, address_id=device.address_id) count = 0 for user_start_counter in user_start_counters: count += user_start_counter.count reply['data'] = {'count': count, 'nopay': device.nopay} return make_response(jsonify(reply), status_code)
def get_ads(): dbg('get_ads') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) ads = dbapi.get_ad(agent_id=device.owner_agent_id, using=1) data = [] for ad in ads: info = { 'id': ad.id, 'name': ad.name, 'desc': ad.desc, 'img': url_for('static', filename=ad.img), 'url': ad.url } data.append(info) reply['data'] = data return make_response(jsonify(reply), status_code)
def get_pay(): dbg('get_pay') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() pay_id = data['pay_id'] except: print_exception_info() raise ApiError('get pay error', error.ERROR_PARAM) pay = dbapi.get_pay(pay_id=pay_id) if not pay: raise ApiError('get pay error', error.ERROR_PAY_NOT_FOUND) data = { 'trade_no': pay.trade_no, 'title': pay.title, 'body': pay.body, 'total_fee': pay.total_fee, 'time': int(pay.utime.timestamp()), 'status': pay.status } reply['data'] = data return make_response(jsonify(reply), status_code)
def get_wallet_products(): dbg('get_wallet_products') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('get products error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) agent_id = device.owner_agent_id products = dbapi.get_product(agent_id=agent_id, cat=99) data = [] if products: for product in products: dic = { 'id': product.id, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, } data.append(dic) reply['data'] = data return make_response(jsonify(reply), status_code)
def get_products(): dbg('get_products') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('get products error', error.ERROR_PARAM) device = dbapi.get_device(imei=imei) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) device_products = dbapi.get_device_product(device_id=device.id) data = [] if device_products: for device_product in device_products: product = dbapi.get_product(product_id=device_product.product_id) if not product: continue dic = { 'id': product.id, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, 'unit': device.product_unit, 'number': str(Fraction(product.value, device.product_unit_pluse)), } data.append(dic) reply['data'] = data return make_response(jsonify(reply), status_code)
def _get_common(self, query, **kwargs): # 筛选 query = self._model_filter(query, **kwargs) # 排序 if 'sorter' in kwargs: # 包含排序字段和排序方式 # ?sorter=-ctime,sort sorter = kwargs['sorter'] try: attrs = sorter.split(',') or [] for attr in attrs: if attr[0] == '-': query = query.order_by( getattr(self.Model, attr[1:]).desc()) else: query = query.order_by(getattr(self.Model, attr)) except: dbg(sorter) print_exception_info() # 单条数据查询 obj = self._model_one_query(query, **kwargs) if obj != 'not one query': return obj # 是否分页 return self._query_by_page_common(query, **kwargs)
def get_device_status(): reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) status = dbapi.get_cws_device_status(imei) online = -1 if status: online = status.get('online', -1) latest_report = dbapi.get_device_latestreport(imei) if not latest_report: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) # dbg((datetime.now().timestamp(), latest_report['time'].timestamp())) if datetime.now().timestamp() - latest_report['time'].timestamp() > 150: if online == 1: online = 0 alipay_available = 0 if check_alipay_available(imei): alipay_available = 1 reply['data'] = { 'status': online, # 0离线 1在线 4关机 'alipay_available': alipay_available, 'user_id': g.oauth_user.user_id } return make_response(jsonify(reply), status_code)
def update_agent(): dbg('update_agent') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() agent_id = data['agent_id'] update = data['update'] keys = ('name', 'desc', 'address', 'remark', 'expandable', 'withdrawable') for k in update: assert (k in keys) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) agent = dbapi.get_agent(id=agent_id) if not agent: raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND) cur_agent_id = current_user.agent_id print(agent_id, cur_agent_id, agent.hook_agent_id) if cur_agent_id not in (agent_id, agent.hook_agent_id): raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) agent = dbapi.update_agent(agent, **update) db.session.commit() return make_response(jsonify(reply), status_code)
def get_wechat_withdraw(): dbg('get_wechat_withdraw') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: page = int(request.args.get('page', 1)) psize = int(request.args.get('psize', 10)) except: print_exception_info() raise ApiError('get_wechat_withdraw error', error.ERROR_PARAM) user_id = current_user.id count, res = dbapi.get_pay_to_user(user_id=user_id, page=page, psize=psize) data = [] if count and res: for pay_to_user in res: info = { 'time': int(pay_to_user.ctime.timestamp()), 'to_nickname': pay_to_user.to_nickname, 'fee' : pay_to_user.total_fee, 'remark': pay_to_user.remark } data.append(info) reply['data'] = { 'count': count, 'pays': data } return make_response(jsonify(reply), status_code)
def get_machine_record(): dbg('get_machine_record') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] except: print_exception_info() raise ApiError('get machine record error', error.ERROR_PARAM) record = dbapi.get_record(imei=imei, recent=1) if not record: raise ApiError('ERROR_RECORD_NOT_FOUND', error.ERROR_RECORD_NOT_FOUND) now = int(time.time()) etime = int(record.etime.timestamp()) if record.status == 1: etime = now reply['data'] = { 'id': record.id, 'stime': int(record.stime.timestamp()), 'etime': etime, 'now': now, 'user_id': record.user_id, } return make_response(jsonify(reply), status_code)
def get_ads(): dbg('get_ads') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data.get('imei') except: print_exception_info() raise ApiError('get products error', error.ERROR_PARAM) if imei: ads = dbapi.get_ad(imei=imei) else: agent_id = current_user.agent_id ads = dbapi.get_ad(agent_id=agent_id) data = [] for ad in ads: info = { 'id': ad.id, 'name': ad.name, 'desc': ad.desc, 'img': url_for('static', filename=ad.img), 'url': ad.url, 'using': ad.using } data.append(info) reply['data'] = data return make_response(jsonify(reply), status_code)
def login(): dbg('login') reply, status_code = {'ret': 0, 'msg': ''}, 200 if request.method == 'POST': try: data = request.get_json() username = data['username'] password = data['password'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) remember_me = 0 try: remember_me = data['remember_me'] except: pass data = auth_core.web_login(username, password, remember_me) if data: if isinstance(data, dict): reply['data'] = data else: return data return make_response(jsonify(reply), status_code)
def search_unknow_device(): dbg('search_unknow_device') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data.get('imei') device_id = data.get('device_id') dbg(data) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) role_bit = int(current_user.role[5]) if role_bit != 2: raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) device = None dbsession = dbapi.db.session Device = dbapi.Device query = dbsession.query(Device).filter(Device.agent_id == 0) if imei: device = query.filter(Device.imei == imei).first() elif device_id: device = query.filter(Device.id == int(device_id)).first() if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) reply['data'] = {'id': device.id, 'imei': device.imei} return make_response(jsonify(reply), status_code)
def get_wallet_receipt(): dbg('get_wallet_receipt') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: page = int(request.args.get('page', 1)) psize = int(request.args.get('psize', 10)) except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) user_id = current_user.id count, wallet_receipts = dbapi.get_wallet_receipt(user_id=user_id, agent_id=current_user.agent_id, no_zero=1, page=page, psize=psize) data = [] if count and wallet_receipts: for wallet_receipt in wallet_receipts: info = { 'time': int(wallet_receipt.ctime.timestamp()), 'receipt': int(wallet_receipt.receipt), 'withdrawable_receipt' : int(wallet_receipt.withdrawable_receipt), 'trade_type': wallet_receipt.trade_type, 'remark': wallet_receipt.remark } data.append(info) reply['data'] = { 'count': count, 'wallet_receipts': data } return make_response(jsonify(reply), status_code)
def machine_restart(): dbg('machine_restart') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() record_id = data['record_id'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) record = dbapi.get_record(record_id=record_id) if not record: dbg('record_id: %d' % record_id) raise ApiError('ERROR_RECORD_NOT_FOUND', error.ERROR_RECORD_NOT_FOUND) # pay = dbapi.get_pay(pay_id=record.pay_id) # imei = pay.imei # delta = record.etime - datetime.now() # value = delta.seconds # if value > 0: # succ, result = launch_relay_signal_deivce_v2(imei, value) reply['data'] = { 'id': record.id, 'stime': int(record.stime.timestamp()), 'etime': int(record.etime.timestamp()), 'now': int(time.time()) } return make_response(jsonify(reply), status_code)
def get_agent_setting(): dbg('get_agent_setting') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() target_agent_id = data['target_agent_id'] except: print_exception_info() raise ApiError('update agent setting error', error.ERROR_PARAM) agent_id = current_user.agent_id target_agent = dbapi.get_agent(id=target_agent_id) if not target_agent: raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND) agent_setting = dbapi.get_agent_setting(agent_id=target_agent_id) reply['data'] = { 'min_withdraw': agent_setting.min_withdraw, 'withdraw_fee': agent_setting.withdraw_fee, 'wallet_pay_enable': agent_setting.wallet_pay_enable, 'trans_url': agent_setting.trans_url } return make_response(jsonify(reply), status_code)
def get_agent_info(): dbg('get_agent_info') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() agent_id = data['agent_id'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) agent = dbapi.get_agent(id=agent_id) if not agent: raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND) reply['data'] = { 'id': agent.id, 'salesman': agent.salesman, 'level': agent.level, 'slevel': agent.slevel, 'name': agent.name, 'phone': agent.phone, 'email': agent.email, 'desc': agent.desc, 'address': agent.address, 'remark': agent.remark, 'expandable': agent.expandable, 'withdrawable': agent.withdrawable } return make_response(jsonify(reply), status_code)
def daily_income(): dbg('daily_income') reply ,status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() start = date.fromtimestamp(int(data['start'])) end = date.fromtimestamp(int(data['end'])) freq = int(data['freq']) except: print_exception_info() raise ApiError('daily income error', error.ERROR_PARAM) agent_id = current_user.agent_id daily_incomes = dbapi.get_daily_income(start=start, end=end, agent_id=agent_id) cur = start d_format = '%Y-%m-%d' if freq == 2: d_format = '%Y-%m' data = [] if freq == 1: # 每天 while cur <= end: info = { 'date': cur.strftime(d_format), 'income': 0 } for i, daily_income in enumerate(daily_incomes): if daily_income.date == cur: info['income'] = daily_income.online_income + daily_income.offline_income daily_incomes.pop(i) break data.append(info) cur = cur + timedelta(days=1) if freq == 2: # 每月 sum_income = 0 while cur <= end: firstday_weekday, month_range = calendar.monthrange(cur.year, cur.month) for i, daily_income in enumerate(daily_incomes): if daily_income.date.month == cur.month and daily_income.date.year == cur.year: sum_income += daily_income.online_income + daily_income.offline_income daily_incomes.pop(i) if cur.day == month_range or len(daily_incomes)==0: break if cur.day==month_range or cur == end: info = { 'date': cur.strftime(d_format), 'income': sum_income } sum_income = 0 data.append(info) cur = cur + timedelta(days=1) reply['data'] = data return make_response(jsonify(reply), status_code)
def add_device(): dbg('add_device') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() imei = data['imei'] cat = data['cat'] except: print_exception_info() raise ApiError('ERROR_PARAM', error.ERROR_PARAM) if len(imei) != 15: raise ApiError('ERROR_INVALIDATE_IMEI', error.ERROR_INVALIDATE_IMEI) agent_id = current_user.agent_id agent = dbapi.get_agent(id=agent_id) if not agent or agent.level != 4: raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION) device = dbapi.get_device(imei=imei) if device: if device.agent_id != 0: raise ApiError('ERROR_DEVICE_EXISTS', error.ERROR_DEVICE_EXISTS) else: # update device device.agent_id = agent_id device.owner_agent_id = agent_id device.cat = cat else: address_id = 0 device = dbapi.make_new_device(imei, cat, agent_id, address_id) try: db.session.commit() except: db.session.rollback() raise if not device: raise Exception('add device db error') data = {} if device: # 添加第一次设备分销 from_agent, to_agent = 0, agent_id dbapi.make_new_device_distribution(device.id, device.imei, from_agent, to_agent) data = {'id': device.id, 'imei': device.imei, 'cat': device.cat} try: db.session.commit() except: db.session.rollback() raise reply['data'] = data return make_response(jsonify(reply), status_code)
def get_products(): dbg('get_products') reply, status_code = {'code': 0, 'msg': ''}, 200 try: data = request.get_json() # cat 和 device_id 二选一 cat = data.get('cat') device_id = data.get('device_id') except: print_exception_info() raise ApiError('get products error', error.ERROR_PARAM) data = [] if cat is not None: agent_id = current_user.agent_id products = dbapi.get_product(agent_id=agent_id, cat=cat) if products: for product in products: info = { 'id': product.id, 'cat': product.cat, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, 'inventory': product.inventory } data.append(info) if device_id is not None: device = dbapi.get_device(device_id=device_id) if not device: raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND) device_products = dbapi.get_device_product(device_id=device_id) if device_products: for device_product in device_products: product = dbapi.get_product( product_id=device_product.product_id) info = { 'id': product.id, 'cat': product.cat, 'title': product.title, 'body': product.body, 'value': product.value, 'price': product.price, 'inventory': product.inventory, 'unit': device.product_unit } data.append(info) reply['data'] = data return make_response(jsonify(reply), status_code)