def _handle_exception(self, handler, user_profile, xml_message): assert (handler and user_profile) alert_message = u"处理消息失败,处理实现:{}\n所处理消息:\n{}\n异常信息:\n{}". \ format(handler.__class__.__name__, xml_message, unicode_full_stack()) watchdog_fatal(alert_message)
def handle(request, webapp_id): global message_pipeline if not message_pipeline: from weixin.message.message_pipeline import MessagePipeline message_pipeline = MessagePipeline() if 'echostr' in request.GET: if is_valid_request(request, webapp_id): return HttpResponse(request.GET['echostr']) else: return HttpResponse('') else: #通过微信来的数据kkj try: content = message_pipeline.handle(request, webapp_id) except: notify_message = u"进行消息处理失败,webapp_id:{} cause:\n{}".format(webapp_id, unicode_full_stack()) watchdog_fatal(notify_message) content =None if content is None or len(content) == 0: return HttpResponse('') else: return HttpResponse(content, mimetype="application/xml")
def _get_new_token_info(app_id, app_secret): if app_id is None or app_secret is None: return None update_url = ACCESS_TOKEN_UPDATE_URL_TMPL.format(app_id, app_secret) update_response_data = None try: update_response_data = _get_access_token_update_response(update_url) except: full_stack_str = unicode_full_stack() watchdog_fatal( u"failed to update access token for app_id({}), cause:\n{}".format( app_id, full_stack_str)) #self.LOG.warn(u"最多重试{}次进行更新".format(self.MaxRetryTimes)) for retry_time in xrange(ACCESS_TOKEN_UPDATE_MAX_RETRY_TIMES): try: update_response_data = _get_access_token_update_response( update_url) except: full_stack_str = unicode_full_stack() watchdog_fatal( u"failed to update access token for appid:({}), cause:\n{}" .format(app_id, full_stack_str)) return update_response_data
def get_express_details_by_order(order): """ 根据 order 获取 快递明细信息 """ details = ExpressDetail.objects.filter( order_id=order.id).order_by('-display_index') if details.count() > 0: return list(details) expresses = ExpressHasOrderPushStatus.objects.filter( express_company_name=order.express_company_name, express_number=order.express_number) if expresses.count() == 0: return list([]) try: express = expresses[0] return list( ExpressDetail.objects.filter( express_id=express.id).order_by('-display_index')) except: innerErrMsg = full_stack() watchdog_fatal( u'获取快递详情失败,order_id={}, case:{}'.format(order.id, innerErrMsg), EXPRESS_TYPE) return list([])
def update_access_token(access_token): if access_token is None: return False if type(access_token) is not WeixinMpUserAccessToken: raise ValueError(u'access_token must be WeixinMpUserAccessToken') new_token_info = _get_new_token_info(access_token.app_id, access_token.app_secret) if None == new_token_info: False if new_token_info.has_key(ACCESS_TOKEN_ATTR): #watchdog_error(u"failed to update access token for user({}), response:\n{}".format(access_token.mpuser.username, json.dumps(new_token_info))) try: new_access_token = new_token_info[ACCESS_TOKEN_ATTR].strip() expires_span = new_token_info[ACCESS_TOKEN_EXPIRES_IN_ATTR] _update_access_token_record(access_token, new_access_token, expires_span) return True except: full_stack_str = unicode_full_stack() watchdog_fatal( u"failed to update access token in db for user({}), cause:\n{}" .format(access_token.mpuser.username, full_stack_str)) return False
def process_request(self, request): path_info = request.path_info pos = path_info.find('/', 2) app = str(path_info[:pos+1]) if not app in resource.RESTFUL_APP_SET: if app in RESOURCE_NAMES: data = { 'path_info': path_info, 'app': app, 'resource': resource.RESTFUL_APP_SET } watchdog_fatal(str(data), type='RESTFUL_MIDDLEWARE') return None method = request.META['REQUEST_METHOD'] if method == 'POST' and '_method' in request.REQUEST: _method = request.REQUEST['_method'] method = _method.upper() request.original_path_info = path_info if path_info[-1] == '/': request.path_info = '%s%s' % (path_info, method) else: request.path_info = '%s/%s' % (path_info, method) # if 'new_weixin' in path_info: # data = { # 'path_info': path_info, # 'new_path_info': request.path_info, # 'app': app, # 'resource': resource.RESTFUL_APP_SET # } # watchdog_info(str(data), type='RESTFUL_MIDDLEWARE') return None
def set_submit_booking(self): data = self._submit_booking() watchdog_info(u"set_submit_booking data:\n{}".format(data), self.shengjing_params.WATCHDOG_TYPE_SHENGJING) if data.get('Header').get('Code') != 0: watchdog_fatal(u'从Shengjing获取submit booking返回code不为0,url:{}, data:{}'.format(self.api_url, self.param_str), self.shengjing_params.WATCHDOG_TYPE_SHENGJING) return data
def get_qq_account_info(user_profile, social_account, can_use_local=True): qq_account_info = None if can_use_local: try: qq_account_info = _get_local_qq_account_info(social_account) except: notify_message = u"从本地获取QQ账号基本信息失败,openid:{}, cause:\n{}".format( social_account.openid, unicode_full_stack()) watchdog_error(notify_message) if qq_account_info and qq_account_info.head_img and qq_account_info.nickname: return qq_account_info #需要进行方式微博api进行实时获取 qq_account_info_via_api = _get_qq_account_info_via_api(user_profile, social_account) if qq_account_info and qq_account_info_via_api: try: _update_local_qq_account_info(social_account, qq_account_info_via_api) except: notify_message = u"更新本地QQ账号基本信息失败,openid:{}, cause:\n{}".format( social_account.openid, unicode_full_stack()) watchdog_fatal(notify_message) elif qq_account_info_via_api: try: _save_local_qq_account_info(social_account, qq_account_info_via_api) except: notify_message = u"添加本地QQ账号基本信息失败,openid:{}, cause:\n{}".format( social_account.openid, unicode_full_stack()) watchdog_fatal(notify_message) return qq_account_info_via_api
def get_weixin_account_info(user_profile, social_account, can_use_local=True): weixin_account_info = None # if can_use_local: # try: # weixin_account_info = _get_local_weixin_account_info(social_account) # except: # notify_message = u"从本地获取微信账号基本信息失败,openid:{}, cause:\n{}".format( # social_account.openid, unicode_full_stack()) # watchdog_error(notify_message) if weixin_account_info and weixin_account_info.nickname and weixin_account_info.head_img: return weixin_account_info #需要进行方式微信api进行实时获取 try: weixin_account_info = _get_weixin_account_info_via_api(user_profile, social_account) except: notify_message = u"访问微信api获取微信账号基本信息失败,openid:{}, cause:\n{}".format( social_account.openid, unicode_full_stack()) watchdog_fatal(notify_message) if weixin_account_info and weixin_account_info.nickname: try: _update_local_weixin_account_info(social_account, weixin_account_info) except: notify_message = u"更新本地微信账号基本信息失败,openid:{}, cause:\n{}".format( social_account.openid, unicode_full_stack()) watchdog_fatal(notify_message) return weixin_account_info
def _verify_response(self): """用登陆返回的code,获取access_token,发送请求""" param_data = {'client_id': self.weibo_config.app_id, 'client_secret': self.weibo_config.app_secret, 'grant_type': 'authorization_code', 'redirect_uri': self.redirect_uri, 'code': self.code} verified_result = '' try: data = urllib.urlencode(param_data) request = urllib2.Request(self.WEIBO_AUTHORIZE_URL_TMPL,data) response = urllib2.urlopen(request) file = response.read() verified_result = file watchdog_info(u"get_access_token_data url: %s ," u"param_data: %s" % (self.WEIBO_AUTHORIZE_URL_TMPL, data)) except: watchdog_error(u'从Weibo获取access token失败,url:{},data:{},原因:{}'.format(self.WEIBO_AUTHORIZE_URL_TMPL, param_data, unicode_full_stack())) notify_message = u'从Weibo获取access token失败,url:{},data:{},原因:{}'.format(self.WEIBO_AUTHORIZE_URL_TMPL, param_data, unicode_full_stack()) watchdog_fatal(notify_message) return verified_result
def login_callback_handler(request): code = request.GET.get('code', None) state = request.GET.get('state', None) new_url = '/' response = HttpResponseRedirect(new_url) watchdog_info(u'从QQ登陆回调函数,code={},state={}'.format(code, state)) if code: try: # 获取webapp_id 和 登陆后跳转地址 webapp_id, new_url = get_shop_name_and_last_url(state) request = set_user_profile(request, webapp_id) # 获取 access_token qq_get_token = QQAccessToken(request, code) data = qq_get_token.get_access_token_data() token = data.get('access_token') # 获取 openId qq_get_open_id = QQOpenID(request, token) data = qq_get_open_id.get_open_id_data() openid = data.get('openid') # 创建 social_account social_account = save_social_account(openid, token, SOCIAL_PLATFORM_QQ, webapp_id) response = HttpResponseRedirect(new_url) save_session(response, social_account) except: watchdog_error(u'从QQ登陆回调函数错误,error={}'.format( unicode_full_stack())) else: response = HttpResponseRedirect(new_url) notify_message = u'从QQ登陆回调函数错误,没有code{}'.format(request) watchdog_fatal(notify_message) return response
def _get_integral_strategy_by_webappid(self, webapp_id): try: return IntegralStrategySttings.objects.get(webapp_id=webapp_id) except: notify_message = u"根据webapp_id获取积分策略失败, webapp_id={}".format( webapp_id) watchdog_fatal(notify_message) return None
def _parse_result(self, result): data = None try: data = json.loads(result) except: notify_message = u'shengjing解析confirm_learn_plan失败,url:{},原因:{}'.format(result, unicode_full_stack()) watchdog_fatal(notify_message, self.shengjing_params.WATCHDOG_TYPE_SHENGJING) return data
def _parse_result(self, result): data = None try: data = json.loads(result) except: notify_message = u'解析access token失败,url:{},原因:{}'.format(result, unicode_full_stack()) watchdog_fatal(notify_message) return data
def process_shared_url_request(request): followed_member_token = get_followed_member_token_from_url_querystr( request) if followed_member_token is None or len(followed_member_token) == 0: return None member = get_member(request) if member: if member.token != followed_member_token: #第一种情况 try: increase_for_click_shared_url(request) except: notify_message = u"根据点击计算积分发生异常, url:{}, cause:\n{}".format( request.get_full_path(), unicode_full_stack()) watchdog_fatal(notify_message) new_url = _replace_followed_member_token_in_request_url( request, member.token) response = HttpResponseRedirect(new_url) response.set_cookie( member_settings.FOLLOWED_MEMBER_TOKEN_SESSION_KEY, followed_member_token, max_age=60 * 60 * 24 * 365) return response else: #第二种情况 return None else: #第三种情况 new_url = remove_querystr_filed_from_request_url( request, member_settings.FOLLOWED_MEMBER_TOKEN_URL_QUERY_FIELD) response = HttpResponseRedirect(new_url) uuid = get_uuid(request) if uuid is None: uuid = generate_uuid(request) request.uuid = uuid response.set_cookie(member_settings.UUID_SESSION_KEY, uuid, max_age=60 * 60 * 24 * 365) try: increase_for_click_shared_url(request) except: notify_message = u"根据点击计算积分发生异常, url:{}, cause:\n{}".format( request.get_full_path(), unicode_full_stack()) watchdog_fatal(notify_message) response.set_cookie(member_settings.FOLLOWED_MEMBER_TOKEN_SESSION_KEY, followed_member_token, max_age=60 * 60 * 24 * 365) return response #其他情况 return None
def update_status(self, new_status): if new_status is None: return self.status = new_status try: CustomizedApp.objects.filter(id=self.id).update(status=new_status) except: notify_msg = u"更新app:{}状态为{}失败,cause:\n{}".format( self.__unicode__(), unicode_full_stack()) watchdog_fatal(notify_msg, self.owner.id)
def __get_current_temp(city_code): data_str, error_info = __get_http_response_data( "www.weather.com.cn", "/data/sk/%s.html" % city_code) temp = '' if data_str: info_json = decode_json_str(data_str) # 当前温度 temp = info_json['weatherinfo']['temp'] else: # print u'获取当前天气温度,天气的api不可用!' watchdog_fatal(u'获取当前天气温度,发送请求失败!%s' % error_info, WATCHDOG_TYPE) return temp
def update_version(self, new_version): if new_version is None: return self.last_version = new_version try: CustomizedApp.objects.filter(id=self.id).update( last_version=new_version) except: notify_msg = u"更新app:{}版本号为{}失败,cause:\n{}".format( self.__unicode__(), new_version, unicode_full_stack()) watchdog_fatal(notify_msg, self.owner.id)
def _parse_result(self, result): data = None try: if result[0: 8] == "callback": start = result.find('(') end = result.find(')') result = result[start+1:end] data = json.loads(result) except: notify_message = u'解析QQ获取的open_id失败,url:{},原因:{}'.format(result, unicode_full_stack()) watchdog_fatal(notify_message) return data
def _parse_result(self, result): data = None try: data = json.loads(result.strip()) if data.get('error_code'): notify_message = u'从weibo获取user_info失败,url:{},返回数据:{}。原因:{}。'.format(result, data, data.get('error_code')) watchdog_fatal(notify_message) except: notify_message = u'解析微博user_info失败,url:{},原因:{}'.format(result, unicode_full_stack()) watchdog_fatal(notify_message) return data
def save_weixin_user_head_img(img_url): new_img_url = None try: if img_url: image_content = _download_image(img_url) new_img_url = _save_head_image_local(image_content) except: notify_message = u"将微信用户头像保存到本地失败({}),cause:\n{}".format( img_url, unicode_full_stack()) watchdog_fatal(notify_message) return new_img_url
def _parse_result(self, result): data = None try: data = json.loads(result) if int(data.get('ret')) != 0: notify_message = u'获取QQ的user_info失败,url:{},返回数据:{}。原因:{}。'.format( result, data, data.get('msg')) watchdog_fatal(notify_message) except: notify_message = u'解析QQ获取的user_info失败,url:{},原因:{}'.format( result, unicode_full_stack()) watchdog_fatal(notify_message) return data
def _handle_member(self, user_profile, weixin_user, is_from_simulator): #是否已经存在会员信息,如果否则进行创建 weixin_user_name = weixin_user.username token = get_token_for(user_profile.webapp_id, weixin_user_name, is_from_simulator) is_for_test = self.__is_test_weixinuser(weixin_user_name) social_accounts = SocialAccount.objects.filter(token=token) if social_accounts.count() > 0: social_account = social_accounts[0] else: social_account = create_social_account(user_profile.webapp_id, weixin_user_name, token, SOCIAL_PLATFORM_WEIXIN, is_for_test) member = get_member_by_binded_social_account(social_account) if member is None: #创建会员信息 try: member = create_member_by_social_account( user_profile, social_account) #之后创建对应的webappuser # if MemberHasSocialAccount.objects.filter(account=social_account, member=member).count() == 0: # MemberHasSocialAccount.objects.create(account=social_account, member=member, webapp_id=social_account.webapp_id) self._create_webapp_user(member) except: notify_message = u"MemberHandler中创建会员信息失败,社交账户信息:('openid':{}), cause:\n{}".format( social_account.openid, unicode_full_stack()) watchdog_fatal(notify_message) if member and hasattr(member, 'is_new') and member.is_new: try: increase_for_be_member_first(user_profile, member) member.is_new = True except: notify_message = u"MemberHandler中创建会员后增加积分失败,会员id:{}, cause:\n{}".format( member.id, unicode_full_stack()) watchdog_error(notify_message) else: member.is_subscribed = True #member.save() #add by bert at 20150323 增加异步任务更新会员关注 member.is_new = False if member and (hasattr(member, 'is_new') is False): member.is_new = False return member
def get_state(self, request): webapp_id = '3181' try: last_page = request.META['HTTP_REFERER'] webapp_id = request.user_profile.webapp_id notify_message = u'微博登陆获取webapp_id在weibo_config.py,webapp_id:{}'.format( webapp_id) watchdog_info(notify_message) except: notify_message = u'微博登陆获取webapp_id错误在weibo_config.py,原因:{}'.format( unicode_full_stack()) watchdog_fatal(notify_message) last_page = '/' return '%s~%s' % (webapp_id, last_page)
def cancel(self, is_force=False): if this._canceller is not None: if is_force: #如果是强制取消,那么无论取消操作是否成功 #都会进行后续的操作(认为该认为已经被取消) try: this._canceller() except: err_msg = u"取消任务({})失败,cause:\n{}".format(self.task, unicode_full_stack()) watchdog_fatal(err_msg) else: this._canceller() this._is_canceled = True
def handle(request, event): """ “调用”services的统一接口 @param request 如果是Request对象,需要包括event_data;也可以是dict()对象,直接作为event_data。 """ if hasattr(request, 'event_data'): event_data = request.event_data else: event_data = request result = None if settings.EVENT_DISPATCHER == 'dummy': print("found sepecial event '{}'".format(event)) return result if settings.TASKQUEUE_ENABLED and REGISTERED_EVENTS.has_key(event): # 如果event是测试的service,以Celery方式处理 task_name = REGISTERED_EVENTS[event] # print("found sepecial event '{}'".format(event)) result = send_task(task_name, args=[None, event_data]) #print("called service: name:'{}', result:{}".format(task_name, result)) else: from services import service_manager if hasattr(request, 'event_data'): if settings.EVENT_DISPATCHER == 'local': # 本地模式:直接调用service函数 service_manager.call_service(event, request.event_data) elif settings.EVENT_DISPATCHER == 'redis': is_processed = False # 如果没有处理,则用老的方式处理event if not is_processed: redis_cli = ensure_redis() if redis_cli: # 将request.event_data数据存入redis if event in EVENT_FILTER: redis_event = '%s_%s' % ( event, str(int(time.time() * 1000))) send_message_to_redis(redis_event, request.event_data) else: redis_cli.rpush(event, request.event_data) else: notify_msg = u"redis服务不能访问, %s:%s:%s" % ( settings.REDIS_HOST, settings.REDIS_PORT, settings.REDIS_SERVICE_DB) watchdog_fatal(notify_msg) service_manager.call_service(event, request.event_data) return result
def __get_weather_info(city_code, weather_id=0): data_str, error_info = __get_http_response_data( "m.weather.com.cn", "/data/%s.html" % city_code) weather_info = [] weather = None if data_str: info_json = decode_json_str(data_str) weather_json = info_json['weatherinfo'] # 计算周几 weeks = [u'一', u'二', u'三', u'四', u'五', u'六', u'日'] week_index = __get_week_index(weeks, weather_json['week']) # 获取今天日期 today_date = datetime.now() total_days, low_date, cur_date, high_date = dateutil.get_date_range( dateutil.get_today(), '6', 6) date_list = dateutil.get_date_range_list(datetime.date(today_date), high_date) for i in range(1, 7): data = dict() data['date'] = date_list[i - 1].strftime("%Y年%m月%d日") data['weather'] = weather_json['weather%d' % i] data['temp'] = weather_json['temp%d' % i] data['week'] = u'周%s' % weeks[week_index] # 给week赋值下标 week_index = week_index + 1 if week_index + 1 < len(weeks) else 0 weather_info.append(data) # 判断是否已经添加过数据,如果添加过就修改 if weather_id: weather = Weather.objects.get(id=weather_id) weather.info = json.dumps(weather_info) weather.update_time = today_date weather.save() else: weather = Weather.objects.create(info=json.dumps(weather_info), city_code=city_code) else: if weather_id: weather = Weather.objects.get(id=weather_id) weather_info = json.loads(weather.info) # print u'更新数据,天气的api不可用!' watchdog_fatal(u'更新数据,天气的api不可用!%s' % error_info, WATCHDOG_TYPE) else: # print u'首次获取数据,天气的api不可用!' watchdog_fatal(u'首次获取数据,天气的api不可用!%s' % error_info, WATCHDOG_TYPE) return weather_info, weather
def _verify_response(self, ): """获取get_user_info,发送请求""" verity_url = self.WEIBO_GET_USER_INFO_TMPL.format(self.access_token, self.uid) verified_result = '' verify_response = None try: verify_response = urllib2.urlopen(verity_url) verified_result = verify_response.read().strip() except: notify_message = u'从weibo获取user_info失败,url:{},原因:{}'.format(verity_url, unicode_full_stack()) watchdog_fatal(notify_message) return verified_result
def app_op_err_callback(task, result, *args, **kw): if result.is_failed(): app_op_result = AppFailedOpResult(app, op, result.result) else: app_op_result = AppSucceedOpResult(app, op, result.result) CustomizedAppOpLog.objects.create( owner = app.owner, customized_app = app, op = op, op_result_msg = '' if result.is_failed() else result.result, failed_cause_stack = result.result if result.is_failed else '' ) watchdog_fatal(app_op_result.unicode(), app.owner.id)
def _process_oauth_member_relation_and_source(fmt, member_id, is_new_created_member=False): from modules.member.integral_new import increase_for_click_shared_url member = Member.objects.get(id=member_id) try: if fmt and member and fmt != member.token: #建立关系,更新会员来源 follow_member = Member.objects.get(token=fmt) if follow_member.webapp_id != member.webapp_id: return if is_new_created_member: MemberFollowRelation.objects.create( member_id=follow_member.id, follower_member_id=member.id, is_fans=is_new_created_member) MemberFollowRelation.objects.create( member_id=member.id, follower_member_id=follow_member.id, is_fans=False) Member.objects.filter(id=member_A.id).update( fans_count=F(fans_count) + 1) member.source = SOURCE_BY_URL member.save() elif MemberFollowRelation.objects.filter( member_id=member.id, follower_member_id=follow_member.id).count() == 0: MemberFollowRelation.objects.create( member_id=follow_member.id, follower_member_id=member.id, is_fans=is_new_created_member) MemberFollowRelation.objects.create( member_id=member.id, follower_member_id=follow_member.id, is_fans=False) #点击分享链接给会员增加积分 try: increase_for_click_shared_url(follow_member, member, request.get_full_path()) except: notify_message = u"increase_for_click_shared_url:('member_id':{}), cause:\n{}".format( member.id, unicode_full_stack()) watchdog_fatal(notify_message) except: notify_message = u"('fmt':{}), 处理分享信息process_oauth_member_relation_and_source cause:\n{}".format( fmt, unicode_full_stack()) watchdog_fatal(notify_message)