예제 #1
0
    def post(self, request):
        try:
            json_data = request.body
            if not json_data:
                return to_json_data(errno=Code.PARAMERR,
                                    errmsg=error_map[Code.PARAMERR])  # 参数错误

            # 5. 将数据转化为字典
            dict_data = json.loads(json_data)

        except Exception as e:
            return to_json_data(errno=Code.UNKOWNERR,
                                errmsg=error_map[Code.UNKOWNERR])  # 未知错误

        en_username = dict_data.get("login_name")  # 加密的登录账号
        en_password = dict_data.get("password")  # 加密的登录账号
        secret_key = dict_data.get("key")  # 加密的登录账号
        decrypt = DeAesCrypt(secret_key, "Pkcs7")
        de_username = decrypt.decrypt_aes(en_username)
        de_password = decrypt.decrypt_aes(en_password)
        dict_data["login_name"] = de_username
        dict_data["password"] = de_password

        form = LoginForm(data=dict_data, request=request)  # 没有使用加密的
        # 7. 数据校验成功返回数据给前端
        if form.is_valid():

            return to_json_data(errmsg="恭喜您登录成功")

        else:
            err_str = error_message(form)
            return to_json_data(errno=Code.PARAMERR, errmsg=err_str)
예제 #2
0
    def post(self, request):

        # 4. 接收前端传来的数据
        try:
            json_data = request.body
            if not json_data:
                return to_json_data(errno=Code.PARAMERR,
                                    errmsg=error_map[Code.PARAMERR])  # 参数错误

            # 5. 将数据转化为字典
            dict_data = json.loads(json_data)

        except Exception as e:
            return to_json_data(errno=Code.UNKOWNERR,
                                errmsg=error_map[Code.UNKOWNERR])  # 未知错误

        # 6. 将数据和request传递给LoginForm表单进行验证
        # form = LoginForm(data=dict_data, request=request)  # 没有使用加密的
        form = SlideLoginForm(data=dict_data, request=request)  # 没有使用加密的

        # 7. 数据校验成功返回数据给前端
        if form.is_valid():

            return to_json_data(errmsg="恭喜您登录成功")

        else:
            err_str = error_message(form)
            return to_json_data(errno=Code.PARAMERR, errmsg=err_str)
예제 #3
0
    def post(self, request):
        gt = GeetestLib(user_config.GEETEST_ID, user_config.GEETEST_KEY)

        # 获取前端传来的登录信息
        en_username = request.POST.get("username")  # 加密的登录账号
        en_password = request.POST.get("password")  # 加密的登录账号
        secret_key = request.POST.get("key")  # 加密的登录账号
        remember = request.POST.get("remember")  # 加密的登录账号

        # 前端传来的geetest的参数
        challenge = request.POST.get(gt.FN_CHALLENGE, "")
        validate = request.POST.get(gt.FN_VALIDATE, "")
        seccode = request.POST.get(gt.FN_SECCODE, "")
        status = request.session[gt.GT_STATUS_SESSION_KEY]
        user_id = request.session["user_id"]

        # 判断滑动验证码是否登录成功
        if status:
            result = gt.success_validate(challenge, validate, seccode, user_id)
        else:
            result = gt.failback_validate(challenge, validate, seccode)

        # 滑动验证码校验成功后校验用户登录信息
        if result:

            # 解密用户名和登录密码
            decrypt = DeAesCrypt(secret_key, "Pkcs7")
            de_username = decrypt.decrypt_aes(en_username)
            de_password = decrypt.decrypt_aes(en_password)
            login_info = {
                "username": de_username,
                "password": de_password,
                "remember": remember,
            }
            form = LoginForm(login_info, request=request)
            if form.is_valid():

                return to_json_data(errno=Code.OK, errmsg="登录成功")

            else:
                err_str = error_message(form)

                return to_json_data(errno=Code.PARAMERR, errmsg=err_str)
        else:
            return to_json_data(errno=Code.LOGINERR, errmsg="验证校验失败")
