promotion/handler/company.py
2019-07-09 17:10:39 +08:00

126 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from flask import Flask, jsonify
from flask_restful import reqparse, abort, Api, Resource
import logging
from myredis.myredis import company_redis, expire_time
from mysql.mmysql import MysqlBase
from config import mysql_promotion_config
import pdb
log = logging.getLogger(__name__)
mydb = MysqlBase(**mysql_promotion_config)
parser = reqparse.RequestParser()
parser.add_argument('id')
parser.add_argument('name')
parser.add_argument('contact')
parser.add_argument('appid')
parser.add_argument('appkey')
parser.add_argument('status')
# # 操作put / get / delete单一资源Todo
# shows a single todo item and lets you delete a todo item
class Company(Resource):
def __init__(self):
self.args = parser.parse_args()
def get(self):
status = self.args['status']
all_data = []
try:
sql = f"select id,name,contact,appid,appkey,status from company where status={status};"
data = mydb.query(sql)
log.info(f"get data from db was {data}")
if data:
company = {}
for line in data:
if line:
company['id'], company['name'], company['contact'], company['appid'], company['appkey'], company[
'status'] = line
all_data.append(company)
del company
else:
log.error(f"{data} not found in mysql !")
return jsonify({'code': 404})
except Exception:
log.error(f"get company status={status} failed! ", exc_info=True)
return jsonify({'code': 500})
return jsonify({'code': 200, 'message': all_data})
def delete(self):
id = self.args['id']
try:
del_sql = f'delete from company where id={id};'
mydb.query(del_sql)
except Exception:
log.error(f"remove {id} failed!", exc_info=True)
return jsonify({'code': 500})
return jsonify({'code': 200})
def get_random(self, num=8):
import random
import string
return ''.join(random.sample(string.ascii_letters + string.digits, num))
def get_appid(self):
appid = self.get_random()
# check appid not in db
check_appid = f"select appid from company where appid={appid}"
data = mydb.query(check_appid)
if data:
self.get_appid()
return appid
def get_companyid(self):
max_id = f"select id from company order by id desc limit 1"
data = mydb.query(max_id)
try:
max = data[0] + 1
except Exception:
max = 1001
return max
def post(self):
try:
company = {}
company['name'] = self.args['name']
company['contact'] = self.args['contact']
company['tel'] = self.args['tel']
company['id'] = self.get_companyid()
company['appid'] = self.get_appid()
mydb.insert("company", company)
except Exception:
log.error("set values to company mysql/redis failed!", exc_info=True)
return jsonify({'code': 500})
return jsonify({'code': 200})
def put(self):
try:
company = {}
company['id'] = self.args['id']
company['name'] = self.args['name']
company['contact'] = self.args['contact']
company['tel'] = self.args['tel']
company['status'] = self.args['status'] or 0
company['appid'] = self.args['appid']
company['appkey'] = self.args['appkey']
sel_sql = f"select name from company where id={self.args['id']};"
data = mydb.query(sel_sql)
if data:
condition = f"id='{self.args['id']}'"
mydb.update("company", company, condition)
return jsonify({'code': 200})
else:
return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"})
except Exception:
log.error("update values to company redis,mysql failed!", exc_info=True)
return jsonify({'code': 500})