コード例 #1
0
ファイル: views.py プロジェクト: ruanxw/mybbs
def add_article(request, *args, **kwargs):
    """
    添加文章
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    if request.method == "GET":
        form = ArticleForm(request=request)
        return render(request, 'backend_add_article.html', {'form': form})
    elif request.method == 'POST':
        form = ArticleForm(request=request, data=request.POST)
        user = request.user
        if form.is_valid():
            with transaction.atomic():
                tags = form.cleaned_data.pop('tags')
                content = form.cleaned_data.pop('content')
                content = XSSFilter().process2(content)
                soup = BeautifulSoup(content, 'html.parser')
                desc = soup.text[0:220] + "..."
                obj = models.Article.objects.create(**form.cleaned_data,
                                                    user=user,
                                                    desc=desc)
                models.ArticleDetail.objects.create(content=content,
                                                    article=obj)
                tag_list = []
                for tag_id in tags:
                    tag_id = int(tag_id)
                    tag_list.append(
                        models.Article2Tag(article_id=obj.nid, tag_id=tag_id))
                models.Article2Tag.objects.bulk_create(tag_list)
            return redirect('/backend/article-0-0.html')
        else:
            return render(request, 'backend_add_article.html', {'form': form})
    else:
        return redirect('/')
コード例 #2
0
ファイル: user.py プロジェクト: w7374520/EdmureBlog
def add_article(request):
    """
    添加文章
    :param request:
    :return:
    """
    if request.method == 'GET':
        form = ArticleForm(request=request)
        return render(request, 'backend_add_article.html', {'form': form})
    elif request.method == 'POST':
        form = ArticleForm(request=request, data=request.POST)
        if form.is_valid():

            with transaction.atomic():
                tags = form.cleaned_data.pop('tags')
                content = form.cleaned_data.pop('content')
                print(content)
                content = XSSFilter().process(content)
                form.cleaned_data['blog_id'] = request.session['user_info'][
                    'blog__nid']
                print(form.cleaned_data)
                obj = models.Article.objects.create(**form.cleaned_data)
                models.ArticleDetail.objects.create(content=content,
                                                    article=obj)
                tag_list = []
                for tag_id in tags:
                    tag_id = int(tag_id)
                    tag_list.append(
                        models.Article2Tag(article_id=obj.nid, tag_id=tag_id))
                models.Article2Tag.objects.bulk_create(tag_list)
            print('ok')
            return redirect('/backend/article-0-0.html')
        else:
            print('err')
            return render(request, 'backend_add_article.html', {'form': form})
    else:
        return redirect('/')
コード例 #3
0
def edit_article(request, id):
    article_id = id
    blog = request.session['user_info']['blog']
    if request.method == 'GET':
        article = Article.objects.filter(id=article_id).first()
        if not article:
            return render(request, 'backend_no_article.html')
        tags = article.tags.values_list('id')
        if tags:
            tags = list(zip(*tags))[0]
        init_dict = {
            'id': article_id,
            'title': article.title,
            'summary': article.summary,
            'detail': article.detail,
            'article_classification_id': article.article_classification_id,
            'article_type_id': article.article_type_id,
            'tags': tags,
        }
        init_files = {'img': article.img}
        form = ArticleForm(request=request, data=init_dict, files=init_files)
        return render(request, 'backend_edit_article.html', {
            'form': form,
            'id': id
        })
    elif request.method == 'POST':
        form = ArticleForm(request=request,
                           data=request.POST,
                           files=request.FILES)
        if form.is_valid():
            obj = Article.objects.filter(id=id).first()
            if not obj:
                return render(request, 'backend_no_article.html')
            with transaction.atomic():
                detail = form.cleaned_data.pop('detail')
                detail = XSSFilter().process(detail)
                form.cleaned_data['detail'] = detail
                if 'publish' in request.POST:
                    if obj.publish == 0:
                        form.cleaned_data['publish'] = 1
                tags = form.cleaned_data.pop('tags')
                file = form.cleaned_data['img']
                if file:
                    if not file.name.startswith('static'):
                        form.cleaned_data[
                            'img'] = 'static/upload/artcle/' + form.cleaned_data[
                                'img'].name
                    if not os.path.exists(form.cleaned_data['img']):
                        f = open(form.cleaned_data['img'], 'wb')
                        for row in file.chunks():
                            f.write(row)
                        f.close()
                else:
                    form.cleaned_data.pop('img')
                Article.objects.filter(id=obj.id).update(**form.cleaned_data)
                Ariticle_Tags.objects.filter(article=obj).delete()
                tag_list = []
                for tag_id in tags:
                    tag_id = int(tag_id)
                    tag_list.append(
                        Ariticle_Tags(article_id=obj.id, tag_id=tag_id))
                Ariticle_Tags.objects.bulk_create(tag_list)
            return redirect('/backend/article-0-0.html')
        else:
            return render(request, 'backend_edit_article.html', {
                'form': form,
                'id': id
            })
コード例 #4
0
def add_article(request):
    # 添加文章
    if request.method == "GET":
        form = ArticleForm(request=request)
        return render(request, "Backend/add_article.html", {
            'form': form,
            'oper': 'add',
            'nid': -1
        })
    elif request.method == 'POST':
        oper = request.GET.get('oper', 'add')
        nid = int(request.GET.get('nid', -1))
        if oper == 'add':
            form = ArticleForm(request=request, data=request.POST)
            if form.is_valid():
                with transaction.atomic():
                    tags = form.cleaned_data.pop('tags')
                    content = form.cleaned_data.pop('content')
                    content = XSSFilter().process(content)
                    form.cleaned_data['blog_id'] = request.session[
                        'user_info']["bloginfo__bid"]
                    # 需要将该值转化一下
                    form.cleaned_data[
                        'classification_id'] = models.Classification.objects.filter(
                            nid=form.cleaned_data['classification_id']).first(
                            )
                    form.cleaned_data['create_time'] = datetime.datetime.now()
                    obj = models.Article.objects.create(
                        **form.cleaned_data)  #添加文章
                    models.Article_Detail.objects.create(
                        detail=content, article_id=obj)  #添加文章详细
                    tag_list = []
                    for tag_id in tags:
                        tag_id = int(tag_id)
                        tag_list.append(
                            models.Article_Tag(article_id_id=obj.nid,
                                               tag_id_id=tag_id))
                    models.Article_Tag.objects.bulk_create(tag_list)  #批量创建,厉害
                return redirect("backend/article-0-0.html")
            else:
                return render(request, "backend/add_article.html", {
                    'form': form,
                    'oper': 'add',
                    'nid': -1
                })
        elif oper == "edit" and nid > 0:
            blog_id = request.session['user_info']['bloginfo__bid']
            form = ArticleForm(request=request, data=request.POST)
            if form.is_valid():
                obj = models.Article.objects.filter(nid=nid,
                                                    blog_id=blog_id,
                                                    status=1).first()
                if not obj:
                    return HttpResponse('该文章不存在或已删除')
                with transaction.atomic():
                    tags = form.cleaned_data.pop('tags')
                    content = form.cleaned_data.pop('content')
                    content = XSSFilter().process(content)
                    form.cleaned_data['blog_id'] = request.session[
                        'user_info']["bloginfo__bid"]
                    # 需要将该值转化一下
                    form.cleaned_data[
                        'classification_id'] = models.Classification.objects.filter(
                            nid=form.cleaned_data['classification_id']).first(
                            )
                    form.cleaned_data['create_time'] = datetime.datetime.now()
                    obj = models.Article.objects.filter(
                        nid=obj.nid,
                        status=1).update(**form.cleaned_data)  # 修改文章
                    models.Article_Detail.objects.filter(
                        article_id_id=nid,
                        status=1).update(detail=content)  # 修改文章详细
                    models.Article_Tag.objects.filter(
                        article_id_id=nid).delete()  #使用之前,先把该文章下面的所有标签删除
                    tag_list = []
                    for tag_id in tags:
                        tag_id = int(tag_id)
                        tag_list.append(
                            models.Article_Tag(article_id_id=nid,
                                               tag_id_id=tag_id))
                    models.Article_Tag.objects.bulk_create(
                        tag_list)  # 批量创建,不知道更新的时候是否有问题
                return redirect("backend/article-0-0.html")
            else:
                return render(request, "backend/add_article.html", {
                    'form': form,
                    'oper': 'edit',
                    'nid': nid
                })
        else:
            return redirect('/')
    else:
        return redirect('/')
コード例 #5
0
def edit_article(request, nid):
    """
    编辑文章
    :param request:
    :return:
    """
    blog_id = request.session['user_info']['blog__nid']
    if request.method == 'GET':
        obj = models.Article.objects.filter(nid=nid).first()
        if not obj:
            return render(request, 'backend_no_article.html')
        tags = obj.tags.values_list('nid')  # <QuerySet [(1,)]>

        if tags:
            tags = list(zip(*tags))[0]  # (1,)
        init_dict = {
            'nid': obj.nid,
            'title': obj.title,
            'summary': obj.summary,
            'category_id': obj.category_id,
            'article_type_id': obj.article_type_id,
            'content': obj.articledetail.content,
            'tags': tags
        }
        form = ArticleForm(request=request, data=init_dict)
        return render(
            request, 'backend_edit_article.html', {
                'form': form,
                'nid': nid,
                'menu_string': request.session['menu_string']
            })
    elif request.method == 'POST':
        form = ArticleForm(request=request, data=request.POST)
        if form.is_valid():
            obj = models.Article.objects.filter(nid=nid,
                                                blog_id=blog_id).first()
            if not obj:
                return render(request, 'backend_no_article.html')
            with transaction.atomic():
                content = form.cleaned_data.pop('content')
                content = XSSFilter().process(content)
                tags = form.cleaned_data.pop('tags')
                models.Article.objects.filter(nid=obj.nid).update(
                    **form.cleaned_data)
                models.ArticleDetail.objects.filter(article=obj).update(
                    content=content)
                models.Article2Tag.objects.filter(article=obj).delete()
                tag_list = []
                for tag_id in tags:
                    tag_id = int(tag_id)
                    tag_list.append(
                        models.Article2Tag(article_id=obj.nid, tag_id=tag_id))

                models.Article2Tag.objects.bulk_create(tag_list)
            return redirect('/backend/article-0-0.html')
        else:
            return render(
                request, 'backend_edit_article.html', {
                    'form': form,
                    'nid': nid,
                    'menu_string': request.session['menu_string']
                })
コード例 #6
0
ファイル: views.py プロジェクト: lxj23626/Blog-System
def edit_article(request, aid):
    blog_id = request.session['userinfo']['blog__bid']

    if request.method == 'GET':
        article_obj = models.Article.objects.filter(blog_id=blog_id,
                                                    aid=aid).first()
        if not article_obj:
            return HttpResponse('not Found')
        article_tag = article_obj.tag.values_list('tid')  # <QuerySet [(2,)]>

        if article_tag:
            article_tag = list(zip(*article_tag))[0]

        try:
            content = article_obj.article_detail.content
        except Exception as e:
            return HttpResponse('not Found')

        init_dict = {
            'aid': article_obj.aid,
            'title': article_obj.title,
            'summary': article_obj.summary,
            'content': content,
            'article_type_id': article_obj.article_type_id,
            'tag': article_tag,
        }
        form = ArticleForm(request, data=init_dict)  # 把数据库的内容展现到get页面上
        # print(111,init_dict)

        return render(request, 'backend/edit_article.html', {
            'form': form,
            'aid': aid
        })

    elif request.method == 'POST':
        form = ArticleForm(request, data=request.POST)
        if form.is_valid():
            article_obj = models.Article.objects.filter(blog_id=blog_id,
                                                        aid=aid).first()
            if not article_obj:
                return HttpResponse('Not Found')
            with transaction.atomic():
                # print(222222,form.cleaned_data)
                content = form.cleaned_data.pop('content')
                content = XSSFilter().process(content)

                tag = form.cleaned_data.pop('tag')
                # print(777,tag)

                models.Article.objects.filter(aid=article_obj.aid).update(
                    **form.cleaned_data)
                models.Article_detail.objects.filter(
                    article=article_obj).update(content=content)

                models.Article2Tag.objects.filter(article=article_obj).delete()

                tag_list = []
                for tag_id in tag:
                    # tag_list.append(models.Article2Tag.objects.filter(article=article_obj, tag_id=int(tag_id)))    # [<QuerySet []>],不能用这个,被删了找不到了
                    tag_list.append(
                        models.Article2Tag(article=article_obj,
                                           tag_id=int(tag_id))
                    )  # [<Article2Tag: Article2Tag object>, <Article2Tag: Article2Tag object>]
                print(5555, tag_list)
                models.Article2Tag.objects.bulk_create(tag_list)

            return redirect('/backend/article-0-0.html')
        else:
            return render(request, 'backend/edit_article.html', {
                'form': form,
                'aid': aid
            })
コード例 #7
0
def edit_article(request, nid):
    """
    编辑文章
    :param request:
    :return:
    """
    username = request.session.get("username", None)  # 获取session中的用户名
    user_obj = models.UserInfo.objects.filter(
        username=username).select_related("blog").first()  # 获取用户对象
    if request.method == "GET":
        article_obj = models.Article.objects.filter(
            nid=nid, blog_id=user_obj.blog.nid).first()  # 获取文章对象
        if not article_obj:
            return render(request, 'backend_no_article.html')
        tags = article_obj.tags.values_list('nid')  # 获取文章对应标签
        if tags:
            tags = [i[0] for i in list(tags)]  # 将每一个标签ID放入列表中
        article_form = ArticleForm(
            request=request,
            initial={
                "title": article_obj.title,  # 文章标题
                "summary": article_obj.summary,  # 文章简介
                "content": article_obj.article_detail.content,  # 文章内容
                "article_type_id": article_obj.article_type_id,  # 文章类型ID
                "category_id": article_obj.category_id,  # 文章分类ID
                "tags": tags,  # [18, 25] 列表中每一个元素为标签ID,元组也可以
                "top": [int(article_obj.top)]  # 显示文章是否置顶
            })
        return render(
            request, 'backend_edit_article.html', {
                "user_obj": user_obj,
                "article_obj": article_obj,
                "article_form": article_form,
            })
    elif request.method == "POST":
        article_form = ArticleForm(request=request, data=request.POST)
        article_obj = models.Article.objects.filter(
            nid=nid, blog_id=user_obj.blog.nid).first()  # 获取文章对象
        if not article_obj:
            return render(request, 'backend_no_article.html')
        if article_form.is_valid():
            with transaction.atomic():
                top_list = article_form.cleaned_data.pop("top")  # 获取高级选项列表
                top_bool = int(top_list[0]) if top_list else 0  # 获取是否选中置顶
                content = XSSFilter().process(
                    article_form.cleaned_data.pop("content"))  # 获取过滤好的文章内容
                models.ArticleDetail.objects.filter(
                    article=article_obj).update(content=content)  # 更新文章内容
                models.Article.objects.filter(nid=article_obj.nid).update(
                    **{
                        "title":
                        article_form.cleaned_data.pop("title"),
                        "summary":
                        article_form.cleaned_data.pop("summary"),
                        "article_type_id":
                        article_form.cleaned_data.pop("article_type_id"),
                        "category_id":
                        article_form.cleaned_data.pop("category_id"),
                        "top":
                        top_bool
                    })  # 更新文章

                # 更新文章对应标签
                models.Article2Tag.objects.filter(
                    article=article_obj).delete()  # 首先删除原有的标签
                tags = article_form.cleaned_data.pop("tags")  # 获取用户选择的标签ID
                tag_list = []  # 存储标签对象
                for tag_id in tags:  # 循环所有标签ID,生成标签对象并存入列表
                    tag_id = int(tag_id)
                    tag_list.append(
                        models.Article2Tag(article_id=article_obj.nid,
                                           tag_id=tag_id))
                models.Article2Tag.objects.bulk_create(tag_list)  # 批量创建标签对象
            return redirect("/backend/article-0-0.html")
        else:
            return render(
                request, 'backend_edit_article.html', {
                    "user_obj": user_obj,
                    "article_obj": article_obj,
                    "article_form": article_form,
                })