Example #1
0
def create_post(request):
	user = request.user
	blog_id = request.POST.get('blog_id')
	blog = Blog.objects.get(pk = blog_id)
	post_id = request.POST.get('post_id')
	if user not in blog.authors.all():
		return HttpResponse("Sorry you are not authorized to post here")
	else:
		if post_id:
			post = Post.objects.get(pk=post_id)
			if user != post.author:
				return HttpResponse("Sorry you are not authorized to edit here")
		else:
			post = Post()
		post.author = user
		post.blog = blog
		post.title = request.POST.get('title')
		post.body = request.POST.get('body')
		
		tags = request.POST.get('tags')
		tag_names = tags.split(',')
		if not tags.strip():
			tag_names = [constant.NO_TAG]
		tag_set = set(tag_names)
		tag_list = []
		for tname in tag_set:
			tname_s = tname.strip()
			try:
				tag = Tag.objects.get(name__iexact=tname_s)
			except Tag.DoesNotExist:
				tag = None
			if not tag:
				tag = Tag()
				tag.name = tname_s
				tag.save()
			tag_list.append(tag)
		post.save()
		post.tags = tag_list;
		post.save()
		return HttpResponseRedirect(reverse('myblog:index'))