def clean(self): cleaned_data = self.cleaned_data area = cleaned_data.get('area') building = cleaned_data.get('building') room = cleaned_data.get('room') if not building or not room: raise forms.ValidationError(u":-D") #验证该寝室号是否正确,即抓一下网站,看能不能返回。 check_data = crawler_dianfei.crawler_once(area.encode('utf-8'),building.encode('utf-8'),str(room)) #未返回数据 if check_data is None or check_data.get('zui_hou_chao_biao_shi_jian') is None: raise forms.ValidationError(u"房间号不存在") return cleaned_data
def responseMsg(request): response = '剩余电量:' from_weixin = handleRequest(request) #关注 if from_weixin.get('MsgType') == 'event': #print 'Msgtype:event:' if from_weixin.get('Event') == 'subscribe': #print 'event:subscribe' content = help.__doc__ response = content #扫码 elif from_weixin.get('Event') == 'SCAN': #print 'event:scan' #TODO response = 'test' #普通消息 else: weixin_id = from_weixin.get('FromUserName') if from_weixin.get('MsgType') == 'text': #print 'Msgtype:text:' text = from_weixin.get('Content') #print 'text:',text i = intent(text) #print '判断:i',i else: i = 5 #检查是否已绑定 try: user = User.objects.get(weixin_id=weixin_id) except User.DoesNotExist: #用户不存在 is_bounded = False is_user_exist = False else: is_bounded = user.is_bounded is_user_exist = True if is_bounded: #绑定用户,消息类型:1.绑定 2.解绑 3.帮助 4.反馈 5. 查自己 area = user.quyu.encode("utf-8") building = user.louhao.encode("utf-8") room = str(user.fangjian) if i == 1: response = '您已绑定(%s),需要重新绑定请点击<a href=\"http://4.dianfei521.sinaapp.com/bound/?wid=%s\">绑定寝室</a>' \ % (area+building+room ,weixin_id) elif i == 2: #仅修改is_bounded user.is_bounded = False user.save() response = '已帮您解绑(%s),需要重新绑定请点击<a href=\"http://4.dianfei521.sinaapp.com/bound/?wid=%s\">绑定寝室</a>' \ % (area+building+room ,weixin_id) elif i == 3: response = help.__doc__ elif i == 4: #TODO response = '反馈建议请点击<a href=\"http://www.baidu.com\">反馈建议</a>' elif i == 5: res_data = crawler_dianfei.crawler_once(area,building,room) response = '您绑定的寝室号:%s\n=============\n最后抄表时间:%s\n剩余电量:%s' \ % (area+building+room,res_data.get('zui_hou_chao_biao_shi_jian')[0],res_data.get('sheng_yu_dian_liang')[0]) else: #未绑定用户,消息类型:1.绑定 2.3.5. 帮助 4.反馈 if i == 1: #先存id,再返回链接 if not is_user_exist: user = User(weixin_id=weixin_id,quyu="1",louhao="1",fangjian="1",is_bounded=False) user.save() response = '绑定寝室请点击<a href=\"http://4.dianfei521.sinaapp.com/bound/?wid=%s\">绑定寝室</a>'\ % weixin_id elif i == 2 or i==3 or i==5 : response = help.__doc__ elif i == 4: #TODO response = '反馈建议请点击<a href=\"\">反馈建议</a>' to_weixin = '''<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>''' to_weixin = to_weixin % (from_weixin['FromUserName'],from_weixin['ToUserName'],str(int(time.time())),'text',response) #to_weixin = response return to_weixin
def responseMsg(request): response = '剩余电量:' from_weixin = handleRequest(request) #关注 if from_weixin.get('MsgType') == 'event': #print 'Msgtype:event:' if from_weixin.get('Event') == 'subscribe': #print 'event:subscribe' content = help.__doc__ response = content #扫码 elif from_weixin.get('Event') == 'SCAN': #print 'event:scan' #TODO response = 'test' #普通消息 else: weixin_id = from_weixin.get('FromUserName') if from_weixin.get('MsgType') == 'text': #print 'Msgtype:text:' text = from_weixin.get('Content') #print 'text:',text i = intent(text) #print '判断:i',i else: i = 5 #检查是否已绑定 try: user = User.objects.get(weixin_id=weixin_id) except User.DoesNotExist: #用户不存在 is_bounded = False is_user_exist = False else: is_bounded = user.is_bounded is_user_exist = True if is_bounded: #绑定用户,消息类型:1.绑定 2.解绑 3.帮助 4.反馈 5. 查自己 6.详细 area = user.quyu.encode("utf-8") building = user.louhao.encode("utf-8") room = str(user.fangjian) if i == 1: response = '您已绑定(%s),需要重新绑定请点击<a href=\"http://m1540057b2.iask.in/bound/?wid=%s\">绑定寝室</a>' \ % (area+building+room ,weixin_id) elif i == 2: #仅修改is_bounded user.is_bounded = False user.save() response = '已帮您解绑(%s),需要重新绑定请点击<a href=\"http://m1540057b2.iask.in/bound/?wid=%s\">绑定寝室</a>' \ % (area+building+room ,weixin_id) elif i == 3: response = help.__doc__ elif i == 4: #TODO response = '反馈建议请点击<a href=\"http://form.mikecrm.com/BhpHew\">反馈建议</a>' elif i == 5: res_data = crawler_dianfei.crawler_once(area,building,room) response = '您绑定的寝室号:%s\n===================\n剩余电量:%s\n最后抄表时间:%s\n===================\n详细抄表和购电请回复:详细\n如有疑问,请回复:帮助' \ %(area+building+room ,res_data.get('sheng_yu_dian_liang').encode('utf-8'),res_data.get('zui_hou_chao_biao_shi_jian').encode('utf-8')) elif i == 6: res_data = crawler_dianfei.crawler_detail(area,building,room) li = res_data.get('chao_biao_ming_xi') chao_biao_ming_xi = '' if li is None: chao_biao_ming_xi = '未查询到抄表明细' else: for item in li: chao_biao_ming_xi += item.get('shu_ju') + ' | ' + item.get('shi_jian')+'\n' if res_data.get('gou_dian_ming_xi') is None: response = '''您绑定的寝室号:%s\n=================== 最近7天抄表:\n-------------------\n数据 | 时间\n-------------------\n%s=================== 最近购电明细:\n-------------------\n%s\n===================\n如有疑问,请输入:帮助''' \ % (area+building+room, chao_biao_ming_xi.encode('utf-8'), '近一月无购电明细' ) else: response = '''您绑定的寝室号:%s\n=================== 最近7天抄表:\n-------------------\n数据 | 时间\n-------------------\n%s=================== 最近购电明细:\n-------------------\n%s\n===================\n如有疑问,请输入:帮助''' \ % (area+building+room, chao_biao_ming_xi.encode('utf-8'), '电量: '+res_data.get('gou_dian_ming_xi').get('chong_zhi').encode('utf-8')\ +'\n电费: '+res_data.get('gou_dian_ming_xi').get('dian_fei').encode('utf-8')\ +'\n时间: '+res_data.get('gou_dian_ming_xi').get('shi_jian').encode('utf-8') ) else: #未绑定用户,消息类型:1.绑定 2.3.5.6 帮助 4.反馈 if i == 1: #先存id,再返回链接 if not is_user_exist: user = User(weixin_id=weixin_id,quyu="1",louhao="1",fangjian="1",is_bounded=False) user.save() response = '绑定寝室请点击<a href=\"http://m1540057b2.iask.in/bound/?wid=%s\">绑定寝室</a>'\ % weixin_id elif i == 2 or i==3 or i==5 or i==6: response = help.__doc__ elif i == 4: #TODO response = '反馈建议请点击<a href=\"\">反馈建议</a>' to_weixin = '''<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>''' to_weixin = to_weixin % (from_weixin['FromUserName'],from_weixin['ToUserName'],str(int(time.time())),'text',response) #to_weixin = response return to_weixin