Example #1
0
def blog_add(request):
    if request.method == "POST":
        form = BlogForm(request.POST)
        tag = TagForm(request.POST)
        if form.is_valid() and tag.is_valid():
            cd = form.cleaned_data
            cdtag = tag.cleaned_data
            tagname = cdtag["tag_name"]
            title = cd["caption"]
            author = request.user
            content = cd["content"]
            type = cd["type"]
            blog = Blog(caption=title, author=author, content=content, type=type)
            blog.save()
            for taglist in tagname.split(","):
                Tag.objects.get_or_create(tag_name=taglist.strip())
                blog.tags.add(Tag.objects.get(tag_name=taglist.strip()))
                blog.save()
            id = Blog.objects.order_by("-publish_time")[0].id
            return HttpResponseRedirect("/blog/%s" % id)
    else:
        form = BlogForm()
        tag = TagForm()
    return render_to_response(
        "blog/blog_add.html", {"form": form, "tag": tag}, context_instance=RequestContext(request)
    )
Example #2
0
def blog_update(request, id=""):
    blog = get_object_or_404(Blog, id=id)
    if request.user == blog.author:
        if request.method == "POST":
            form = BlogForm(request.POST)
            tag = TagForm(request.POST)
            if form.is_valid() and tag.is_valid():
                cd = form.cleaned_data
                cdtag = tag.cleaned_data
                tagname = cdtag["tag_name"]
                tagnamelist = tagname.split(",")
                title = cd["caption"]
                content = cd["content"]
                # blog = get_object_or_404(Blog, id=id)
                if blog:
                    blog.caption = title
                    blog.content = content
                    blog.save()
                    for taglist in tagnamelist:
                        Tag.objects.get_or_create(tag_name=taglist.strip())
                        blog.tags.add(Tag.objects.get(tag_name=taglist.strip()))
                        blog.save()
                    tags = blog.tags.all()
                    for tagname in tags:
                        tagname = unicode(str(tagname), "utf-8")
                        if tagname not in tagnamelist:
                            notag = blog.tags.get(tag_name=tagname)
                            blog.tags.remove(notag)
                else:
                    blog = Blog(caption=blog.caption, content=blog.content)
                    blog.save()
                return HttpResponseRedirect("/blog/%s" % id)
        else:
            # blog = get_object_or_404(Blog, id=id)
            # form = BlogForm(initial={'caption': blog.caption, 'content': blog.content}, auto_id=False)
            form = BlogForm(instance=blog)
            tags = blog.tags.all()
            if tags:
                taginit = ""
                for x in tags:
                    taginit += str(x) + ","
                tag = TagForm(initial={"tag_name": taginit})
            else:
                tag = TagForm()
        return render_to_response(
            "blog/blog_add.html",
            {"blog": blog, "form": form, "id": id, "tag": tag},
            context_instance=RequestContext(request),
        )
    return HttpResponseRedirect(reverse("bloglist"))
Example #3
0
 def get(self, request):
     forms = {'post': PostForm(), 'tag': TagForm()}
     return render(
         request,
         'create_update_post.html',
         {
             'forms': forms,
             'action': 'new',
             'header': 'Novo Post'
         },
     )
Example #4
0
 def get(self, request, pk):
     post = Post.objects.get(pk=pk)
     forms = {'post': PostForm(instance=post), 'tag': TagForm()}
     return render(
         request,
         'create_update_post.html',
         {
             'forms': forms,
             'action': 'edit',
             'header': 'Editar Post'
         },
     )
Example #5
0
 def post(self, request):
     form = TagForm(request.POST)
     form.save()
     return JsonResponse(list(Tag.objects.all().values()),
                         safe=False,
                         status=201)
Example #6
0
def add_blog(request):
    form = BlogForm()
    tag = TagForm()
    return render_to_response('blog_add.html')