Ejemplo n.º 1
0
def add_article(request):
    if request.method == "GET":
        form = ArticleForm.ArticleForm(request)
        return render(request, 'backend/add_article.html', {
            'form': form,
        })
    elif request.method == "POST":
        form = ArticleForm.ArticleForm(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.get('blogid')
                obj = Article.objects.create(**form.cleaned_data)
                ArticleDetail.objects.create(content=content, article=obj)
                tag_list = []
                for tag_id in tags:
                    tag_id = int(tag_id)
                    tag_list.append(tag_id)
                obj.tags.add(*tag_list)
            return redirect('/backend/article-0-0-0-0.html')

        else:
            return render(request, 'backend/add_article.html', {
                'form': form,
            })
    else:
        return redirect('backend/article.html')
Ejemplo n.º 2
0
def article2(request):
    if request.method == "POST":
        obj = ArticleForm(request.POST)
        if obj.is_valid():
            print(request.POST)
            article_detail = obj.cleaned_data.pop('content')
            models.Article.objects.create(**obj.cleaned_data)
            # models.ArticleDetail.objects.create(content=article_detail, article_id=obj.cleaned_data.)
        else:
            print(obj.errors)
        return HttpResponse('xxxx')
    obj = ArticleForm()
    return render(request, 'backend/article2.html', {'article': obj})
Ejemplo n.º 3
0
def edit_article(request, nid):
    blogid = request.session.get('blogid')
    if request.method == 'GET':
        obj = Article.objects.filter(nid=nid, blog_id=blogid).first()
        if not obj:
            return render(request, 'backend/no-article.html')
        tags = obj.tags.values_list('nid')
        if tags:
            tags = list(zip(*tags))[0]
        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.detail.content,
            'tags': tags
        }
        formobj = ArticleForm.ArticleForm(request=request, data=init_dict)
        return render(request, 'backend/edit_article.html', {
            'form': formobj,
            'nid': nid
        })
    elif request.method == "POST":
        form = ArticleForm.ArticleForm(request, data=request.POST)
        if form.is_valid():
            atricleobj = Article.objects.filter(nid=nid,
                                                blog_id=blogid).first()
            if not atricleobj:
                return render(request, 'backend/no-article.html')
            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.get('blogid')
                obj = Article.objects.filter(nid=atricleobj.nid).update(
                    **form.cleaned_data)
                ArticleDetail.objects.filter(article=obj).update(
                    content=content)
                atricleobj.tags.clear()
                tag_list = []
                for tag_id in tags:
                    tag_id = int(tag_id)
                    tag_list.append(tag_id)
                atricleobj.tags.add(*tag_list)
            return redirect('/backend/article-0-0-0-0.html')
        else:
            return render(request, 'backend/edit_article.html', {
                'form': form,
                'nid': nid
            })
Ejemplo n.º 4
0
def add_article(request):
    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,
                           files=request.FILES)
        if form.is_valid():
            print(form.cleaned_data)
            with transaction.atomic():
                detail = form.cleaned_data.pop('detail')
                detail = XSSFilter().process(detail)
                form.cleaned_data['detail'] = detail
                form.cleaned_data['blog_id'] = request.session['user_info'][
                    'blog']
                if 'publish' in request.POST:
                    form.cleaned_data['publish'] = 1
                tags = form.cleaned_data.pop('tags')
                obj = Article.objects.create(**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_add_article.html', {'form': form})
    else:
        return redirect('/')
Ejemplo n.º 5
0
def article_add(request):
    if request.method == "POST":
        form = ArticleForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            article = Article()
            article.title = cd['title']
            article.content = cd['content']
            article.created = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
            article.save()
            return HttpResponseRedirect("/backend/articles")
    else:
        form = ArticleForm()
        c = {'form':form}
        return render_to_response("backend/article_add.html",c,context_instance=RequestContext(request))
Ejemplo n.º 6
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
            })
Ejemplo n.º 7
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     article = Article.objects.get(slug=self.kwargs.get('slug'))
     form = ArticleForm(instance=article)
     context['form'] = form
     return context