def simple_device(request): """ simple_device() push simple text to specified device :param data: device_token,title,content,platform :return: """ cr = SuccCallReturn() access_id = request.data['access_id'] secret_key = request.data['secret_key'] data = request.data device_token = data['device_token'] title = data['title'] content = data['content'] platform = data.get('platform',PlatformType.P_UNDEFINED) platform = int(platform) message = Message_t() message.title = title message.content = content app = core.UserApplication.objects.get(access_id=access_id,secret_key=secret_key) rs = app.app_devices.all() rs = rs.filter( access_token = device_token) if platform: rs = rs.filter(platform = int(platform)) if rs: # r = result[0] token_list =[ device_token ] mexs.ServerAppMexs.instance().sendMessage( app.app_id, token_list, message) return cr.httpResponse()
def sendmail(request): cr = SuccCallReturn() try: conf = BaseAppServer.instance().getYamlConfig()['hippo']['smtp'] mail_to =[] serial = SendMail_Serializer(data = request.data) if not serial.is_valid(): print serial.errors return FailCallReturn(ErrorDefs.ParameterIllegal).httpResponse() mail_to = request.data['mail_to'].split(',') subject = request.data['subject'] content = request.data['content'] mail_from = request.data.get('mail_from') if not mail_from: mail_from = conf.get('mail_from') mail_host= conf.get('smtp_host') mail_port = conf.get('smtp_port') mail_user = conf.get("user") mail_user_password = conf.get('password') desert.network.sendmail.send_mail_smtp( mail_to, subject,content,mail_user,mail_user_password,mail_from,mail_host) identify = genUUID() cr.assign( identify ) except: cr = FailCallReturn(ErrorDefs.ObjectNotExisted) return cr.httpResponse()
def list(self,request): """ 获取用户所能使用的所有业务模型 """ user_id = USER_ID(request) cr = SuccCallReturn() try: user = core.OrgUser.objects.get(id = user_id) bizmodels = user.org.data_models.all() result =[] auth_time = 0 is_authed = False for m in bizmodels: obj ={'name':m.name,'type':m.type,'comment':m.comment,'apps':[]} for app in m.apps.all(): appcfgs = core.OrgUserAppConfig.objects.filter(user__id=user_id,app__type=app.type) if appcfgs: auth_time = maketimestamp64( appcfgs[0].app_auth_time) if auth_time: is_authed = True obj['apps'].append({'name':app.name,'type':app.type,'auth_uri':app.auth_uri,'is_authed':is_authed,'auth_time':auth_time}) result.append(obj) cr.assign( result ) except: traceback.print_exc() cr = FailCallReturn(ErrorDefs.InternalException) return cr.httpResponse()
def create(self,request): """ 手机端app通过oauth授权后发送accessToken,服务器完成验证和数据登记 """ cr = SuccCallReturn() user_id = USER_ID(request) data = request.data serial = serializer.UserAppBindSerializer(data = data) if not serial.is_valid(False): return FailCallReturn(ErrorDefs.ParameterIllegal) try: #转发到connector进行账号验证 data['user_id'] = user_id connector_uri = BaseAppServer.instance().getConfig().get('app_bind_uri') # connector 的bind app处理接口 resp = urllib2.urlopen( connector_uri ,urllib.urlencode( data )) content = resp.read() r = X(json.loads(content)) if r.status == ErrorDefs.SUCC.code:#处理成功 #读取记录 cfg = core.OrgUserAppConfig.objects.get(user__id = user_id,app__type = data['app_type']) serial = serializer.OrgUserAppConfigSerializer(cfg,many=False) # result ={'id':cfg.id,'app_type':cfg.app.type,'app_name':cfg.app.name,'app_acct':cfg.app_user_name,'auth_time':cfg.app_auth_time} cr.assign( serial.data ) else: cr =FailCallReturn(ErrorDefs.InternalException) except: cr =FailCallReturn(ErrorDefs.InternalException) return cr.httpResponse()
def simple_tag(request): """ simple_tag push simple text to devices that be taged. :param data: tag,title,content,platform :return: """ cr = SuccCallReturn() access_id = request.data['access_id'] secret_key = request.data['secret_key'] data = request.data tag = data['tag'] title = data['title'] content = data['content'] platform = data.get('platform',PlatformType.P_UNDEFINED) platform = int(platform) message = Message_t() message.title = title message.content = content result = core.UserApplication.objects.get(access_id=access_id,secret_key=secret_key).app_devices.all() if platform: result = result.filter(platform = int(platform)) result = result.filter( tag = tag) sendMessagePaginated(result,message) return cr.httpResponse()
def satisfaction(self,request): """ 转发 业务分析请求到 jobserver :param request: :return: """ user_id = USER_ID(request) cr = SuccCallReturn() try: user = core.OrgUser.objects.get(id = user_id) data = copy.deepcopy(request.query_params) data['user_acct'] = user.user_name serial = serializer.Analyses_SecondSales_Serializer(data=data) if not serial.is_valid(False): print serial.errors return FailCallReturn(ErrorDefs.ParameterIllegal).httpResponse() # jobserver_uri = BaseAppServer.instance().getConfig().get('job_server_data_analyses_uri') request_uri = jobserver_uri+'/satisfaction' # data = serial.data # # user = core.OrgUser.objects.get(id = user_id) # data['user_acct'] = user.user_name resp = urllib2.urlopen( request_uri+'/?'+urllib.urlencode( data )) content = json.loads( resp.read() ) # cr.assign( content) return Response(content) except: traceback.print_exc() cr = FailCallReturn(ErrorDefs.InternalException) return cr.httpResponse()
def fetchall(request,format=None): cr = SuccCallReturn() result = { 'user':getSelfInfo(request), 'apps':app.getUserAppList(request) } cr.assign(result) return cr.httpResponse()
def list(self,request): """ 列出所有用户绑定的第三方账号 :param request: :return: """ cr = SuccCallReturn() cr.assign( getUserAppList(request) ) return cr.httpResponse()
def domain_probe(request,domain): cr = SuccCallReturn() try: if core.Orgnization.objects.filter(domain = domain).count(): cr.assign(domain) else: cr = FailCallReturn(ErrorDefs.ObjectNotExisted) except: cr = FailCallReturn(ErrorDefs.ObjectNotExisted) return cr.httpResponse()
def aborted(self,request): """ connector 通知任务执行失败 :param request: :return: """ cr = SuccCallReturn() serial = serializer.ConnectorTaskSerializer(data=request.data) if not serial.is_valid(False): return FailCallReturn(ErrorDefs.ParameterIllegal).httpResponse() task = X(serial.data) service.core.JobService.instance().onJobTaskAborted(task) return cr.httpResponse()
def post(self,request): cr = SuccCallReturn() serializer = RegisterSerializer(data=request.data) if not serializer.is_valid(): cr = FailCallReturn(ErrorDefs.ParameterIllegal,serializer.errors) return cr.httpResponse() access_id = serializer.data['access_id'] secret_key = serializer.data['secret_key'] account = serializer.data['account'] device_id = serializer.data['device_id'] platform = serializer.data['platform'] tag = serializer.data['tag'] #添加或更新设备注册记录 try: app = core.UserApplication.objects.get( access_id = access_id, secret_key = secret_key) rs = app.app_devices.filter( account = account , device_id = device_id ) device = None if not rs: device = core.UserAppDevice() else: device = rs[0] device.app = app device.account = account device.device_id = device_id if tag: device.tag = tag if platform: device.platform = platform device.access_time = datetime.datetime.now() device.access_token = create_device_access_token() device.save() cr.assign( device.access_token ) # return device access_token to app except : cr = FailCallReturn(ErrorDefs.InternalException) return cr.httpResponse()
def gen_identify_image(request): cr = SuccCallReturn() try: conf = BaseAppServer.instance().getYamlConfig()['hippo']['idenfity_image'] prefix = conf.get('key_prefix') image,code = desert.image.sign_code.create_validate_code() redis = BaseAppServer.instance().getCacheServer() key = prefix + '_' + getdigest(code) redis.set( key,code,60*5 ) # expired in 5 minutes cr.assign( { 'key': key,'data':image} ) except: traceback.print_exc() cr = FailCallReturn(ErrorDefs.ObjectNotExisted) return cr.httpResponse()
def destroy(self,request,pk=None): """ 解除绑定 :param request: :param pk: :return: """ cr = SuccCallReturn() user_id = USER_ID(request) try: rs = core.OrgUserAppConfig.objects.filter(id= int(pk),user__id= user_id ) rs.delete() except: return FailCallReturn(ErrorDefs.InternalException).httpResponse() return cr.httpResponse()
def put(self,request): user_id = USER_ID(request) cr = SuccCallReturn() try: user = core.OrgUser.objects.get(id = user_id) serial = serializer.OrgUserSerializer(user,data=request.data,partial=True) if not serial.is_valid(False): # return FailCallReturn(ErrorDefs.ParameterIllegal).httpResponse() print serial.errors serial.save() except: traceback.print_exc() cr = FailCallReturn(ErrorDefs.InternalException) return cr.httpResponse()
def retrieve(self,request,pk=None,format=None): """ 接收app账号信息 :param request: :param pk: :param format: :return: """ cr = SuccCallReturn() user_id = USER_ID(request) rs = core.OrgUserAppConfig.objects.filter(id= int(pk),user__id= user_id ) if not rs: return FailCallReturn(ErrorDefs.ObjectNotExisted).httpResponse() cfg = rs[0] serial = serializer.OrgUserAppConfigSerializer(cfg,many=False) print serial cr.assign( serial.data ) return cr.httpResponse()
def satisfaction(self,request): """ 满意度调查 :param request: :return: """ cr = SuccCallReturn() try: serial = serializer.Analyses_SecondSales_Serializer(data=request.query_params) if not serial.is_valid(False): return FailCallReturn(ErrorDefs.ParameterIllegal).httpResponse() user = core.OrgUser.objects.get(user_name = serial.data['user_acct']) #找出 biz_mode关联的app,并判别app是否已经配置? bizmode_type = swarm.base.AnalysisDataModeDef.Satisfaction.value datamode = core.AnalysisDataModel.objects.get(type = bizmode_type) apps = datamode.apps.all() error_result =[] for app in apps: if user.userapp_set.all().filter(app__id = app.id).count() == 0: #用户未配置 app 帐号 error_result.append({'app_name':app.name,'app_type':app.type,'app_auth_uri':app.auth_uri}) if error_result: cr = FailCallReturn(swarm.error.ErrorDefs.AppUnAuthorized) cr.assign( error_result ) return cr.httpResponse() # apps =user.userapp_set.all().order_by('app_user_name') # if not apps: # return FailCallReturn(swarm.error.ErrorDefs.AppUnAuthorized).httpResponse() # 未有帐号绑定 apps =user.userapp_set.all().order_by('app_user_id') appnames = map(lambda app:app.app_user_id,apps) # 将app帐号名称拼接成串 job = service.core.Job() job.user_id = user.id job.user_acct = serial.data['user_acct'] # job.biz_model = serial.data['biz_model'] job.biz_model = swarm.base.AnalysisDataModeDef.Satisfaction.value job.app_accts = appnames status,result = service.core.JobService.instance().getJobResult(job.ID) enable = BaseAppServer.instance().getConfig().get('connector_enable','true') if not enable: print 'service "connector" be skipped..' status = service.core.JobStatusType.FINISHED if status == service.core.JobStatusType.FINISHED: accts = string.join( map(lambda a: '%s:%s'%(a.app.type,a.app_user_id),apps) ,',') data = dict(request.query_params) request_uri = BaseAppServer.instance().getConfig().get('acs_data_analyses_uri') request_uri +='/satisfaction' # print request.GET # '''{u'time_granule': [u'day'], u'user_acct': [u'wangdazhi'], u'start_time': [u'0'], u'biz_model': [u'satisfaction'], u'subtype': [u'5'], u'end_time': [u'0']}> # ''' params = map(lambda p: (p[0],p[1][0]),data.items()) data = dict(params) data['app_accts'] = accts # print request_uri+'?'+urllib.urlencode( data ) resp = urllib2.urlopen( request_uri+'?'+urllib.urlencode( data )) content = json.loads( resp.read() ) return Response(content) #从acs返回的 elif status == service.core.JobStatusType.RUNNING: cr = FailCallReturn(swarm.error.ErrorDefs.DataInProcessing) else: service.core.JobService.instance().createJobRunner(job).startJob() # if not result: # cr = FailCallReturn(ErrorDefs.InternalException,u'startJob Failed') # else: cr = FailCallReturn(swarm.error.ErrorDefs.DataInProcessing) except: traceback.print_exc() cr = FailCallReturn(ErrorDefs.InternalException) return cr.httpResponse()
def get(self,request,format=None): user_id = USER_ID(request) cr = SuccCallReturn() result = getSelfInfo(request) cr.assign(result) return cr.httpResponse()