Example #1
0
def comment(request):
    if request.POST:
        form = CommentForm(request.POST)
        comment = form.save(commit=False)
        global COMMENTID
        COMMENTID += 1
        comment.id = COMMENTID
        comment.user = request.user
        story = Story.objects.get(pk=request.POST.get("storyid"))
        comment.story = story
        comment.save()
        if comment.pk:
            response = {"result": "true"}
        else:
            response = {"result": "false"}
    return HttpResponse(simplejson.dumps(response), mimetype="application/json")
Example #2
0
def story_detail(request, story_id):
	context_dict = {}
	context_dict['user'] = request.user

	story = get_object_or_404(Story, pk=story_id)
	context_dict['story'] = story

	if request.user.is_authenticated():
		liked_stories = request.user.liked_stories.filter(id__in=[story_id])
	else:
		liked_stories = []

	context_dict['liked_stories'] = liked_stories
	
	if request.method == "POST":
		form = CommentForm(data=request.POST)

		if form.is_valid():
			temp = form.save(commit=False)
			parent = form['parent'].value()

			#print 'parent', parent
			
			if parent == '' or parent is None:
				#Set a blank path then save it to get an ID
				temp.path = ''
				temp.story_id = story
				temp.comment_moderator = request.user
				temp.save()
				temp.path = temp.id
			else:
				#Get the parent node
				node = Comment.objects.get(id=parent)

				#Max 5 levels deep
				if node.depth < 6:
					temp.depth = node.depth + 1
				else:
					temp.depth = 5

				temp.path = node.path
				temp.story_id = story
				temp.comment_moderator = request.user
				
				#Store parents path then apply comment ID
				temp.save()
				temp.path += ',' + str(temp.id)
				
			#Final save for parents and children
			temp.save()
			context_dict['form'] = form

			# After successful submission and processing of a web form
			return HttpResponseRedirect(reverse('story_detail', args=(story_id,)))

	else:
		context_dict['form'] = CommentForm()

	# may need to do the sorting in python since the database will treat the column as a string 
	# I believe, so 10 will come before 2

	try:
		comment_tree = Comment.objects.filter(story_id=story).order_by('path')
		context_dict['comment_tree'] = [comment for comment in comment_tree]

		#for comment in comment_tree:
		#	print comment.id, comment.path

	except:
		context_dict['comment_tree'] = []    

	return render(request, 'tidder/detail.html', context_dict)