Beispiel #1
0
 def post(self, request):
     tag_name = request.POST.get("name")
     if tag_name:
         tag = NewTag.objects.filter(name=tag_name).first()
         if tag:
             return status_code.params_error(message="标签已存在,请不要重复创建")
         NewTag.objects.create(name=tag_name)
         return status_code.result(message="创建成功")
     return status_code.params_error(message="标签名不能为空")
Beispiel #2
0
 def delete(self, request):
     tag_del = QueryDict(request.body)
     tag_id = tag_del.get("tag_id")
     if tag_id:
         tag = NewTag.objects.filter(id=tag_id)
         if tag:
             tag.update(is_delete=True)
             return status_code.result(message="删除成功")
         return status_code.params_error(message="标签不存在")
     return status_code.params_error(message="标签不存在")
Beispiel #3
0
 def put(self, request):
     tag_put = QueryDict(request.body)
     tag_name = tag_put.get("tag_name")
     tag_id = tag_put.get("tag_id")
     if tag_name and tag_id:
         tag = NewTag.objects.filter(name=tag_name).first()
         if tag:
             return status_code.params_error(message="标签已存在,请不要重复创建")
         NewTag.objects.filter(id=tag_id).update(name=tag_name)
         return status_code.result(message="修改成功")
     return status_code.params_error(message="标签不存在")
Beispiel #4
0
 def post(self, request):
     telephone = request.POST.get("telephone")
     sms_captcha = request.POST.get("sms_captcha")
     if telephone and sms_captcha:
         db_telephone = User.objects.filter(telephone=telephone).first()
         if db_telephone:
             mc_sms_captcha = mached.get_key(sms_captcha.lower())
             if mc_sms_captcha == sms_captcha:
                 return status_code.result(message="短信验证成功")
             return status_code.params_error(message="验证码输入错误,请重新输入")
         return status_code.params_error(message="用户不存在,请注册")
     return status_code.params_error(message="请填写信息")
Beispiel #5
0
 def put(self, request):
     data = QueryDict(request.body)
     telephone = data.get("telephone")
     password = data.get("password")
     password_repeat = data.get("password_repeat")
     if 6 <= len(
             password) <= 25 and password and password_repeat == password:
         if password == password_repeat:
             user = User.objects.filter(telephone=telephone).first()
             user.set_password(password)
             user.save()
             login(request, user)
             return status_code.result(message="密码修改完成")
         return status_code.params_error(message="两次输入密码不一致")
     return status_code.params_error(message="密码长度错误")
Beispiel #6
0
 def post(self, request):
     form = NewsForm(request.POST)
     if form.is_valid():
         author = request.user
         title = form.cleaned_data.get("title")
         desc = form.cleaned_data.get("desc")
         tag_id = form.cleaned_data.get("tag_id")
         tag = NewTag.objects.filter(id=tag_id).first()
         img_url = form.cleaned_data.get("thumbnail_url")
         content = form.cleaned_data.get("content")
         if tag:
             NewsPub.objects.create(title=title, desc=desc, tag=tag, img_url=img_url, content=content,
                                    author=author)
             return status_code.result(message="发布成功")
         return status_code.params_error(message="标签不能为空")
     return status_code.params_error(message=form.get_error())
Beispiel #7
0
 def post(self, request):
     form = CommentsForm(request.POST)
     if form.is_valid():
         news_id = form.cleaned_data.get("news_id")
         content = form.cleaned_data.get("content")
         news = NewsPub.objects.filter(id=news_id).first()
         if news:
             # 保存评论至数据库
             comments = NewsComments.objects.create(news=news,
                                                    content=content,
                                                    author=request.user)
             # 把新增评论序列化
             serializers = CommentSerializers(comments)
             return status_code.result(data=serializers.data)
         return status_code.params_error(message="新闻不存在")
     return status_code.params_error(message=form.get_error())
Beispiel #8
0
def news_comments(request):
    news_id = int(request.GET.get("news_id"))
    print(news_id)
    news = NewsPub.objects.filter(id=news_id, is_delete=False).first()
    if news:
        # 反向查询获得指定新闻的所有评论
        comments_all = news.comments.all()
        # 序列化多条数据时需要参数many=True
        serializers = CommentSerializers(comments_all, many=True)
        return status_code.result(data=serializers.data)
    return status_code.params_error(message="新闻不存在")
Beispiel #9
0
    def post(self, request):
        form = RegisterForm(request.POST)
        # print(form)

        if form.is_valid() and form.check_captcha():
            # 获取前端数据
            telephone = form.cleaned_data.get("telephone")
            password = form.cleaned_data.get("password")
            username = form.cleaned_data.get("username")
            user = User.objects.filter(telephone=telephone).first()
            if user:  # 判断用户是否存在
                # return JsonResponse({"code": 0, "msg": "该手机号码已注册,请登录"})
                return status_code.params_error(message="该手机号码已注册,请登录")
            user = User.objects.create_user(telephone, username, password)
            login(request, user)  # 注册成功后自动登录
            request.session.set_expiry(0)  # 设置session在关闭浏览器就过期
            return status_code.result(message="注册成功")
            # return JsonResponse({"code": 1, "msg": "注册成功"})
        # return JsonResponse({"code": 0, "msg": form.get_error()})
        return status_code.params_error(message=form.get_error())
Beispiel #10
0
 def post(self, request):
     form = LoginForm(request.POST)
     if form.is_valid():
         telephone = form.cleaned_data.get("telephone", None)
         password = form.cleaned_data.get("password", None)
         remember = form.cleaned_data.get("remember", None)
         # print(telephone, password)
         user = authenticate(username=telephone, password=password)
         if user:
             # 保持登录状态
             login(request, user)
             if remember:
                 # 如果用户勾选记住,session有效期设置为默认值14天,单位为秒
                 request.session.set_expiry(None)
             else:
                 # 如果不勾选记住,session在关闭浏览器就过期
                 request.session.set_expiry(0)
             # return JsonResponse({"code": 1, "msg": "登录成功"})
             return status_code.result(message="登录成功")
         # return JsonResponse({"code": 0, "msg": "账号或密码错误"})
         return status_code.params_error(message="账号或密码错误")
     # return JsonResponse(form.get_error())
     return status_code.params_error(message=form.get_error())