예제 #4
0
    def post(self, requests):
        try:
            json_data = requests.body
            if not json_data:
                return to_json_data(errno=Code.PARAMERR,
                                    errmsg=error_map[Code.PARAMERR])
            dict_data = json.loads(json_data)
        except Exception as e:
            return to_json_data(errno=Code.UNKOWNERR,
                                errmsg=error_map[Code.UNKOWNERR])

        form = ResetPasswordForm(dict_data)
        if form.is_valid():
            return to_json_data(errmsg="修改密码成功")

        else:
            err_str = error_message(form)
            return to_json_data(errno=Code.PARAMERR, errmsg=err_str)
예제 #5
0
    def post(self, request, article_id):

        # 要先判断是否用户已经登录(必须登录后才能进行评论)
        if not request.user.is_authenticated:
            return to_json_data(errno=Code.SESSIONERR,
                                errmsg=error_map[Code.SESSIONERR])

        # 2. 获取前端传来的参数
        try:
            json_data = request.body
            if not json_data:
                return to_json_data(errno=Code.PARAMERR, errmsg="参数错误,请重新输入")
            dict_data = json.loads(json_data)
        except Exception as e:
            return to_json_data(errno=Code.UNKOWNERR,
                                errmsg=error_map[Code.UNKOWNERR])

        # 3. 获取前端传来的回复内容
        content = dict_data.get("content")

        # 4. 判断content是否为空
        if not content:
            return to_json_data(errno=Code.PARAMERR, errmsg="评论内容为空,请输入!")

        # 5. 获取父评论id
        parent_id = dict_data.get("parent_id")

        try:
            if parent_id:
                parent_id = int(parent_id)
                # 判断文章id和父评论id是否和传来过同时满足
                if not _models.Comments.objects.only("id").filter(
                        is_delete=False, id=parent_id,
                        article_id=article_id).exists():
                    return to_json_data(errno=Code.PARAMERR,
                                        errmsg=error_map[Code.UNKOWNERR])
        except Exception as e:
            logger.error("父评论id异常:{}".format(e))
            return to_json_data(errno=Code.PARAMERR, errmsg="父评论ID参数异常")

        # 添加评论数
        article = _models.Articles.objects.only("id").filter(
            id=article_id).first()
        article.add_comment_num()

        # 6. 保存到数据库
        article_comment = _models.Comments()  # 获取评论实例
        article_comment.content = content  # 保存评论内容
        article_comment.article_id = article_id  # 保存评论文章id
        article_comment.author = request.user
        article_comment.parent_id = parent_id if parent_id else None  # 判断是否为空

        article_comment.save()  # 保存实例

        return to_json_data(
            data={
                "data": article_comment.to_dict_data(),
                "count": article.comment_num  # 文章评论数量
            })
예제 #6
0
    def post(self, request):

        # 4. 获取前端传来的数据
        try:
            json_data = request.body
            if not json_data:
                return to_json_data(errno=Code.PARAMERR,
                                    errmsg=error_map[Code.PARAMERR])
            dict_data = json.loads(json_data)
        except Exception as e:
            # logger.info("注册错误:{}".format(e)) # 调试用
            return to_json_data(errno=Code.UNKOWNERR,
                                errmsg=error_map[Code.UNKOWNERR])

        # 5. 将数据转化为字典并传入到RegisterForm表单里面
        form = RegisterForm(data=dict_data)

        # 6. RegisterForm验证成功后获取到校验后的的数据
        if form.is_valid():
            username = form.cleaned_data.get("username")
            mobile = form.cleaned_data.get("mobile")
            password = form.cleaned_data.get("password")
            password_repeat = form.cleaned_data.get("password_repeat")

            # 7. 判断密码和确认密码是否一致
            if password != password_repeat:
                return to_json_data(errmsg="密码前后不一致,请重新输入!")

            # 8. 将用户信息保存到数据库
            user = models.Users.objects.create_user(username=username,
                                                    password=password,
                                                    mobile=mobile)

            # 9. 跳转到登录页面
            login(request, user)

            # 返回数据到前端
            return to_json_data(errmsg="恭喜您注册成功")

        else:
            err_str = error_message(form)

            return to_json_data(errno=Code.PARAMERR, errmsg=err_str)
