def set_cache_result(self): if settings.APPLICATION_CACHE and self.CACHE_RESULT_TAG is not None: try: self.search_results = cache.hgetall(self.CACHE_RESULT_TAG) except Exception: # TODO: Log this # Error trying to get all the cache search pass
def wrapper(request, account, pwd): sessionid = request.COOKIES.get(G_SESSIONID, None) resdict = redis_pool.hgetall(sessionid) addr = request.META.get(G_REMOTE_ADDR) if sessionid and resdict and (addr == resdict[G_IPADDR]) \ and (G_ACCOUNT in resdict) and (account in resdict[G_ACCOUNT]): # print "get return from redis " # print "you cookies ",request.COOKIES d = json.loads(resdict["res"]) d["time"] = str(int(time.time())) redis_pool.expire(sessionid, settings.SESSION_COOKIE_AGE) return HttpReturn(json.dumps(d)) else: return func(request, account, pwd)
def AppVerifyPhone(request, Md5sum, smscode): adict = redis_pool.hgetall(Md5sum) if not adict or not all(x in adict for x in [G_PHONE, 'sms']): return HttpReturn(UnAuth) if cmp(adict.get('sms', None), smscode): return HttpReturn(CaptchaError) # print "verify phone adict is", adict ipobj, ok = IpAddress.objects.get_or_create( ipaddr=request.META.get(G_REMOTE_ADDR)) # 注册成功之后,添加注册记录,并添加一条 /uuid/# 的记录到mqtt_acl if G_REGISTER in adict: AppUser.objects.create(email=adict[G_EMAIL], phone=adict[G_PHONE], key=adict[G_KEY], uname=adict[G_UNAME], uuid=adict[G_UUID], regtime=timezone.now(), regip=ipobj, phone_active=True, data={'null': 'null'}) topic, ok = MqttTopics.objects.get_or_create(topic="/%s/#" % adict[G_UUID]) MqttAcl.objects.get_or_create(allow=1, ipaddr=None, clientid=None, app_id=adict[G_UUID], access=3, topic=topic) else: obj = AppUser.objects.get(phone=adict[G_PHONE]) obj.phone_active = True obj.save() # 已经验证了,清除内存 # redis_pool.expire(Md5sum,1) redis_pool.delete(Md5sum) return ReturnOK
def AppUserChange(body, *args): user, _ = args oldpass = body.get(G_OLDPASS, None) if not oldpass: return HttpReturn(UnAuth) # if cmp(oldpass,user.key): #### 修改信息必须要用原密码 if not check_password(oldpass, user.key): return HttpReturn(PwdError) newpass = body.get(G_NEWPASS, None) # if G_PHONE in body: if G_RESCODE not in body or G_SMS not in body: return HttpReturn(UnAuth) adict = redis_pool.hgetall(body.get(G_RESCODE, '')) if not adict or not all(x in adict for x in [G_PHONE, G_SMS]): return HttpReturn(UnAuth) if cmp(adict.get(G_PHONE, None), body.get(G_PHONE)) or\ cmp(adict.get(G_SMS, None), body.get(G_SMS)): return HttpReturn(CaptchaError) for k, v in body.items(): if k in [G_EMAIL, 'nickname', G_PHONE, 'sex', G_UNAME]: # print k, " set value ",type(v) setattr(user, k, v) # if k == G_PHONE: ### 更改了手机要重新激活帐号 # # setattr(user,"phone_active",False) if newpass: # print "set newpass ",newpass user.key = make_password(newpass) user.save() return ReturnOK
def AcceptBindLink(request, user, uuid): shared_keys = redis_pool.hgetall(uuid) # print "shared_keys",shared_keys,"type",type(shared_keys) if not shared_keys: return HttpReturn(UnAuth) if user == shared_keys[G_APPID]: return HttpReturn(UnAuth) # print "shared redis key",shared_keys,'type is',type(shared_keys) # print "shared topics",type(shared_keys[G_TOPICS]),shared_keys[G_TOPICS] # topic,ok = MqttTopics.objects.get_or_create(topic="/%s/#" % # user.uuid.hex) # SharedDevList.objects.get_or_create(host_id = shared_keys[G_APPID], # guest = user, # sdevice_id = shared_keys[G_DEVID], # topics = shared_keys[G_TOPICS]) tdict = json.loads(shared_keys[G_TOPICS]) shared_keys[G_TOPICS] = tdict # print "topic dict is ",tdict for item in tdict: # topic,ok = MqttTopics.objects.get_or_create(topic=item) MqttAcl.objects.get_or_create(allow=1, ipaddr=None, clientid=None, app_id=user, access=3, topic="/%s/%s/#" % (shared_keys[G_DEVID], item)) # 记录分享者,接受者,设备uuid,主题 # redis_pool.expire(uuid,1) redis_pool.delete(uuid) # for k,v in shared_keys.items(): # redis_pool.hdel(uuid,key) return HttpReturn(json.dumps({G_OK: True, G_DATA: shared_keys}))
def AppSendSms(request, account): SENDCOUNT = 'sendcount' # phone = redis_pool.hget(account, G_PHONE) adict = redis_pool.hgetall(account) # print "all key is",adict if G_PHONE not in adict: return HttpReturn(UnAuth) # redis_pool.hdel(account, G_PHONE) # print "phone number ", phone ### 同一号24小时只能发送三次短信. ### sendnum = redis_pool.hget(adict[G_PHONE], SENDCOUNT) # print "gettxt from redis", sendnum if sendnum and int(sendnum) == 3: return HttpReturn(SmsOverError) sendtime = redis_pool.hget(adict[G_PHONE], 'lastime') if sendtime and (time.time() - int(sendtime)) < settings.SESSION_COOKIE_AGE / 10: return HttpReturn(SmsIntervalError) # ipaddr = request.META.get('REMOTE_ADDR') ipobj, ok = IpAddress.objects.get_or_create( ipaddr=request.META.get(G_REMOTE_ADDR)) oldaddr = redis_pool.hget(account, G_IPADDR) if cmp(ipobj.ipaddr, oldaddr): return HttpReturn(IpAddrError) try: (sms, state) = sendsms.SendSMS(adict[G_PHONE]) except HTTPError: return HttpReturn(InternalError) except IndexError: SmsErrorLog.objects.create(-100, ipobj, timezone.now, adict[G_PHONE]) return HttpReturn(InternalError) if state == 0: redis_pool.expire(adict[G_PHONE], settings.SESSION_COOKIE_AGE) redis_pool.hset(adict[G_PHONE], 'lastime', int(time.time())) # redis_pool.expire(phone,settings.SESSION_COOKIE_AGE) if redis_pool.hget(adict[G_PHONE], SENDCOUNT): redis_pool.hincrby(adict[G_PHONE], SENDCOUNT, 1) else: redis_pool.hset(adict[G_PHONE], SENDCOUNT, 1) resetcode = hashlib.md5(adict[G_PHONE] + str(time.time())).hexdigest().upper() if G_REGISTER in adict: for (k, v) in adict.items(): redis_pool.hset(resetcode, k, v) redis_pool.hmset(resetcode, {G_PHONE: adict[G_PHONE], 'sms': sms}) # print "reset code dict is",redis_pool.hgetall(resetcode) redis_pool.expire(resetcode, settings.SESSION_COOKIE_AGE) redis_pool.hdel(account, G_PHONE) # 删除这个键,一次有效 return HttpReturn(json.dumps({G_OK: True, 'rescode': resetcode})) else: if state in ErrDict: errobj, ok = SmsErrorTable.objects.get_or_create(errcode=state, msg=ErrDict.get( state, u"未知错误")) SmsErrorLog.objects.create(errcode=errobj, ipaddr=ipobj, addtime=timezone.now(), phone=adict[G_PHONE]) if state in [10006, 10007, 10005]: return HttpReturn( json.dumps({ G_ERR: "OtherError", G_MSG: ErrDict[state] })) else: return HttpReturn(InternalError)