async def save_statistic_data(cls, feature='', success=0, failure=0, ignored=0): sum = success + failure + ignored if cls.ENV == "Local": url = 'http://localhost:8085/statistic/msn_cms' else: url = 'http://msnapi.eastasia.cloudapp.azure.com/statistic/msn_cms' res = await cls.Request(headers={}, endpoint=url, methods='post', json={ "feature": feature, "sum": sum, "success": success, "failure": failure, "ignored": ignored, "env": cls.ENV }) if 'error' in res: logging.info('save statistic data failed') message = Util.format_Resp(code_type=CodeStatus.RequestError, message='save statistic data failed', exception=res['error'], sys_obj=sys._getframe()) else: logging.info('Save statistic data successfully') message = Util.format_Resp(data='Save statistic data successfully')
def gdisconnect(): access_token = login_session.get('access_token') if access_token is None: return Util.custom_make_response( json.dumps(_('current_user_not_connected')), 401) # noqa url = 'https://accounts.google.com/o/oauth2/revoke?token=%s' % \ login_session['access_token'] del login_session['user_id'] del login_session['access_token'] del login_session['gplus_id'] del login_session['username'] del login_session['email'] del login_session['picture'] h = httplib2.Http() result, content = h.request(url, 'GET') response_code = 0 response_msg = '' if result['status'] == '200': response_code = 200 response_msg = 'successfully_disconnected' else: response_code = 400 response_msg = _('failed_to_revoke_token_for_given_user') return Util.custom_make_response(json.dumps(response_msg), response_code)
async def get_all(cls, page=None, pageSize=None, filterParams={}): filterObj = cls.filter(**filterParams) if page and pageSize: data = list( map( lambda x: cls.toDict(x), await filterObj.offset( (int(page) - 1) * int(pageSize)).limit(int(pageSize)))) return Util.format_Resp(data=data, count=await filterObj.count(), curPage=page) else: data = [cls.toDict(item) for item in await filterObj.all()] return Util.format_Resp(data=data)
async def send_mail_by_msn_api(cls, subject, to, data, cc=None, bcc=None, template_path=None, feature=None): ''' 调用msn_api发送邮件 :param subject: :param to: :param data: :param cc: :param bcc: :param template_path: :return: ''' if cls.ENV == "Local": url = 'http://localhost:8085/mail/msn_cms' else: url = 'http://msnapi.eastasia.cloudapp.azure.com/mail/msn_cms' template_dir = Path(__file__).parent / 'templates' env = Environment(loader=FileSystemLoader(str(template_dir)), trim_blocks=True) body = env.get_template(template_path).render(data=data) headers = {'sender': "MMAIS-MSN"} res = await cls.Request(headers=headers, endpoint=url, methods='post', json={ "subject": subject, "body": body, "to": to, "cc": cc, "bcc": bcc, "feature": feature, "env": cls.ENV }) response = res.get('response') if 'error' in response: logging.info('send mail {} failed'.format(to)) message = Util.format_Resp(code_type=CodeStatus.SendMailError, message='send mail failed', exception=res['error'], sys_obj=sys._getframe()) else: logging.info('Send mail {} successfully'.format(to)) message = Util.format_Resp(data='Send mail successfully') return message
async def update(cls, data): ''' 更新,如果没有传id则为新增 :param data: :return: ''' id = data.get("id") if not id: return await cls.add(data) del data["id"] if data.get("start_datetime") and data.get("end_datetime"): data["work_hours"] = Util.cal_work_hour(data.get("start_datetime"), data.get("end_datetime")) await cls.filter(id=id).update(**data) return Util.format_Resp(message="更新成功")
async def error_middleware(request, handler): try: response = await handler(request) return response except web.HTTPException as ex: if ex.status == 404: res = Util.format_Resp(code_type=CodeStatus.NotFound, alert='page not found') return web.json_response(res, status=CodeStatus.NotFound.value) raise except: exp = sys.exc_info() data = Util.format_Resp(code_type=CodeStatus.UnknownError, exp_obj=exp) logging.error(data.get('errorDetail')) return web.json_response(data)
def user_login(self, username, password): try: user_result = db.session.query(User).filter( User.username == username).first() if not user_result: return None, ResultCode.ValueError user_hash = user_result.user_hash result = db.session.query( Authentication.hmac_key, Authentication.encrypted_password).filter( Authentication.user_hash == user_hash).first() except Exception as exc: current_app.logger.critical("user_login: Unknown error: %s" % exc) return None, ResultCode.DBError if result is None: return None, ResultCode.ValueError hmac_key = result.hmac_key encrypted_password = Util.getEncryptedPassword(hmac_key, password) if result.encrypted_password == encrypted_password: return FlaskUser(user_hash, username), ResultCode.Success else: return None, ResultCode.ValueError
async def get_one(cls, filterParams=None): if filterParams: queryObj = await cls.get_or_none(**filterParams) if not queryObj: data = {} else: data = cls.toDict(queryObj) else: data = cls.toDict(await cls.first()) return Util.format_Resp(data=data)
async def subscribe(self, value: Any): state: DhtState = value if self.url is not None: requests.post( url=self.url, json={ 'payload': state.to_json(), 'hash': Util.get_sig(json.dumps(state.to_json())) } )
def user_register(self, username, password): hmac_key = Util.generateRandomBytes(32) encrypted_password = Util.getEncryptedPassword(hmac_key, password) user_hash = Util.generateUserHash(username) try: db.session.add(User(username, user_hash, email=None)) db.session.add( Authentication(user_hash, encrypted_password, hmac_key)) db.session.commit() except sqlalchemy.exc.IntegrityError as exc: current_app.logger.critical("user_register: Integrity error: %s" % exc) return None, ResultCode.FormatError except Exception as exc: current_app.logger.critical("user_register: Unknown error: %s" % exc) return None, ResultCode.DBError return "ok", ResultCode.Success
def toDict(cls, obj, delKeys=[]): if not obj: return {} data = {} for item in obj._meta.fields: if item in delKeys: continue value = getattr(obj, item) if isinstance(value, Enum): value = value.value elif isinstance(value, datetime): if item in ["aim_datetime", "plant_start_datetime"]: value = Util.datetime_to_str(value, "%Y-%m-%d") else: value = Util.datetime_to_str(value, "%Y-%m-%d %H:%M") elif item in [ "snapshot", "company_logo", "seed_photo", "company_wx_logo" ]: if not value: value = [] else: baseUrl = os.getenv( "STATICFILEDOMAIN", "https://agro-cloud-doctor-pictures.oss-cn-beijing.aliyuncs.com" ) value = [{ "origin": OSS.get_temp_url(pic), "thumbnail": "{}/{}?x-oss-process=image/resize,p_50".format( baseUrl, pic) } for pic in value.split(",")] else: value = getattr(obj, item) data[item] = value return data
def device_register(self, user_hash, device_name, sensor_type): device_id = Util.generateRandomString(32) api_key = Util.generateRandomString(32) try: # 存在するセンサータイプが指定されているか sensorType = db.session.query(SensorType)\ .filter(SensorType.sensor_id == sensor_type)\ .first() if not sensorType: return None, ResultCode.ValueError # デバイス登録 db.session.add(Device( device_id=device_id, device_name=device_name, sensor_type=sensor_type, user_hash=user_hash, api_key=api_key)) db.session.commit() except sqlalchemy.exc.IntegrityError as exc: current_app.logger.critical("SQL Integrity error: %s" % exc) return None, ResultCode.FormatError except Exception as exc: current_app.logger.critical("Unknown error: %s" % exc) return None, ResultCode.GenericError device_dict = { "device_name": device_name, "device_id": device_id, "sensor_type": sensor_type, "api_key": api_key} return device_dict, ResultCode.Success
async def upload(cls, fileObj=None, path=None, request=None, content=None): ''' :param fileObj: :param path: :param request: 获取customerId、stationId :param content: file stream :return: ''' async with asyncoss.Bucket(cls.auth, cls.endpoint, cls.bucketName) as bucket: basePath = "{}/{}/{}".format(Util.get_now("%Y%m%d"), request.customerId, request.id) if content: fullFileName = "{}/{}".format(basePath, path) else: suffix = fileObj.filename.split(".")[-1] fileName = Util.gen_md5_hash("{}+{}".format( fileObj.filename, time.time())) fullFileName = "{}/{}/{}.{}".format(basePath, path, fileName, suffix) content = fileObj.file.read() await bucket.put_object(fullFileName, content) return fullFileName
async def api_middleware(request, handler): ''' :param request: :param handler: :return: ''' from app import app ip = request.remote if "docs" in request.path: return await handler(request) headers = request.headers token = headers.get("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1NSIsImN1c3RvbWVySWQiOiIxNTkyMDY1MjE0IiwidXNlcm5hbWUiOiLpooTnoJTmuKnlrqQiLCJhdXRob3JpdGllcyI6IiIsImV4cCI6MTYwODExMDY0MH0.K2wbpHLx9InscxERKD8qvFynVJoiXLDaORmX6GB0reCXL8Nirxi1NZeSvskm74IlT4NpoUPRkvKwP10vRwECwQ") # config = await app.redis.hkeys(token) # if not config: data = (await Lib.Request(headers={"Authorization": "Bearer {}".format(token)}, endpoint="https://agro-iot.auto-control.com.cn/api/user")).get("response", {}).get( "data", {}) if data: del data['createDate'] del data['modifiedDate'] del data['active'] await app.redis.hmset_dict( token, data) else: return web.json_response(Util.format_Resp(code_type=CodeStatus.BadRequest, message="登陆失败")) # else: # data = await app.redis.hgetall(token, encoding="utf-8") ''' data中字段 "id": 6, "customerId": 1586272542, "createDate": "2020-04-08 07:15:42", "modifiedDate": "2020-12-17 01:27:04", "phoneNumber": "13911682107", "username": "******", "nickname": "奥托测试", "avatar": "", "password": "", "roles": "ADMIN", "active": true ''' for k, v in data.items(): setattr(request, k, v) request.token = token return await handler(request)
def create_app(DBURL=None): app = Flask(__name__) try: app.config.from_pyfile('../sensors.conf') except FileNotFoundError as exc: app.logger.critical("'../sensors.conf' is not found.") raise FileNotFoundError(exc) try: if DBURL is not None: dburl = DBURL else: dburl = app.config['DBURL'] app.config['SQLALCHEMY_DATABASE_URI'] = dburl app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True except KeyError as exc: app.logger.critical( "DBURL is not set. please set dburl at sensors.conf!") raise KeyError(exc) app.config["SECRET_KEY"] = Util.generateRandomBytes(32) app.config['JSON_AS_ASCII'] = False Util.MaxUsernameLength = app.config["MAX_USERID_LENGTH"] Util.MaxUserPassLength = app.config["MAX_USERPASS_LENGTH"] csrf = CSRFProtect(app) csrf.init_app(app) # ビューの登録 app.register_blueprint(webui, url_prefix='/') app.register_blueprint(api, url_prefix='/api/') # データベースの登録 db.init_db(app) return app
async def add_all(cls, data): for item in data: await cls(**item).save() return Util.format_Resp(message="添加成功")
async def add(cls, data): await cls(**data).save() return Util.format_Resp(message="添加成功")
async def remove(cls, id): await cls.filter(id=id).delete() return Util.format_Resp(message="删除成功")
import celery from datetime import datetime, timedelta from .db_conn import Session, Emails, Recipients from app.util import Util util = Util() @celery.task def check_email(): session = Session() logger = check_email.get_logger() logger.info("Check email") now = datetime.now().replace(second=0, microsecond=0) data = session.query(Emails.email_subject, Emails.email_content, Recipients.email_address)\ .join(Recipients, Emails.event_id == Recipients.event_id)\ .filter(Emails.timestamp.between( now, now+timedelta(minutes=1))) for i in data: print(i) send_email.delay(i) @celery.task def send_email(data): logger = send_email.get_logger() logger.info(data) util.sendemail(data)
async def delete(cls, fileName): async with asyncoss.Bucket(cls.auth, cls.endpoint, cls.bucketName) as bucket: await bucket.delete_object(fileName) return Util.format_Resp(message="delete successfully")
def gconnect(): # Validate state token if request.args.get('state') != login_session['state']: return Util.custom_make_response( json.dumps(_('invalid_state_parameter')), 401) # noqa # Obtain authorization code code = request.data try: # Upgrade the authorization code into a credentials object oauth_flow = flow_from_clientsecrets(Config.CLIENT_SECRET_JSON, scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: return Util.custom_make_response( json.dumps(_('failed_to_upgrade_the_authorization_code')), 401) # noqa # Check that the access token is valid. access_token = credentials.access_token url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' % access_token) h = httplib2.Http() result = json.loads(h.request(url, 'GET')[1]) # If there was an error in the access token info, abort. if result.get('error') is not None: return Util.custom_make_response(json.dumps(result.get('error')), 500) # Verify that the access token is used for the intended user. gplus_id = credentials.id_token['sub'] if result['user_id'] != gplus_id: return Util.custom_make_response( json.dumps(_("tokens_user_id_doesnt_match_given_user_id")), 401) # noqa # Verify that the access token is valid for this app. if result['issued_to'] != CLIENT_ID: return Util.custom_make_response( json.dumps(_("tokens_client_id_does_not_match_apps")), 401) # noqa # Get user info userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo" params = {'access_token': credentials.access_token, 'alt': 'json'} answer = requests.get(userinfo_url, params=params) data = answer.json() user = User.check_new_user(name=data['name'], email=data['email'], picture=data['picture']) if user is None or not user.id: return Util.custom_make_response(json.dumps(_("invalid_user")), 401) login_session['user_id'] = user.id login_session['username'] = data['name'] login_session['picture'] = data['picture'] login_session['email'] = data['email'] login_session['access_token'] = credentials.access_token login_session['gplus_id'] = gplus_id return '1'