Exemple #1
0
	def addJobResult(self,job_id,result):
		import time
		cache = BaseAppServer.instance().getCacheServer()
		max_life_time =BaseAppServer.instance().getConfig().get('score_board').get('max_job_lifetime',1)*60
		max_life_time = int(max_life_time)

		cache.set( self.job_hash_id(job_id),str(time.localtime()),max_life_time)
Exemple #2
0
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()
Exemple #3
0
	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()
Exemple #4
0
	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()
Exemple #5
0
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()
Exemple #6
0
	def _get_app_account_hash_from_cache(self,user):
		user_id = 0
		if type(user) == core.OrgUser:
			user_id = user.id
		else:
			user = int(user_id)
		key = swarm.base.CacheFieldFormatType.user_app_acct_hash%(user_id,)
		value = BaseAppServer.instance().getCacheServer().get(key)
		return value
Exemple #7
0
	def _wrapper(request,*args,**kwargs):
		token = request.data.get('token')
		if not  token:
			return FailCallReturn(desert.errors.ErrorDefs.TokenInvalid).httpResponse()


		redis = BaseAppServer.instance().getCacheServer()
		key = 'token_%s'%token
		value = redis.get( key)
		if not value:
			return FailCallReturn(desert.errors.ErrorDefs.TokenInvalid).httpResponse()

		data = json.loads( value)

		rs = core.Player.objects.filter(num_id = data['num_id'])
		if not rs:
			return FailCallReturn(desert.errors.ErrorDefs.ObjectNotExisted).httpResponse()
		request.player = rs[0]

		return func(request,*args,**kwargs)
Exemple #8
0
 def getJobResult(self, job_id):
     cache = BaseAppServer.instance().getCacheServer()
     value = cache.get(self.job_hash_id(job_id))
     return value
Exemple #9
0
 def removeJobResult(self, job_id):
     cache = BaseAppServer.instance().getCacheServer()
     cache.delete(self.job_hash_id(job_id))
Exemple #10
0
	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()