def api_admin_spot_unlink(): """Удаление кошелька, очистка спота""" base._api_access(request) hid = request.form['hid'] if not hid: abort(400) wallet = PaymentWallet.query.filter_by(hard_id=hid, user_id=0).first() if not wallet: abort(404) report = Report.query.filter_by(payment_id=wallet.payment_id).first() if report: abort(400) spot = Spot.query.filter_by(discodes_id=wallet.discodes_id).first() if not spot: abort(404) if not spot.clear(): abort(500) wallet.delete() return set_message('success', 'Success', 201)
def api_admin_spot_unlink(): """Удаление кошелька, очистка спота""" base._api_access(request) hid = request.form['hid'] if not hid: abort(400) wallet = PaymentWallet.query.filter_by( hard_id=hid, user_id=0).first() if not wallet: abort(404) report = Report.query.filter_by( payment_id=wallet.payment_id).first() if report: abort(400) spot = Spot.query.filter_by( discodes_id=wallet.discodes_id).first() if not spot: abort(404) if not spot.clear(): abort(500) wallet.delete() return set_message('success', 'Success', 201)
def api_admin_spot_hard_type(): """Возвращает информацию о типах спотов""" base._api_access(request) query = SpotHardType.query.order_by('sort asc') args = request.args if 'show' in args: query = query.filter_by(show=request.args.get('show')) if 'color_id' in args: query = query.filter_by(color_id=request.args.get('color_id')) if 'pattern_id' in args: query = query.filter_by(pattern_id=request.args.get('pattern_id')) if 'hard_id' in args: query = query.filter_by(hard_id=request.args.get('hard_id')) data = query.all() if not data: abort(404) info_xml = render_template( 'api/admin/hard_type_list.xml', data=data, count=len(data) ).encode('utf8') return make_response(info_xml)
def api_admin_get_info(hid=False, ean=False, code128=False): """Возвращает информацию о споте по его HID или EAN""" base._api_access(request) if not hid and not ean and not code128: abort(400) try: hid = int(hid) except: abort(405) wallet = None if hid: wallet = PaymentWallet.query.filter_by( hard_id=hid).first() if not wallet: abort(404) spot = Spot.query.filter_by( discodes_id=wallet.discodes_id).first() troika_info = None if ean or code128: if ean and not len(str(ean)) == 13: abort(400) if ean: spot = Spot.query.filter_by( barcode=ean).first() elif code128: spot = Spot.query.filter_by( code128=code128).first() if not spot: abort(404) wallet = PaymentWallet.query.filter_by( discodes_id=spot.discodes_id).first() troika_info = None if wallet: troika_info = troika_api.get_card_by_hard_id(wallet.hard_id) info_xml = render_template( 'api/admin/spot_info.xml', spot=spot, wallet=wallet, troika=troika_info, ).encode('cp1251') response = make_response(info_xml) return response
def api_admin_get_free(): """Возвращает информацию неактивированых спотах""" base._api_access(request) spot = Spot.query.filter_by(status=Spot.STATUS_GENERATED).all() if not spot: abort(404) info_xml = render_template( 'api/admin/spot_free.xml', spot=spot, ).encode('cp1251') response = make_response(info_xml) return response
def api_admin_get_info(hid=False, ean=False, code128=False): """Возвращает информацию о споте по его HID или EAN""" base._api_access(request) if not hid and not ean and not code128: abort(400) try: hid = int(hid) except: abort(405) wallet = None if hid: wallet = PaymentWallet.query.filter_by(hard_id=hid).first() if not wallet: abort(404) spot = Spot.query.filter_by(discodes_id=wallet.discodes_id).first() troika_info = None if ean or code128: if ean and not len(str(ean)) == 13: abort(400) if ean: spot = Spot.query.filter_by(barcode=ean).first() elif code128: spot = Spot.query.filter_by(code128=code128).first() if not spot: abort(404) wallet = PaymentWallet.query.filter_by( discodes_id=spot.discodes_id).first() troika_info = None if wallet: troika_info = troika_api.get_card_by_hard_id(wallet.hard_id) info_xml = render_template( 'api/admin/spot_info.xml', spot=spot, wallet=wallet, troika=troika_info, ).encode('cp1251') response = make_response(info_xml) return response
def api_admin_get_free(): """Возвращает информацию неактивированых спотах""" base._api_access(request) spot = Spot.query.filter_by( status=Spot.STATUS_GENERATED).all() if not spot: abort(404) info_xml = render_template( 'api/admin/spot_free.xml', spot=spot, ).encode('cp1251') response = make_response(info_xml) return response
def api_social_get_loyalties(): """Возвращает список акций""" base._api_access(request) count = base._get_request_count(request, PaymentLoyalty.DEFAULT_COUNT) offset = base._get_request_offset(request) query = PaymentLoyalty.query.order_by(PaymentLoyalty.id) loyalties = query.limit(count).offset(offset).all() info_xml = render_template('api/social/loyalties_list.xml', loyalties=loyalties, count=count, offset=offset).encode('utf8') return make_response(info_xml)
def api_social_get_loyalty(loyalty_id): """Возвращает детализацию по акции""" base._api_access(request) loyalty = PaymentLoyalty.query.get(loyalty_id) if not loyalty: abort(404) wallet_list = [] wallet_loyalty = WalletLoyalty.query.filter_by(loyalty_id=loyalty.id).all() for part in wallet_loyalty: if part.wallet_id in wallet_list: continue wallet_list.append(part.wallet_id) spot_ist = [] part_wallets = PaymentWallet.query.filter( PaymentWallet.id.in_(wallet_list)).all() for part_wallet in part_wallets: if part_wallet.discodes_id in spot_ist: continue spot_ist.append(part_wallet.discodes_id) spots = Spot.query.filter(Spot.discodes_id.in_(spot_ist)).all() spot_wallets = [] for spot in spots: for part_wallet in part_wallets: if part_wallet.discodes_id != spot.discodes_id: continue spot_wallets.append(dict( discodes_id=spot.discodes_id, barcode=spot.barcode, hard_id=part_wallet.hard_id )) info_xml = render_template( 'api/social/loyalty_info.xml', loyalty=loyalty, spots=spot_wallets ).encode('utf8') return make_response(info_xml)
def api_social_get_loyalty(loyalty_id): """Возвращает детализацию по акции""" base._api_access(request) loyalty = PaymentLoyalty.query.get(loyalty_id) if not loyalty: abort(404) wallet_list = [] wallet_loyalty = WalletLoyalty.query.filter_by(loyalty_id=loyalty.id).all() for part in wallet_loyalty: if part.wallet_id in wallet_list: continue wallet_list.append(part.wallet_id) spot_ist = [] part_wallets = PaymentWallet.query.filter( PaymentWallet.id.in_(wallet_list)).all() for part_wallet in part_wallets: if part_wallet.discodes_id in spot_ist: continue spot_ist.append(part_wallet.discodes_id) spots = Spot.query.filter(Spot.discodes_id.in_(spot_ist)).all() spot_wallets = [] for spot in spots: for part_wallet in part_wallets: if part_wallet.discodes_id != spot.discodes_id: continue spot_wallets.append( dict(discodes_id=spot.discodes_id, barcode=spot.barcode, hard_id=part_wallet.hard_id)) info_xml = render_template('api/social/loyalty_info.xml', loyalty=loyalty, spots=spot_wallets).encode('utf8') return make_response(info_xml)
def api_social_get_loyalties(): """Возвращает список акций""" base._api_access(request) count = base._get_request_count(request, PaymentLoyalty.DEFAULT_COUNT) offset = base._get_request_offset(request) query = PaymentLoyalty.query.order_by(PaymentLoyalty.id) loyalties = query.limit(count).offset(offset).all() info_xml = render_template( 'api/social/loyalties_list.xml', loyalties=loyalties, count=count, offset=offset ).encode('utf8') return make_response(info_xml)
def api_admin_spot_generate(): """Генерация спотов""" base._api_access(request) count = 10 if 'count' in request.form: try: count = int(request.form['count']) except: abort(405) count = 1 if count > Spot.MAX_GENERATE else count dis = SpotDis().get_new_list(count) if not dis: abort(405) result = {} for row in dis: if not row.set_generated(): continue spot = Spot.query.get(row.id) if spot: continue spot = Spot() spot.discodes_id = row.id if not spot.save(): if not row.set_init(): continue result[spot.code] = spot.code128 spot_list = render_template( 'api/admin/spot_list.xml', result=result, count=len(result) ).encode('cp1251') return spot_list
def api_socnet_list(ean): """Список соцсетей, подключенных к споту с правами записи""" base._api_access(request) ean = str(ean) if not len(ean) == 13 or not ean.isdigit(): abort(400) spot = Spot.query.filter_by(barcode=ean).first() if not spot: abort(404) list = spot.getBindedNets() info_xml = render_template('api/social/socnet_list.xml', spot=spot, list=list, count=len(list)).encode('utf8') return make_response(info_xml)
def api_admin_spot_generate(): """Генерация спотов""" base._api_access(request) count = 10 if 'count' in request.form: try: count = int(request.form['count']) except: abort(405) count = 1 if count > Spot.MAX_GENERATE else count dis = SpotDis().get_new_list(count) if not dis: abort(405) result = {} for row in dis: if not row.set_generated(): continue spot = Spot.query.get(row.id) if spot: continue spot = Spot() spot.discodes_id = row.id if not spot.save(): if not row.set_init(): continue result[spot.code] = spot.code128 spot_list = render_template('api/admin/spot_list.xml', result=result, count=len(result)).encode('cp1251') return spot_list
def api_admin_spot_delete(): """Удаление спотов""" base._api_access(request) code128 = request.form['code128'] if not code128: abort(400) spot = Spot.query.filter_by(code128=code128).first() if not spot or spot.user_id: abort(404) wallet = PaymentWallet.query.filter_by( discodes_id=spot.discodes_id).first() if wallet: abort(400) if not spot.clear(): abort(500) spot.delete() return set_message('success', 'Success', 201)
def api_socnet_list(ean): """Список соцсетей, подключенных к споту с правами записи""" base._api_access(request) ean = str(ean) if not len(ean) == 13 or not ean.isdigit(): abort(400) spot = Spot.query.filter_by(barcode=ean).first() if not spot: abort(404) list = spot.getBindedNets() info_xml = render_template( 'api/social/socnet_list.xml', spot=spot, list=list, count=len(list) ).encode('utf8') return make_response(info_xml)
def api_admin_spot_delete(): """Удаление спотов""" base._api_access(request) code128 = request.form['code128'] if not code128: abort(400) spot = Spot.query.filter_by( code128=code128).first() if not spot or spot.user_id: abort(404) wallet = PaymentWallet.query.filter_by( discodes_id=spot.discodes_id).first() if wallet: abort(400) if not spot.clear(): abort(500) spot.delete() return set_message('success', 'Success', 201)
def api_admin_spot_hard_type(): """Возвращает информацию о типах спотов""" base._api_access(request) query = SpotHardType.query.order_by('sort asc') args = request.args if 'show' in args: query = query.filter_by(show=request.args.get('show')) if 'color_id' in args: query = query.filter_by(color_id=request.args.get('color_id')) if 'pattern_id' in args: query = query.filter_by(pattern_id=request.args.get('pattern_id')) if 'hard_id' in args: query = query.filter_by(hard_id=request.args.get('hard_id')) data = query.all() if not data: abort(404) info_xml = render_template('api/admin/hard_type_list.xml', data=data, count=len(data)).encode('utf8') return make_response(info_xml)
def api_admin_spot_model(): """Возвращает информацию о доступных для спота корпусах""" base._api_access(request) return api_admin_hard_list(SpotHard, request)
def api_admin_spot_color(): """Возвращает информацию о доступных для спота цветах""" base._api_access(request) return api_admin_hard_list(SpotColor, request)
def api_admin_spot_pattern(): """Возвращает информацию о доступных для спота шаблонах""" base._api_access(request) return api_admin_hard_list(SpotPattern, request)
def api_social_post(ean, soc_id): """Публикует пост с картинкой в заданной в soc_id соцсети""" base._api_access(request) success = 0 ean = str(ean) if not len(ean) == 13: abort(400) if not 'img' in request.files: abort(400) file = request.files['img'] spot = Spot.query.filter_by(barcode=ean).first() if not spot: abort(404) message = '' if 'text' in request.form: message = request.form['text'] filesize = 0 if file: file.seek(0, os.SEEK_END) filesize = file.tell() file.seek(0, os.SEEK_SET) filepath = False token = False error = 'Unknown error' img = '' if not file or '.' not in file.filename: error = 'Incorrect file' elif file.filename.rsplit('.', 1)[1] not in app.config['IMG_EXTENSIONS']: error = 'usupported file extension' elif filesize > app.config['MAX_IMG_LENGTH']: error = 'img too large' else: base_name = secure_filename(file.filename) img_name = base_name filepath = "%s/%s/%s" % (os.getcwd(), app.config['IMG_FOLDER'], img_name) i = 0 while (os.path.exists(filepath)): i += 1 img_name = "%s_%s" % (str(i), base_name) filepath = "%s/%s/%s" % (os.getcwd(), app.config['IMG_FOLDER'], img_name) file.save(filepath) img = "http://%s/%s/%s" % ( request.host, 'upload/img', img_name) img = img.replace('/././', '/') error = 'no write rights fo this social account' token = SocToken.query.filter_by( user_id=spot.user_id, type=soc_id, write_access=1).first() if token and img and filepath: if SocnetsApi.post_photo(token, token.id, filepath, message): success = 1 error = '' else: error = 'filed when uploading img to socnet' info_xml = render_template( 'api/social/spot_post.xml', spot=spot, error=error, img=img, success=success ).encode('utf8') return make_response(info_xml)
def api_social_post(ean, soc_id): """Публикует пост с картинкой в заданной в soc_id соцсети""" base._api_access(request) success = 0 ean = str(ean) if not len(ean) == 13: abort(400) if not 'img' in request.files: abort(400) file = request.files['img'] spot = Spot.query.filter_by(barcode=ean).first() if not spot: abort(404) message = '' if 'text' in request.form: message = request.form['text'] filesize = 0 if file: file.seek(0, os.SEEK_END) filesize = file.tell() file.seek(0, os.SEEK_SET) filepath = False token = False error = 'Unknown error' img = '' if not file or '.' not in file.filename: error = 'Incorrect file' elif file.filename.rsplit('.', 1)[1] not in app.config['IMG_EXTENSIONS']: error = 'usupported file extension' elif filesize > app.config['MAX_IMG_LENGTH']: error = 'img too large' else: base_name = secure_filename(file.filename) img_name = base_name filepath = "%s/%s/%s" % (os.getcwd(), app.config['IMG_FOLDER'], img_name) i = 0 while (os.path.exists(filepath)): i += 1 img_name = "%s_%s" % (str(i), base_name) filepath = "%s/%s/%s" % (os.getcwd(), app.config['IMG_FOLDER'], img_name) file.save(filepath) img = "http://%s/%s/%s" % (request.host, 'upload/img', img_name) img = img.replace('/././', '/') error = 'no write rights fo this social account' token = SocToken.query.filter_by(user_id=spot.user_id, type=soc_id, write_access=1).first() if token and img and filepath: if SocnetsApi.post_photo(token, token.id, filepath, message): success = 1 error = '' else: error = 'filed when uploading img to socnet' info_xml = render_template('api/social/spot_post.xml', spot=spot, error=error, img=img, success=success).encode('utf8') return make_response(info_xml)
def api_social_spot_loyalty(ean): """Возвращает акции, в которых участвует спот по EAN""" base._api_access(request) ean = str(ean) if not len(ean) == 13 or not ean.isdigit(): abort(400) spot = Spot.query.filter_by(barcode=ean).first() if not spot: abort(404) wallet = PaymentWallet.query.filter_by( discodes_id=spot.discodes_id).first() if not wallet: abort(404) count = base._get_request_count(request, PaymentLoyalty.DEFAULT_COUNT) offset = base._get_request_offset(request) if 'id' in request.args: # данные только по требуемой акции try: loyalty_id = int(request.args['id']) except: abort(405) loyalty = PaymentLoyalty.query.get(loyalty_id) if not loyalty: abort(404) wallet_loyalty = WalletLoyalty.query.filter_by( loyalty_id=loyalty.id, wallet_id=wallet.id).all() if not wallet_loyalty: abort(404) if not wallet_loyalty[0].checked: abort(404) loyalties = [loyalty] else: # по всем акциям спота wallet_loyalty = WalletLoyalty.query.filter_by( wallet_id=wallet.id).filter_by(checked=1).all() if not wallet_loyalty: abort(404) loyaltyList = [] for part in wallet_loyalty: if part.loyalty_id in loyaltyList: continue loyaltyList.append(part.loyalty_id) query = PaymentLoyalty.query.filter(PaymentLoyalty.id.in_(loyaltyList)) loyalties = query.limit(count).offset(offset).all() info_xml = render_template( 'api/social/spot_loyalty.xml', spot=spot, loyalties=loyalties, count=count, offset=offset ).encode('utf8') return make_response(info_xml)
def api_admin_linking_spot(): """Добавляем спот и связанный с ним кошелёк""" base._api_access(request) add_success = 0 hid = request.form['hid'] pids = request.form['pids'] ean = request.form['ean'] hard_type = Spot.DEFAULT_HARD_TYPE if 'hard_type' in request.form: hard_type = int(request.form['hard_type']) if not SpotHardType.query.get(hard_type): abort(400) status = 1 if 'status' in request.form: status = int(request.form['status']) if not hid or not ean or not pids: abort(400) if not len(str(ean)) == 13: abort(400) try: hid = int(hid) pids = int(pids) except Exception: abort(405) spot = Spot.query.filter_by(barcode=ean).first() if not spot: abort(404) spot.hard_type = hard_type troika_info = troika_api.release_card(hid) if troika_info: spot.user_id = troika_info['user_id'] spot_troika = SpotTroika() spot_troika.discodes_id = spot.discodes_id spot_troika.save() wallet = PaymentWallet.query.filter((PaymentWallet.hard_id == hid) | ( PaymentWallet.discodes_id == spot.discodes_id)).first() if not wallet: wallet = PaymentWallet() wallet.payment_id = wallet.get_pid(pids) wallet.hard_id = hid wallet.discodes_id = spot.discodes_id if status == 0: wallet.type = PaymentWallet.TYPE_DEMO spot.spot_type_id = Spot.TYPE_DEMO if troika_info: wallet.user_id = troika_info['user_id'] wallet.status = PaymentWallet.STATUS_ACTIVE if wallet.save(): spot.status = Spot.STATUS_ACTIVATED if troika_info: spot.status = Spot.STATUS_REGISTERED if not spot.save(): abort(400) add_success = 1 if wallet.discodes_id != spot.discodes_id: abort(400) add_xml = render_template( 'api/admin/add_info.xml', spot=spot, wallet=wallet, add_success=add_success, ).encode('cp1251') response = make_response(add_xml) return response
def api_admin_linking_spot(): """Добавляем спот и связанный с ним кошелёк""" base._api_access(request) add_success = 0 hid = request.form['hid'] pids = request.form['pids'] ean = request.form['ean'] hard_type = Spot.DEFAULT_HARD_TYPE if 'hard_type' in request.form: hard_type = int(request.form['hard_type']) if not SpotHardType.query.get(hard_type): abort(400) status = 1 if 'status' in request.form: status = int(request.form['status']) if not hid or not ean or not pids: abort(400) if not len(str(ean)) == 13: abort(400) try: hid = int(hid) pids = int(pids) except Exception: abort(405) spot = Spot.query.filter_by( barcode=ean).first() if not spot: abort(404) spot.hard_type = hard_type troika_info = troika_api.release_card(hid) if troika_info: spot.user_id = troika_info['user_id'] spot_troika = SpotTroika() spot_troika.discodes_id = spot.discodes_id spot_troika.save() wallet = PaymentWallet.query.filter( (PaymentWallet.hard_id == hid) | (PaymentWallet.discodes_id == spot.discodes_id)).first() if not wallet: wallet = PaymentWallet() wallet.payment_id = wallet.get_pid(pids) wallet.hard_id = hid wallet.discodes_id = spot.discodes_id if status == 0: wallet.type = PaymentWallet.TYPE_DEMO spot.spot_type_id = Spot.TYPE_DEMO if troika_info: wallet.user_id = troika_info['user_id'] wallet.status = PaymentWallet.STATUS_ACTIVE if wallet.save(): spot.status = Spot.STATUS_ACTIVATED if troika_info: spot.status = Spot.STATUS_REGISTERED if not spot.save(): abort(400) add_success = 1 if wallet.discodes_id != spot.discodes_id: abort(400) add_xml = render_template( 'api/admin/add_info.xml', spot=spot, wallet=wallet, add_success=add_success, ).encode('cp1251') response = make_response(add_xml) return response
def api_social_spot_loyalty(ean): """Возвращает акции, в которых участвует спот по EAN""" base._api_access(request) ean = str(ean) if not len(ean) == 13 or not ean.isdigit(): abort(400) spot = Spot.query.filter_by(barcode=ean).first() if not spot: abort(404) wallet = PaymentWallet.query.filter_by( discodes_id=spot.discodes_id).first() if not wallet: abort(404) count = base._get_request_count(request, PaymentLoyalty.DEFAULT_COUNT) offset = base._get_request_offset(request) if 'id' in request.args: # данные только по требуемой акции try: loyalty_id = int(request.args['id']) except: abort(405) loyalty = PaymentLoyalty.query.get(loyalty_id) if not loyalty: abort(404) wallet_loyalty = WalletLoyalty.query.filter_by( loyalty_id=loyalty.id, wallet_id=wallet.id).all() if not wallet_loyalty: abort(404) if not wallet_loyalty[0].checked: abort(404) loyalties = [loyalty] else: # по всем акциям спота wallet_loyalty = WalletLoyalty.query.filter_by( wallet_id=wallet.id).filter_by(checked=1).all() if not wallet_loyalty: abort(404) loyaltyList = [] for part in wallet_loyalty: if part.loyalty_id in loyaltyList: continue loyaltyList.append(part.loyalty_id) query = PaymentLoyalty.query.filter(PaymentLoyalty.id.in_(loyaltyList)) loyalties = query.limit(count).offset(offset).all() info_xml = render_template('api/social/spot_loyalty.xml', spot=spot, loyalties=loyalties, count=count, offset=offset).encode('utf8') return make_response(info_xml)