Exemple #1
0
def add_article(request):
    tag_list = models.Tag.objects.filter(blog=request.user.blog)
    category_list = models.Category.objects.filter(blog=request.user.blog)
    if request.method == "POST":
        title = request.POST.get('title')
        content = request.POST.get('content')
        tags_list = request.POST.getlist('tags')
        category_id = request.POST.get('category')
        # 先生成一个模块对象
        soup = BeautifulSoup(content, 'html.parser')
        # print(soup.text)  # 获取纯文本
        tags = soup.find_all()
        # print(tags)
        for tag in tags:
            if tag.name == 'script':
                tag.decompose()  # 删除标签
        # 先简单的除暴的直接截取内容的150个字符串
        # desc = content[0:150]
        desc = soup.text[0:150]
        # 操作数据
        article_obj = models.Article.objects.create(title=title,
                                                    content=str(soup),
                                                    desc=desc,
                                                    category_id=category_id,
                                                    blog=request.user.blog)
        # 去文章与标签表的第三张关系报中手动录入数据   bulk_create
        obj_list = []
        for tag_id in tags_list:
            obj_list.append(
                models.Article2Tag(article=article_obj, tags_id=tag_id))
        models.Article2Tag.objects.bulk_create(obj_list)
        return redirect('/backend/')
    return render(request, 'backend/add_article.html', locals())
Exemple #2
0
def edit_article(request):
    edit_id = request.GET.get('edit_id')
    edit_obj = models.Article.objects.filter(pk=edit_id).first()
    if request.method == "POST":
        title = request.POST.get('title')
        content = request.POST.get('content')
        category_id = request.POST.get('category')
        tag_list = request.POST.get('tag')
        soup = BeautifulSoup(content, 'html.parser')
        tags = soup.find_all()
        for tag in tags:
            if tag.name == 'script':
                tag.decompose()
        desc = soup.text[0:150]
        models.Article.objects.filter(pk=edit_id).update(
            title=title,
            content=str(soup),
            desc=desc,
            category_id=category_id,
            blog=request.user.blog)
        tag_article_list = []
        for i in tag_list:
            tag_article_list.append(
                models.Article2Tag(article_id=edit_id, tag_id=i))
        models.Article2Tag.objects.bulk_create(tag_article_list)
        return redirect('/backend')
    category_list = models.Category.objects.filter(blog=request.user.blog)
    tag_list = models.Tag.objects.filter(blog=request.user.blog)
    return render(request, 'backend/edit_article.html', locals())
