promotion/handler/location.py
2019-10-09 19:34:12 +08:00

165 lines
5.1 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from flask import Flask, jsonify
from flask_restful import reqparse, abort, Api, Resource
import logging
from mysql.mmysql import MysqlBase
from prod_config import mysql_promotion_config
from ops.reflush_ad import rebuild_ad
import pdb
log = logging.getLogger(__name__)
parser = reqparse.RequestParser()
parser.add_argument('id')
parser.add_argument('area')
parser.add_argument('x')
parser.add_argument('y')
parser.add_argument('x_offset')
parser.add_argument('y_offset')
parser.add_argument('type')
parser.add_argument('mode')
parser.add_argument('in_used')
parser.add_argument('gameid')
parser.add_argument('channelid')
parser.add_argument('ld_property')
class Location(Resource):
def __init__(self):
self.args = parser.parse_args()
self.mydb = MysqlBase(**mysql_promotion_config)
def get(self):
gameid = self.args['gameid']
if not gameid:
log.error("get data from location failed!", exc_info=True)
return jsonify({"code": 500, "message": "PLS Input gameid!"})
channelid = self.args['channelid'] or 6001
sql = f"""select
gameid,
channelid,
id,
area,
x,
y,
x_offset,
y_offset,
type,
mode ,
ld_property
from
location
where
gameid={gameid}
AND channelid={channelid}
and in_used=1;"""
try:
#print(f"sql={sql}")
data = self.mydb.query(sql)
except Exception:
log.error("get data from location failed!", exc_info=True)
return jsonify({'code': 500})
all_data = []
log.debug(f"get data from localtion was {data}")
if data:
for line in data:
if line:
localtion = {}
localtion['gameid'], localtion['channelid'], localtion['id'], localtion['area'], localtion['x'], \
localtion['y'], localtion['x_offset'], localtion['y_offset'], localtion['type'], localtion[
'mode'],localtion['ld_property'] = line
all_data.append(localtion)
del localtion
return jsonify({'code': 200, 'message': all_data})
else:
log.error(f"{sql} not found in mysql !")
return jsonify({'code': 200, 'message': ""})
def post(self):
try:
location = {}
location['area'] = self.args['area']
location['x'] = self.args['x']
location['y'] = self.args['y']
location['x_offset'] = self.args['x_offset']
location['y_offset'] = self.args['y_offset']
location['type'] = self.args['type']
location['mode'] = self.args['mode']
location['ld_property'] = self.args['ld_property']
location['gameid'] = self.args['gameid']
location['channelid'] = self.args['channelid']
location['id'] = self.create_id()
if location['area'] and location['type'] and location['mode'] and location['gameid'] and location[
'channelid']:
print(f"{location}")
self.mydb.insert("location", location)
return jsonify({'code': 200})
else:
return jsonify({'code': 500,"message":f"get args was area={location['area']},type={self.args['type']},mode={self.args['mode']},gameid={self.args['gameid']}"})
except Exception:
log.error("set values to location mysql failed!", exc_info=True)
return jsonify({'code': 500})
def create_id(self):
max_id = f"select id from location order by id desc limit 1"
data = self.mydb.query(max_id)
log.info(f"2 {data}")
try:
max = int(data[0][0]) + 1
log.info(f"max id was {max}!")
except Exception:
log.error(f"error with get location id ", exc_info=True)
max = 1001
return max
def put(self):
try:
location = {}
location['id'] = self.args['id']
location['area'] = self.args['area']
location['x'] = self.args['x']
location['y'] = self.args['y']
location['x_offset'] = self.args['x_offset']
location['y_offset'] = self.args['y_offset']
location['type'] = self.args['type']
location['mode'] = self.args['mode']
location['ld_property'] = self.args['ld_property']
location['gameid'] = self.args['gameid']
location['channelid'] = self.args['channelid']
location['in_used'] = self.args['in_used'] or 1
if location['area'] and location['type'] and location['mode'] and location['gameid'] and location[
'channelid']:
sel_sql = f"select area from location where id={self.args['id']};"
data = self.mydb.query(sel_sql)
if data:
condition = f"id='{self.args['id']}'"
self.mydb.update("location", location, condition)
if rebuild_ad():
return jsonify({'code': 200})
else:
return jsonify({'code': 500, 'message': "rebuild cache failed!"})
else:
return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"})
else:
return jsonify({'code': 500, 'message': f"someting not found in {self.args}!"})
except Exception:
log.error("update values to location mysql failed!", exc_info=True)
return jsonify({'code': 500})
def delete(self):
id = self.args['id']
if not id:
return jsonify({'code': 500, 'message': 'id not found'})
try:
del_sql = f'update location set in_used=0 where id={id};'
self.mydb.change(del_sql)
return jsonify({'code': 200})
except Exception:
log.error(f"location remove {id} failed!", exc_info=True)
return jsonify({'code': 500})