示例#1
0
def rent_bicycle(request):

    bicycle_id = request.data.get('bicycle_id')
    open_id = request.data.get('open_id')

    if not cache.hget(bicycle_id, open_id):
        end_time = request.data.get("end_time")
        cost = (end_time - cache.hget(bicycle_id, open_id)) * 1


    else:
        cache.hput(bicycle_id, open_id, request.data.get('start_time'))
        return 'successfully!'
示例#2
0
def AppAction(request, target, action):
    ipaddr = request.META.get(G_REMOTE_ADDR)
    sessionid = request.COOKIES.get(G_SESSIONID, None)
    mem_addr = redis_pool.hget(sessionid, G_IPADDR)

    #     print "action from signid ",data,' ipaddr ',ipaddr
    print "action access cookies", sessionid, request.COOKIES
    if not mem_addr or cmp(mem_addr, ipaddr):
        print "not access"
        return HttpReturn(UnAuth)
    # ## 更新登录状态时间
    redis_pool.expire(sessionid, settings.SESSION_COOKIE_AGE)

    if action in actionFunc:
        return actionFunc[action](request, redis_pool.hget(sessionid, G_UUID),
                                  target)
    else:
        return HttpReturn(UnkownAction)
示例#3
0
def AppQuery(request, action):
    #     data = json.loads(cache.get(token))
    ipaddr = request.META.get(G_REMOTE_ADDR)
    sessionid = request.COOKIES.get(G_SESSIONID, None)
    mem_addr = redis_pool.hget(sessionid, G_IPADDR)

    #     print " data " , redis_pool.hgetall(sessionid)
    #     print "query access cookies",sessionid,request.COOKIES
    #     sid  = redis_pool.hget(token, G_CSRFTOKEN)
    if not sessionid or not mem_addr or cmp(mem_addr, ipaddr):
        return HttpReturn(UnAuth)

    if action == 'logout':

        #         print "request", request.path
        ipobj, state = IpAddress.objects.get_or_create(ipaddr=ipaddr)
        if 'dev' in request.path:
            obj = Devices.objects.get(uuid=redis_pool.hget(sessionid, G_UUID))
            DevicesLoginHistory.objects.create(user=obj,
                                               inout=False,
                                               ipaddr=ipobj,
                                               optime=timezone.now())
        else:
            obj = AppUser.objects.get(uuid=redis_pool.hget(sessionid, G_UUID))
            AppUserLoginHistory.objects.create(user=obj,
                                               inout=False,
                                               ipaddr=ipobj,
                                               optime=timezone.now())
#         redis_pool.expire(token, 1)
        redis_pool.delete(sessionid)
        return ReturnOK

    user = AppUser.objects.get(uuid=redis_pool.hget(sessionid, G_UUID))
    # ## 更新登录状态时间
    redis_pool.expire(sessionid, settings.SESSION_COOKIE_AGE)
    if action in QueryFunc:
        return QueryFunc[action](request, user)
    else:
        return HttpReturn(UnkownAction)
示例#4
0
def AppResetPwd(request, Md5sum, newpass, smscode):

    phone = redis_pool.hget(Md5sum, G_PHONE)
    mysms = redis_pool.hget(Md5sum, 'sms')

    if not phone or not smscode:
        return HttpReturn(UnAuth)
    if cmp(mysms, smscode):
        return HttpReturn(CaptchaError)

    # 已经验证了,清除内存
    redis_pool.hdel(Md5sum, G_PHONE)
    redis_pool.hdel(Md5sum, 'sms')

    try:
        user = AppUser.objects.get(phone=phone)
        user.key = make_password(newpass)
        user.save()
    except:
        pass

    return ReturnOK
示例#5
0
 def get_queryset(self):
     options = {
         'active': True,
     }
     if settings.APPLICATION_CACHE:
         tag = Commercial.get_commercial_tag()
         if cache.hexists(tag, options):
             data = json.loads(cache.hget(tag, options))
         else:
             data = self.get_list()
             cache.hset(Commercial.get_commercial_tag(), options, json.dumps(data))
     else:
         data = self.get_list()
     return data
示例#6
0
def ChangeDevName(request, newname):

    devuuid = redis_pool.hget(request.COOKIES.get(G_SESSIONID, None), G_UUID)

    if not devuuid:
        return HttpReturn(UnAuth)
    try:
        devobj = Devices.objects.get(uuid=devuuid)
    except:
        return HttpReturn(TargetNotExists)
    else:
        devobj.name = newname
        devobj.save()
        return ReturnOK
示例#7
0
    def get_queryset(self):
        options = {
            'brand_id': self.request.GET.get('brand_id', ''),
            'brand__entry': self.request.GET.get('brand__entry', ''),
            'name': self.request.GET.get('name__icontains', ''),
            'page': self.request.GET.get('page', '')
        }
        if settings.APPLICATION_CACHE and self.cache_status:
            str(self.request.GET.get('brand__entry', ''))

            if cache.hexists(Commercial.get_commercial_tag(), options):
                data = json.loads(cache.hget('commercial_list', options))
            else:
                qs = super(CommercialListView, self).get_queryset()
                data = self.get_list(qs)
                cache.hset(Commercial.get_commercial_tag(), options, json.dumps(data))
        else:
            qs = super(CommercialListView, self).get_queryset()
            data = self.get_list(qs)
        return data
示例#8
0
def QueryCert(request, ipaddr):
    sessionid = request.COOKIES.get(G_SESSIONID, None)
    if not redis_pool.hget(sessionid, G_UUID):
        return HttpReturn(UnAuth)

    # ## 更新登录状态时间
    redis_pool.expire(sessionid, settings.SESSION_COOKIE_AGE)

    retdict = {}
    retdict[G_OK] = True
    try:
        srv = SrvList.objects.get(ipaddr=ipaddr)
    except (ObjectDoesNotExist, IndexError) as e:
        retdict['cert'] = None
    else:
        #retdict['cert'] = srv.pubkey
        # 小机端不支持zlib
        #         print "cert is ",srv.cert
        #         import zlib
        retdict['cert'] = str(srv.cert)

    return HttpReturn(json.dumps(retdict))
示例#9
0
def IotPing(request):
    sessionid = request.COOKIES.get(G_SESSIONID, None)
    if not redis_pool.hget(sessionid, G_UUID):
        return HttpReturn(UnAuth)
    redis_pool.expire(sessionid, settings.SESSION_COOKIE_AGE)
    return ReturnOK
示例#10
0
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)