Пример #1
0
def remove_term_like(request):

	term_id = None
	up = False
	down = False
	context_dict ={}

	if request.method == 'POST':
		params=json.loads(request.body)
		term_id = params['term_id']
		signal = params['signal']

	if term_id:
		term = Term.objects.get(id = term_id)
		termvote = TermVote.objects.get(user = request.user, term = term)
		if termvote:
			termvote.delete()
			if signal == "up":
				term.upvotes = term.upvotes - 1
				up = True
				
			elif signal == "down":
				term.downvotes = term.downvotes - 1
				down = True
				
			term.save()
			context_dict['downvotes'] = convert_to_small_representation(term.downvotes)
			context_dict['upvotes'] = convert_to_small_representation(term.upvotes)
			context_dict['up'] = up
			context_dict['down'] = down
			context_dict['success'] = True
			

		else:
			context_dict['success'] = False
			context_dict['no_success_message'] = "Already removed a vote."
			

	else:
		context_dict['success'] = False
		context_dict['no_success_message'] = "Invalid Form."

	return HttpResponse(json.dumps(context_dict),content_type="application/json")
Пример #2
0
def add_term_like(request):

	term_id = None
	up = False
	down = False
	context_dict = {}

	if request.method == 'POST':
		params=json.loads(request.body)
		term_id = params['term_id']
		signal = params['signal']


	if term_id:

		term = Term.objects.get(id = term_id)

		if not TermVote.objects.has_voted(request.user, term):
			termvote = TermVote()
			termvote.user = request.user
			termvote.term = term

			if signal == 'up':
				termvote.upvote = True
				termvote.downvote = False
				term.upvotes = term.upvotes + 1
				up = True

			elif signal == 'down':
				termvote.downvote = True
				termvote.upvote = False
				term.downvotes = term.downvotes + 1
				down = True

			termvote.save()
			term.save()
 	
		else:
			termvote = TermVote.objects.get(user = request.user, term=term)
			if termvote.upvote:
				if signal == 'down':
					termvote.downvote = True
					termvote.upvote = False
					term.downvotes = term.downvotes + 1
					term.upvotes = term.upvotes - 1
					down = True

			elif termvote.downvote:
				if signal == 'up':
					termvote.upvote = True
					termvote.downvote = False
					term.upvotes = term.upvotes + 1
					term.downvotes = term.downvotes - 1
					up = True
					
			termvote.save()
			term.save()
		context_dict['downvotes'] = convert_to_small_representation(term.downvotes)
		context_dict['upvotes'] = convert_to_small_representation(term.upvotes)
		context_dict['up'] = up
		context_dict['down'] = down
			
	else:
		context_dict['success']=False
		return HttpResponse(json.dumps(context_dict),content_type="application/json")

	context_dict['success'] = True
	return HttpResponse(json.dumps(context_dict),content_type="application/json")