#coding=utf-8 from django.template import RequestContext from django.shortcuts import render_to_response from django import newforms from django.http import HttpResponseRedirect from django.contrib.auth.decorators import login_required from utils.render import render_template from blog.models import Category,Post,Comment from tagging.models import Tag def postlist(request,page=0): c = {'page':page} return render_template(request,'blog/post_list.html',c) def category(request,id,page=0): cate = Category.objects.get(id = id) c = {'cate':cate,'page':page} return render_template(request,'category.html',c) def posttaglist(request,id): posttag = Tag.objects.get(id=id) c = {'posttag':posttag} return render_template(request,'blog/post_taglist.html',c) def alltags(request): tags = Tag.objects.usage_for_model(Post, counts=True) c = {'tags':tags} return render_template(request,'blog/alltags.html',c) def postdetail(request,id): post = Post.objects.get(id=id) post.icount += 1 post.save() comments = Comment.objects.filter(post = id) c = {'post':post,'comments':comments} return render_template(request,'blog/post_detail.html',c) @login_required def newpost(request): PostForm = newforms.models.form_for_model(Post) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.icount = 1 post.save() #add tags to the blog tags = request['tagstring'] if tags != '': Tag.objects.update_tags(post,tags) return HttpResponseRedirect("/post/%s"% post.id) else: form = PostForm() form.fields['category'].choices = [(cate.id, cate.name) for cate in Category.objects.all()] c = {'form':form} return render_template(request,'blog/post_new.html',c) def editpost(request,id): post = Post.objects.get(id=id) icount = post.icount PostForm = newforms.models.form_for_instance(post) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.icount = icount post.save() #add tags to the blog tags = request['tagstring'] if tags != '': Tag.objects.update_tags(post,tags) return HttpResponseRedirect("/post/%s"% post.id) else: form = PostForm() c = {'form':form,'post':post} return render_template(request,'blog/post_new.html',c) def postcomment(request): author = request.POST["author"] content = request.POST["content"] postid = request.POST["postid"] p = Post.objects.get(id=postid) comment = Comment() comment.author = author comment.content = content comment.post = p comment.save() #send mail from django.core.mail import send_mail send_mail('[maplye.com]%s的回复'%p.title, '来自%s的回复:%s(时间:%s) URL:http://www.maplye.com/post/%s/#%s' % (comment.author,comment.content,comment.date,postid,comment.id),None, ['maplye@gmail.com'], fail_silently=False) return HttpResponseRedirect("/post/%s/#%s"% (postid,comment.id)) def deletecomment(request,id): comment = Comment.objects.get(id=id) postid = comment.post.id comment.delete() return HttpResponseRedirect("/post/%s/"% postid)