示例#1
0
def get_vaild_img(request):
    # 获取随机(x,y,z)数字元组
    def get_random_color():
        return (random.randint(0, 255), random.randint(0, 255),
                random.randint(0, 255))

    # 图片背景随机颜色
    img = Image.new('RGB', (140, 32), get_random_color())
    # 画笔对象
    draw = ImageDraw.Draw(img)
    # 字体对象
    font = ImageFont.truetype('app01/static/font/KumoFont.ttf', size=32)
    # 验证码信息
    codes = ''
    # 创建随机验证码图片并且获得验证码字符串codes
    for i in range(5):
        low_letter = chr(random.randint(97, 122))
        up_letter = chr(random.randint(65, 90))
        digit = str(random.randint(0, 9))
        code = random.choice([low_letter, up_letter, digit])
        codes += code
        draw.text((5 + i * 25, 0), code, get_random_color(), font)

    # 保存图片
    f = BytesIO()
    img.save(f, 'png')
    img = f.getvalue()
    # 把验证码和IP信息绑定后存入Redis
    ip = get_ip(request)
    key = "login:{0}".format(ip)
    REDIS.setex(key, codes, 30)  # 30s过期时间
    # 返回图片
    return HttpResponse(img)
示例#2
0
def sendVaildEmail(request):
    try:
        if request.method == "POST":
            data = {"statue": 0}
            try:
                email = request.POST.get("email")
                username = request.POST.get("username")
                key = "register:{0}:{1}".format(username, email)
                print(key)
                # 生成六位验证码
                codes = ""
                for i in range(6):
                    low_letter = chr(random.randint(97, 122))
                    up_letter = chr(random.randint(65, 90))
                    digit = str(random.randint(0, 9))
                    code = random.choice([low_letter, up_letter, digit])
                    codes += code
                print("生成验证码:", codes)
                msg = "亲爱的 {0},您好。您的注册验证码为:{1}".format(username, codes)
                # 通过celery异步发送邮件
                send_email.delay("CTBU学习博客论坛 - 注册验证码", msg, email)
                # 把验证码加入缓存
                REDIS.setex(key, codes, 300)
            except Exception:
                data["statue"] = 1

            return JsonResponse(data)
    except Exception as e:
        print("sendVaildEmail - 发送邮箱验证码,服务器逻辑错误:", e)
        return JsonResponse({
            "statue":
            0,
            "error":
            "sendVaildEmail - 发送邮箱验证码,服务器逻辑错误:" + str(e)
        })
示例#3
0
 def addView(self, request, article_id):
     # 获取IP
     ip = get_ip(request)
     # 判断是否存在访问锁
     lock = self.getViewLock(article_id, ip)
     print("是否存在锁:", lock)
     if not lock:
         s = "visit:%s:totals" % article_id
         # 增加点击量
         REDIS.zincrby("visit_rank", s)
         # 添加访问锁
         self.addViewLock(article_id, ip)
示例#4
0
 def clean_code(self):
     code = self.cleaned_data.get('code')
     username = self.cleaned_data.get("username")
     email = self.cleaned_data.get("email")
     key = "register:{0}:{1}".format(username, email)
     vaild_code = REDIS.get(key)
     if vaild_code:
         print("输入的注册验证码:", code)
         print("正确的注册验证码:", vaild_code)
         print("验证码比较结果:", vaild_code.upper() == code.upper())
     if vaild_code and vaild_code.upper() == code.upper():
         # 删除Redis缓存的验证码
         REDIS.delete(key)
         return code
     else:
         raise ValidationError('验证码错误')
示例#5
0
def login(request):
    # 如果是ajax请求
    if request.is_ajax():
        # 返回信息
        data = {"state": False, "msg": None}
        # 获得正确验证码
        ip = get_ip(request)
        key = "login:{0}".format(ip)
        codes_values = REDIS.get(key)
        print("正确的验证码:", codes_values)
        # 获得POST信息
        username = request.POST.get('username', None)
        pwd = request.POST.get('pwd', None)
        code = request.POST.get('code', None)
        print("input:", code)
        # 验证码 校验
        if not codes_values:
            data["msg"] = "验证码已失效,请点击图片重新获得验证码"
            return JsonResponse(data)
        if codes_values.upper() == code.upper():
            user = auth.authenticate(username=username, password=pwd)
            REDIS.delete(key)
            if user:
                # 修改state
                data["state"] = True
                # 用户组件 登陆,把用户信息保存至request
                auth.login(request, user)
                # 返回json
                return JsonResponse(data)
            else:
                data["msg"] = "用户名或密码错误"
                return JsonResponse(data)
        else:
            data["msg"] = "验证码错误"
            return JsonResponse(data)
    # GET请求
    if request.method == 'GET':
        return render(request, 'login.html')
示例#6
0
 def getViewLock(self, article_id, ip):
     # 查询锁
     s = "visit:{}:lock:{}".format(article_id, ip)
     lock = REDIS.get(s)
     return lock
示例#7
0
 def addViewLock(self, article_id, ip):
     # 加锁
     s = "visit:{0}:lock:{1}".format(article_id, ip)
     REDIS.setex(s, 1, 30)
示例#8
0
 def getTop10View(self):
     lst = REDIS.zscan("visit_rank")[1][::-1][:10]
     res = [(i[0].split(":")[1], int(i[1])) for i in lst]
     return res
示例#9
0
 def getView(self, article_id):
     s = "visit:%s:totals" % article_id
     return int(REDIS.zscore("visit_rank", s)) if REDIS.zscore(
         "visit_rank", s) else None
示例#10
0
 def delView(self, article_id):
     s = "visit:%s:totals" % article_id
     REDIS.zrem("visit_rank", s)
示例#11
0
def addView(article_id):
    s = "visit:%s:totals" % article_id
    # 增加点击量
    REDIS.zincrby("visit_rank", s)
示例#12
0
def getRankView():
    return REDIS.zrevrange("visit_rank", 0, -1)
示例#13
0
def getView(article_id):
    s = "visit:%s:totals" % article_id
    return REDIS.zscore("visit_rank", s)