예제 #7
0
    def post(self, request):
        # 3. 获取前端传来的数据
        json_data = request.body

        # 4. 将数据转化为字典
        dict_data = json.loads(json_data)

        # 5. 将数据传递给SmsCodeForm表单进行校验
        form = SmsCodeForm(data=dict_data)

        # 6. 校验成功处理方式
        if form.is_valid():
            # 7. 获取校验后的数据
            mobile = form.cleaned_data.get("mobile")

            # 8. 生成短信验证码
            sms_text = "%06d" % random.randint(0, 999999)
            #  9. 将短信验证码和和过期时间保存到redis中
            mobile_captcha = MobileCaptcha(
                mobile,
                sms_text,
                alias="verify_code",
                expire=contains.SMS_CODE_EXPIRE_TIME,
                re_expire=contains.SMS_REPEAT_EXPIRE_TIME)
            mobile_captcha.captcha_cache()

            # 使用celery异步处理短信发动任务
            print(sms_text)
            send_sms_code.delay(mobile, sms_text,
                                contains.SMS_CODE_EXPIRE_TIME,
                                contains.SMS_TEMPLATE)
            return to_json_data(errno=Code.OK, errmsg="短信验证码发送成功")

        # 校验未通过
        else:
            err_info = error_message(form)
            return to_json_data(errno=Code.PARAMERR, errmsg=err_info)
예제 #8
0
    def get(self, request):
        # 1. 从数据库中获取数据
        banner_queryset_list = _models.Banner.objects.select_related(
            "article").only("image_url", "article__id",
                            "article__title").filter(is_delete=False).order_by(
                                "priority", "-update_time",
                                "-id")[0:contains.BANNER_IMAGE_NUMBER]

        banner_list = []
        # 2. 数据序列化
        for banner_queryset in banner_queryset_list:
            banner_list.append({
                "image_url": banner_queryset.image_url,
                "article_id": banner_queryset.article.id,
                "article_title": banner_queryset.article.title,
            })

        data = {"banner_list": banner_list}

        # 3. 返回数据到前端
        return to_json_data(data=data)
예제 #9
0
    def get(self, request):
        # 1. 获取前端传来的数据文章类型id
        try:
            tag_id = int(request.GET.get("tag_id", 0))
        except Exception as e:
            logger.error("文章标签id参数错误:{}".format(e))
            tag_id = 0

        # 2. 获取前端传来的数据页码编号 page_num
        try:
            page_num = int(request.GET.get("page", 1))
        except Exception as e:
            logger.error("文章页码参数错误:{}".format(e))
            page_num = 1

        # 3. 从数据库中获取文章表签id为tag_id的数据
        article_queryset_total = _models.Articles.objects.select_related(
            "author", "tag").only("id", "title", "digest", "update_time",
                                  "clicks", "image_url", "author__username",
                                  "tag__name").filter(is_delete=False)

        article_tag_queryset = article_queryset_total.filter(is_delete=False, tag_id=tag_id) or \
                               article_queryset_total.filter(is_delete=False)

        # 4. 对数据尽心分页
        pagen = Paginator(article_tag_queryset,
                          contains.PER_PAGE_DATA_NUMBER)  # 传递待分页对象 、每页多少个

        # 5. 返回当页数据
        try:
            article_list = pagen.page(page_num)
        except EmptyPage:
            logger.error("访问页数超过总页数")
            article_list = pagen.page(pagen.num_pages)

        # 6. 数据序列化
        article_per_page_list = []
        for article in article_list:
            article_per_page_list.append({
                "id":
                article.id,
                "title":
                article.title,
                "digest":
                article.digest,
                "update_time":
                article.update_time.strftime("%Y年%m月%d日"),
                "clicks":
                article.clicks,
                "image_url":
                article.image_url,
                "author":
                article.author.username,
                "comment_num":
                article.comment_num,
                "tag_name":
                article.tag.name if article.tag else "",
            })

        data = {
            "total_page": pagen.num_pages,
            "article": article_per_page_list
        }
        return to_json_data(data=data)