Exemple #3
0
def add_article(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        content = request.POST.get('content')
        tags = request.POST.getlist('tag')
        category_id = request.POST.get('category')
        # 麻瓜式做法  直接对content窃取150
        #  1 先生成一个BeautifulSoup对象
        soup = BeautifulSoup(content,'html.parser')
        for tag in soup.find_all():
            # 针对script标签 应该直接删除
            # print(tag.name)  # 获取当前html页面所有的标签
            if tag.name == 'script':
                tag.decompose()  # 将符合条件的标签删除

        # 文章简介应该是150个文本内容
        desc = soup.text[0:150]
        # desc = content[0:150]
        article_obj = models.Article.objects.create(title=title,desc=desc,content=str(soup),category_id=category_id,blog=request.user.blog)
        # 一个个的添加
        b_list = []
        for tag_id in tags:
            b_list.append(models.Article2Tag(article=article_obj,tag_id=tag_id))
        models.Article2Tag.objects.bulk_create(b_list)
        return redirect('/backend/')

    tag_list = models.Tag.objects.filter(blog=request.user.blog)
    category_list = models.Category.objects.filter(blog=request.user.blog)
    return render(request,'backend/add_article.html',locals())
Exemple #4
0
def add_essays(request):
    category_list = models.Category.objects.filter(blog__user=request.user)
    tag_list = models.Tag.objects.filter(blog__user=request.user)
    if request.method == "POST":
        title = request.POST.get('title')
        content = request.POST.get('content')
        category_id = request.POST.get('category')
        tag_lis = request.POST.getlist('tag')
        soup = BeautifulSoup(content, 'lxml')
        h5_list = soup.find_all()
        for tag in h5_list:
            if tag.name == 'script':
                tag.decompose()

        content = str(soup)
        desc = soup.get_text()[0:150] + '...'
        article_obj = models.Article.objects.create(title=title,
                                                    desc=desc,
                                                    content=content,
                                                    blog=request.user.blog,
                                                    category_id=category_id)
        tag_obj_list = []
        for tag in tag_lis:
            tag_obj_list.append(
                models.Article2Tag(article=article_obj, tag_id=tag))
        models.Article2Tag.objects.bulk_create(tag_obj_list)
        return redirect('/back_stage/')
    return render(request, 'back/add_essays.html', locals())
Exemple #5
0
def add_article(request):
    if request.method == "POST":
        title = request.POST.get('title')
        content = request.POST.get('content')
        category_id = request.POST.get('category')
        tag_list = request.POST.getlist('tag')

        # 先生成一个对象
        soup = BeautifulSoup(content,'html.parser')
        tags = soup.find_all()  # tags 是所有的标签
        for tag in tags:
        #     # tag.name取到标签的名字
        #     print(tag.name)
            if tag.name == 'script':  # # 取出script标签,删除
                tag.decompose()
        # 文章简介
        # 先简单粗暴 截150个字符串
        # desc = content[0:150]
        # 先获取文章文本内容  再截取150个字符
        desc = soup.text[0:150]
        article_obj = models.Article.objects.create(title=title,desc=desc,content=str(soup),category_id=category_id,blog=request.user.blog)
        # 关系表是我们自己建的 没法使用add set等方法
        tag_article_list = []
        for i in tag_list:
            tag_article_list.append(models.Article2Tag(article=article_obj,tag_id=i))
        # 批量插入数据
        models.Article2Tag.objects.bulk_create(tag_article_list)
        return redirect('/backend/')
    category_list = models.Category.objects.filter(blog=request.user.blog)
    tag_list = models.Tag.objects.filter(blog=request.user.blog)

    return render(request,'backend/add_article.html',locals())
Exemple #6
0
def add_article(request):
    if request.method == 'POST':
        title=request.POST.get('title')
        content=request.POST.get('content')
        category_id=request.POST.get('category')
        tag_id_list=request.POST.getlist('tag')
        # 模块使用
        soup=BeautifulSoup(content,'html.parser')

        tags=soup.find_all()
        # 获取所有的标签
        for tag in tags:
            # print(tag.name)  # 获取页面所有的标签
            # 针对script标签,直接删除
            if tag.name == 'script':
                # 删除标签
                tag.decompose()


        # 文章简介
        # 1.先简单暴力的直接切取content 150个字符
        # desc=content[0:150]
        # 2.截取文本150个
        desc=soup.text[0:150]
        article_obj=models.Article.objects.create(
            title=title,
            content=str(soup),
            desc=desc,
            category_id=category_id,
            blog=request.user.blog
        )
        # 文章和标签的关系表,是我们自己创建的,没法使用add,set,remove,clear方法
        # 自己去操作关系表,一次性可能需要创建多条数据,批量插入bulk_create()
        article_obj_list=[]
        for i in tag_id_list:
            # article_obj_list.append(models.Article2Tag(article=article_obj,tag_id=i))
            tag_article_obj=models.Article2Tag(article=article_obj,tag_id=i)
            article_obj_list.append(tag_article_obj)
        # 批量插入数据
        models.Article2Tag.objects.bulk_create(article_obj_list)
        # 跳转到后台管理文章展示页
        return redirect('/backend/')



    category_list=models.Category.objects.filter(blog=request.user.blog)
    tag_list=models.Tag.objects.filter(blog=request.user.blog)
    return render(request,'backend/add_article.html',locals())
Exemple #7
0
def add_article(request):
    category_list = models.Category.objects.all()
    tag_list = models.Tag.objects.all()
    if request.method == "POST":
        title = request.POST.get('title')
        article_content = request.POST.get('article_content')
        keywords = request.POST.get("keywords", "")
        category_id = request.POST.get("category", "")
        tags = request.POST.getlist("tags", "")

        user = request.user
        bs = BeautifulSoup(article_content, "html.parser")
        for tag in bs.find_all():
            if tag.name in ["script", "link"]:
                tag.decompose()
        desc = str(bs.text)[:150]
        mainImage = request.FILES.get("mainImage")
        if not mainImage:
            ret = re.search(r'src="/media/(\w+/.*?.\w{3,4})"', article_content)
            if ret:
                mainImage = ret.groups()[0]
        # 生成article新增对象
        article = models.Article.objects.create(
            title=title,
            user=user,
            desc=desc,
            mainPicture=mainImage,
            category_id=category_id,
        )
        if keywords:
            keywords = keywords.split(',')
            queryset = []
            for key in keywords:
                queryset.append(models.Keywords(word=key, article=article))
            models.Keywords.objects.bulk_create(queryset)
        if tags:
            queryset = []
            for tag in tags:
                queryset.append(
                    models.Article2Tag(tag_id=tag, article_id=article.pk))
            # tag表
            models.Article2Tag.objects.bulk_create(queryset)
        models.ArticleDetail.objects.create(content=(str(bs)), article=article)
        return redirect("/index/")
    return render(request, "add_article.html", locals())
Exemple #8
0
def edit_article(request, username, article_id):
    if request.method == "POST":
        title = request.POST.get("title")
        content = request.POST.get("content")
        category_id = request.POST.get("category")
        tag_id_list = request.POST.getlist("tag")
        # beautifulsoup4 模块使用
        soup = BeautifulSoup(content, "html.parser")
        tags = soup.find_all()
        # 获取所有的标签
        for tag in tags:
            # print(tag.name)  # 获取页面上所有标签
            # 针对script标签 直接删除
            if tag.name == "script":
                # 删除标签
                tag.decompose()

        # 文章简介
        # 截取文本
        # desc = content[0:150]
        desc = soup.text[0:150]
        article_obj = models.Article.objects.filter(pk=article_id).update(
            title=title,
            content=str(soup),
            desc=desc,
            category_id=category_id,
            blog=request.user.blog)
        # 文章和标签表的关系表是我们用半自动方式创建的,不能使用add set remove clear方法
        # 自己去操作关系表
        # 更新
        edit_article_obj = models.Article.objects.filter(pk=article_id).first()
        edit_article_obj.tags.clear()
        article_obj_list = []
        for i in tag_id_list:
            article_obj_list.append(
                models.Article2Tag(article=edit_article_obj, tag_id=i))

        models.Article2Tag.objects.bulk_create(article_obj_list)
        return redirect("backend")

    category_list = models.Category.objects.filter(blog=request.user.blog)
    tag_list = models.Tag.objects.filter(blog=request.user.blog)
    article_obj = models.Article.objects.filter(pk=article_id).first()

    return render(request, 'backend/edit_article.html', locals())
Exemple #9
0
 def post(self, request, article_id):
     form_obj = myforms.ArticleEditForm(request.POST)
     category_list = models.Category.objects.filter(blog=request.user.blog)
     tag_list = models.Tag.objects.filter(blog=request.user.blog)
     if form_obj.is_valid():
         title = form_obj.cleaned_data.get('title')
         content = form_obj.cleaned_data.get('content')
         soup = pre_xss(content)
         category = request.POST.get('category')
         tag_id_list = request.POST.getlist('tag')
         with transaction.atomic():
             models.Article.objects.filter(pk=article_id).\
                 update(title=title, content=str(soup), desc=soup.text[0:150], category_id=category)
             models.Article2Tag.objects.filter(article_id=article_id).delete()
             tag_tmp_list = (models.Article2Tag(article_id=article_id, tag_id=tag) for tag in tag_id_list)
             models.Article2Tag.objects.bulk_create(tag_tmp_list)
         return redirect('backend')
     return render(request, 'backend/article_edit.html', locals())
Exemple #10
0
def add_article(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        content = request.POST.get('content')
        category_id = request.POST.get('category')
        tag_list = request.POST.get('tag')

        # BeautifulSoup4模块使用:专门用来处理HTML中的内容
        soup = BeautifulSoup(content, 'html.parser')
        # 获取页面中的整个html
        tags = soup.find_all()
        # 获取所有的标签
        for tag in tags:
            # 针对script标签 直接删除
            # tag.name获取html中所有的标签
            if tag.name == 'script':
                # 删除标签
                tag.decompose()

        # 文章简介:截取文章中前150
        desc = soup.text[0:150]

        article_obj = models.Artice.objects.create(title=title,
                                                   content=str(soup),
                                                   desc=desc,
                                                   category_id=category_id,
                                                   blog=request.user.blog)

        # 文章和标签的关系表 是我们自己创建的 没法使用add set remove clear方法
        # 自己去操作关系表   一次性可能需要创建多条数据      批量插入bulk_create()
        article_obj_list = []
        for i in tag_list:
            tag_article_obj = models.Article2Tag(article=article_obj, tag_id=i)
            article_obj_list.append(tag_article_obj)
            # 批量插入数据
        models.Article2Tag.objects.bulk_create(article_obj_list)
        # 跳转到后台管理文章展示页
        return redirect('/backend/')

    category_list = models.Category.objects.filter(blog=request.user.blog)
    tag_list = models.Tag.objects.filter(blog=request.user.blog)
    return render(request, 'backend/add_article.html', locals())
Exemple #11
0
 def post(self, request):
     form_obj = myforms.ArticleAddForm(request.POST)
     if form_obj.is_valid():
         title = form_obj.cleaned_data.get('title')
         content = form_obj.cleaned_data.get('content')
         # 预防xss攻击
         soup = pre_xss(content)
         category_id = request.POST.get('category')
         tag_id_list = request.POST.getlist('tag')
         with transaction.atomic():
             article_obj = models.Article.objects.create(title=title,
                                                         content=str(soup),        # 使用去除了script的文章内容
                                                         desc=soup.text[0:150],
                                                         blog=request.user.blog,
                                                         category_id=category_id)
             tag_tmp_list = (models.Article2Tag(article=article_obj, tag_id=tag) for tag in tag_id_list)
             models.Article2Tag.objects.bulk_create(tag_tmp_list)
         return redirect('backend')
     else:
         category_list = models.Category.objects.filter(blog__userinfo=request.user)
         tag_list = models.Tag.objects.filter(blog__userinfo=request.user)
         return render(request, 'backend/article_add.html', locals())
Exemple #12
0
def add_article(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        content = request.POST.get('content')
        category_id = request.POST.get('category')
        tag_id_list = request.POST.getlist('tag')

        # bs4模块的使用
        soup = BeautifulSoup(content, 'html.parser')
        # 获取所有的标签
        tags = soup.find_all()
        for tag in tags:
            print(tag.name)  # 获取页面所有的标签
            # 将script标签找出来并直接删除
            if tag.name == 'script':
                tag.decompose()

        # 获取文章简介,截取150个文本
        # desc = content[0:150]  # 直接截取到的是HTML代码
        desc = soup.text[0:150]  # 通过soup.text截取
        article_obj = models.Article.objects.create(title=title,
                                                    content=str(soup),
                                                    desc=desc,
                                                    category_id=category_id,
                                                    blog=request.user.blog)
        # 文章和标签的关系表是我们自己创建的,没法使用add\set\remove\clear方法
        # 需要自己去操作关系表,文章和标签是一对多的关系,所以添加一篇文章有时需要几条数据,所以这里用批量插入的方式
        article_obj_list = []
        for i in tag_id_list:
            article_obj1 = models.Article2Tag(article=article_obj, tag_id=i)
            article_obj_list.append(article_obj1)
        models.Article2Tag.objects.bulk_create(article_obj_list)
        # 跳转到后台管理文章展示页
        return redirect('/backend/')

    category_list = models.Category.objects.filter(blog=request.user.blog)
    tag_list = models.Tag.objects.filter(blog=request.user.blog)
    return render(request, 'backend/add_article.html', locals())