Beispiel #1
0
 def delete(id):
     if not id:
         return ResultCode.params_error()
     db_tag_by_id = Comments.objects.filter(id=int(id))
     if not db_tag_by_id.exists():
         return ResultCode.params_error(msg="该评论不存在~")
     # 模拟删除
     db_tag_by_id.update(is_del=True)
Beispiel #2
0
 def delete(id):
     if not id:
         return ResultCode.params_error()
     db_category_by_id = Category.objects.filter(id=int(id))
     if not db_category_by_id.exists():
         return ResultCode.params_error(msg="该分类不存在~")
     # 模拟删除
     db_category_by_id.update(is_del=True)
Beispiel #3
0
 def delete(id):
     """
     删除数据
     :param id:
     :return:
     """
     if not id:
         return ResultCode.params_error()
     db_post_by_id = Post.objects.filter(pid=id)
     if not db_post_by_id.exists():
         return ResultCode.params_error(msg="该文章不存在~")
     # 模拟删除
     db_post_by_id.update(is_del=True)
Beispiel #4
0
def tags_add(request):
    """
    添加新标签
    :param request:
        name: 新标签名
    :return:
    """
    name = request.POST.get('name', '').lower()
    if not name:
        return ResultCode.params_error()
    db_tag = Tags.objects.filter(tname=name)
    if db_tag.exists():
        return ResultCode.params_error(msg="该标签已存在~")
    name = Tags.objects.create(tname=name)
    return ResultCode.ok()
Beispiel #5
0
def category_add(request):
    """
    添加新分类
    :param request:
        name: 新分类名
    :return:
    """
    name = request.POST.get('name', '').lower()
    if not name:
        return ResultCode.params_error()
    db_category = Category.objects.filter(cname=name)
    if db_category.exists():
        return ResultCode.params_error(msg="该分类已存在~")
    name = Category.objects.create(cname=name)
    return ResultCode.ok()
Beispiel #6
0
 def post(self, request):
     username = request.POST.get('username')
     password = request.POST.get('password')
     user = authenticate(username=username, password=password)
     if user:
         login(request, user)
         return ResultCode.ok()
     return ResultCode.params_error()
Beispiel #7
0
def tags_edit(request):
    """
    修改标签
    :param request:
        id: 需要修改的标签id
        name: 新标签名
    :return:
    """
    id = int(request.POST.get('id', 0))
    name = request.POST.get('name', '').lower()
    if not all([id, name]):
        return ResultCode.params_error()
    db_tag_by_id = Tags.objects.filter(id=int(id))
    if not db_tag_by_id.exists():
        return ResultCode.params_error(msg="该标签不存在~")
    db_tag_by_name = Tags.objects.filter(tname=name)
    if db_tag_by_name.exists():
        return ResultCode.params_error(msg="该标签已存在~")
    name = db_tag_by_id.update(tname=name)
    return ResultCode.ok()
Beispiel #8
0
def category_edit(request):
    """
    修改分类
    :param request:
        id: 需要修改的分类id
        name: 新分类名
    :return:
    """
    id = int(request.POST.get('id', 0))
    name = request.POST.get('name', '').lower()
    if not all([id, name]):
        return ResultCode.params_error()
    db_category_by_id = Category.objects.filter(id=int(id))
    if not db_category_by_id.exists():
        return ResultCode.params_error(msg="该分类不存在~")
    db_category_by_name = Category.objects.filter(cname=name)
    if db_category_by_name.exists():
        return ResultCode.params_error(msg="该分类已存在~")
    name = db_category_by_id.update(cname=name)
    return ResultCode.ok()
Beispiel #9
0
def post_write(request):
    """
    发布文章
    :param request:
        title: 标题
        desc: 描述
        content: 内容
        category_id: 分类id
        tags_list: 标签id list
        thumbnail: 封面
    :return:
    """
    title = request.POST.get('title')
    desc = request.POST.get('desc')
    content = request.POST.get('content')
    category_id = request.POST.get('category')
    tags_list = request.POST.getlist('tags[]')
    thumbnail = request.POST.get('thumbnail')
    if not all([title, desc, content, category_id, thumbnail]):
        return ResultCode.params_error(msg="还有必填项未填写~")
    db_category = Category.objects.filter(id=int(category_id))
    post = Post.objects.create(
        ptitle=title,
        pdesc=desc,
        pcontent=content,
        pcategory=db_category.first(),
        pthumbnail=thumbnail,
        puser=User.objects.first(),
    )
    for item in tags_list:
        if not item:
            continue
        db_tag = Tags.objects.filter(id=int(item))
        if db_tag.exists():
            post.ptags.add(db_tag.first())
            post.save()
    return ResultCode.ok()