promotion/handler/company.py
2019-07-08 14:38:08 +08:00

101 lines
3.5 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, expirt_time
from mysql.mmysql import MysqlBase
from config import mysql_promotion_config
log = logging.getLogger(__name__)
mydb = MysqlBase(**mysql_promotion_config)
parser = reqparse.RequestParser()
parser.add_argument('comp_id')
# # 操作put / get / delete单一资源Todo
# shows a single todo item and lets you delete a todo item
class Company(Resource):
def get(self, comp_id):
try:
data = company_redis.hmget(comp_id, ['id', 'name', 'contact', 'appid', 'appkey'])
if not data:
sql = f"select id,name,contact,appid,appkey from company where id={comp_id};"
data = mydb.query(sql)
company = {}
company['id'], company['name'], company['contact'], company['appid'], company['appkey'] = data
company_redis.hmset(company['id'], company)
company_redis.expire(company['id'], expirt_time)
except Exception:
log.error(f"get company id {comp_id} failed! ", exc_info=True)
return jsonify({'code': 0})
return jsonify({'code': 1, 'body': data})
def delete(self, comp_id):
try:
company_redis.expirt(comp_id, 0)
del_sql = f'delete from company where id={comp_id};'
mydb.query(del_sql)
except Exception:
log.error(f"remove {comp_id} failed!", exc_info=True)
return jsonify({'code': 0})
return jsonify({'code': 1})
def post(self, comp_id):
args = parser.parse_args()
try:
company = {}
company['id'] = comp_id
company['name'] = args['name']
company['contact'] = args['contact']
company['appid'] = args['appid']
company['appkey'] = args['appkey']
# insert to mysql
mydb.insert("company", company)
# insert to redsi
company_redis.hmset(company['id'], company)
company_redis.expirt(company['id'], expirt_time)
except Exception:
log.error("set values to company mysql/redis failed!", exc_info=True)
return jsonify({'code': 0})
return jsonify({'code': 1})
def put(self, comp_id):
args = parser.parse_args()
try:
company = {}
company['id'] = comp_id
company['name'] = args['name']
company['contact'] = args['contact']
company['appid'] = args['appid']
company['appkey'] = args['appkey']
# insert to mysql
update_sql = f"update company set id={company['id']} name={company['name']} contact={company['contact']}\
appid={company['appid']} appkey={company['appkey']} where id={comp_id};"
mydb.query("company", update_sql)
# update redsi
company_redis.hmset(company['id'], company)
company_redis.expirt(company['id'], expirt_time)
except Exception:
log.error("update values to company redis,mysql failed!", exc_info=True)
return jsonify({'code': 0})
return jsonify({'code': 1})
# # 操作post / get资源列表TodoList
# shows a list of all todos, and lets you POST to add new tasks
class CompanyList(Resource):
def get(self):
return jsonify({'code': 1, 'body': company_redis.lrang("ALL_Company", 0, -1)})
def post(self):
args = parser.parse_args()
try:
company_redis.rpush("ALL_Company", args['company'])
except Exception:
log.error("Insert values to redis failed,args was {}!".format(args), exc_info=True)
return jsonify({'code': 0})
return jsonify({'code': 1})