Esempio n. 1
0
    def post(self, request):
        # 验证码参数
        hashkey = CaptchaStore.generate_key()
        image_url = captcha_image_url(hashkey)

        reset_form = ResetPasswordForm(request.POST)
        if reset_form.is_valid():
            # 表单
            email = request.POST.get('email', "")
            password = request.POST.get("password", "")
            email_code = request.POST.get("email_code", "")
            # 正则表单验证
            email_msg = check_email(email)
            password_msg = check_password(password)
            email_code_msg = check_email_code(email_code)
            re_msgs = []
            if email_msg:
                re_msgs.append(email_msg)
            if password_msg:
                re_msgs.append(password_msg)
            if email_code_msg:
                re_msgs.append(email_code_msg)
            if len(re_msgs) > 0:
                return render(request, 'users/reset.html', {locals()})
            try:
                # 取出验证邮箱对应的用户对象
                user = UserProfile.objects.get(email=email)
            except Exception as e:
                user = None
            try:
                # 取出验证码对应的验证邮箱对象
                user_veri = EmailVerification.objects.get(
                    Q(code=email_code) & Q(type="modify"))
            except Exception as e:
                user_veri = None

            if user:
                if user_veri:
                    user_veri_status = user_veri.status
                    if user_veri_status == 0:
                        user.password = make_password(password)
                        user.save()
                        user_veri.status = 1
                        user_veri.save()
                        return render(request, 'users/reset_success.html',
                                      locals())
                    elif user_veri_status == 1:
                        msg = '该邮箱验证码已激活'
                        return render(request, 'users/reset.html', locals())
                    else:
                        msg = '未知错误'
                        return render(request, 'users/reset.html', locals())
                else:
                    msg = '邮箱验证码错误'
                    return render(request, 'users/reset.html', locals())
            else:
                msg = '邮箱不存在'
                return render(request, 'users/reset.html', locals())
        else:
            return render(request, 'users/reset.html', locals())
Esempio n. 2
0
 def get(self, request, *args, **kwargs):
     new_key = CaptchaStore.pick()
     response = {
         'key':
         new_key,
         'image_url':
         request.build_absolute_uri(location=captcha_image_url(new_key)),
     }
     return Response(response)
Esempio n. 3
0
    def post(self, request):
        hashkey = CaptchaStore.generate_key()
        image_url = captcha_image_url(hashkey)
        register_form = RegisterForm(request.POST)
        if register_form.is_valid():
            username = request.POST.get("username", "")
            password = request.POST.get("password", "")
            email = request.POST.get('email', "")
            nickname = request.POST.get('nickname', "")
            # 正则表单验证
            username_msg = check_username(username)
            password_msg = check_password(password)
            email_msg = check_email(email)
            re_msgs = []
            if username_msg:
                re_msgs.append(username_msg)
            if password_msg:
                re_msgs.append(password_msg)
            if email_msg:
                re_msgs.append(email_msg)
            if len(re_msgs) > 0:
                return render(request, 'users/register.html', locals())

            # 处理数据库中未激活的用户
            self.delete_inactive_user()

            if UserProfile.objects.filter(username=username):
                msg = "该用户已注册"
                return render(request, 'users/register.html', locals())
            elif UserProfile.objects.filter(email=email):
                msg = "该邮箱已注册"
                return render(request, 'users/register.html', locals())
            elif UserProfile.objects.filter(nickname=nickname):
                msg = "该昵称已存在:)"
                return render(request, 'users/register.html', locals())

            user = UserProfile()
            user.username = username
            # 明文加密
            user.password = make_password(password)
            user.email = email
            user.nickname = nickname
            # 用户状态默认未激活
            user.is_active = False
            user.save()

            # 邮件发送处理
            send_email_code(email, "register")
            # 邮件登录入口
            email_access = email_login_access(email)

            return render(request, "users/activate.html", {
                "email": email,
                "email_access": email_access
            })
        else:
            return render(request, 'users/register.html', locals())
Esempio n. 4
0
def users_register(request):
    if request.GET.get('newsn') == '1':
        csn = CaptchaStore.generate_key()
        cimageurl = captcha_image_url(csn)
        return HttpResponse(cimageurl)

    if request.method == 'POST':
        if (not request.POST.get('email')) or (not request.POST.get('password')):
            return HttpResponse(u'账号或密码不能为空')
        try:
            form=RegForm(request.POST)
        except Exception as e:
            print e
            err=u'注册失败,请重试'
            return HttpResponse(err)

        if form.is_valid():
            print 'success'
            try:
                human=True
                email = form.cleaned_data['email']
                password = form.cleaned_data['password']
                user=UserProfile.objects.create(email=email,password=make_password(password),
                                                username=datetime.now().strftime('%Y%m%d%H%M%S'))
                user.save()
                user.backend = 'django.contrib.auth.backends.ModelBackend'  # 指定默认的登录验证方式
                # 验证成功登录
                login(request, user)
                #成功注册后向用户邮箱发送一封验证邮件
                subject='麦子学院邮箱注册验证'
                message='你好,你已注册了麦子学院的账号,请点击链接完成验证http://127.0.0.1:8000/users/email_finish_valid?id=%s'%(user.id)
                from_email='*****@*****.**'
                user.email_user(subject,message,from_email,fail_silently=False)

                return HttpResponse("success")
            except Exception as e:
                print str(e)
                setFormTips(form, "注册失败,请重试")

        else:
            print "register failed"

            if request.POST.get('captcha_1') == "":
                setFormTips(form, "验证码不能为空")

        # 登录失败 返回错误提示
        err = getFormTips(form)
        return HttpResponse(err)
        #return HttpResponse('登录失败')

    form=RegForm()
    return render(request,'users/register.html',locals())
Esempio n. 5
0
    def get(self, request, *args, **kwargs):
        #验证码
        if request.GET.get('newsn') == '1':
            # 生成的数字
            csn = CaptchaStore.generate_key()
            #生成的图片路径
            cimageurl = captcha_image_url(csn)

            return HttpResponse(cimageurl)

        response = super(BlogDetailView, self).get(request, *args, **kwargs)
        # 每看一次阅读数数量加一
        self.object.increase_views()

        return response
Esempio n. 6
0
    def get(self,request):
        #刷新验证码
        if request.GET.get('newsn') == '1':
            # 生成的数字
            csn = CaptchaStore.generate_key()
            #生成的图片路径
            cimageurl = captcha_image_url(csn)

            return HttpResponse(cimageurl)

        # get时展示联系方式

        info=get_object_or_404(Info,isDelete=False)

        form= messageForm()
        return render(request,'contact.html',{'info':info,
                                              'title':'★联系页面',
                                              'form':form})
Esempio n. 7
0
def get_new_captcha():
    Captcha = namedtuple("Captcha", ["hashKey", "imgUrl"])
    key = CaptchaStore.generate_key()
    img_url = captcha_image_url(key)
    return Captcha(hashKey=key, imgUrl=img_url)
Esempio n. 8
0
 def get(self, request):
     # 验证码参数
     hashkey = CaptchaStore.generate_key()
     image_url = captcha_image_url(hashkey)
     register_form = RegisterForm()
     return render(request, 'users/register.html', locals())
Esempio n. 9
0
 def get(self, request):
     hashkey = CaptchaStore.generate_key()
     image_url = captcha_image_url(hashkey)
     return render(request, 'users/reset.html', locals())