예제 #1
0
def savematch(request):
	if request.is_ajax() and request.method == 'POST':
		try:
			pin1_id = request.POST['pin1_id']
			pin2_id = request.POST['pin2_id']
			choice = request.POST['choice']
			
			pin1 = PinModel.objects.get(pin_id=pin1_id)
			pin2 = PinModel.objects.get(pin_id=pin2_id)
			
			pin1.score, pin2.score = Pin.getNewScore(pin1.score, pin2.score, pin1_id == choice)
			pin1.save()
			pin2.save()
			
			try: # Save the match history if the user is logged with facebook
				fb_id = request.POST['fb_id']
				if fb_id == 'null':
					raise KeyError
				fb_user = SocialUserModel.objects.get_or_create(social_id=fb_id, net_name="facebook")[0]
				pin1_perso = fb_user.pinpersomodel_set.get_or_create(pin=pin1)[0]
				pin2_perso = fb_user.pinpersomodel_set.get_or_create(pin=pin2)[0]
				pin1_perso.score, pin2_perso.score = Pin.getNewScore(pin1_perso.score, pin2_perso.score, pin1_id == choice)
				pin1_perso.save()
				pin2_perso.save()	
			except KeyError:
				pass
			
			request.session.modified = True
			request.session['match_list'].append((pin1.pin_id, pin2.pin_id, choice))
	
			msg = "OK"
			return HttpResponse(msg)
		
		except KeyError, PinModel.DoesNotExist:
			msg = "Pin not found"
			return HttpResponse(msg)
예제 #2
0
def ranking(request):
	if request.is_ajax() and request.method == 'GET':
		
		try:
			cat = request.GET['category']
			try:
				if cat == 'all':
					ranking_q = PinModel.objects.all()
				else:
					cat_model = CategoryModel.objects.get(category_id=cat)
					ranking_q = cat_model.pinmodel_set.all()
			except CategoryModel.DoesNotExist:
				d = {'status': 'ERR', 'data':'This category does not exist on the database '}
				data = dumps(d, cls=MyEncoder)
				return HttpResponse(data, mimetype='application/json')
		except KeyError:
			ranking_q = PinModel.objects.all()
			
		# Personal ranking
		if request.GET['perso'] == u'true':
			info_open_graph = []
			try:
				# Db-based personal ranking (only if the user is logged in with Facebook)
				fb_id = request.GET['fb_id']
				#access_token=request.GET['access_token']
				if fb_id == 'null':
					raise KeyError
				fb_u = SocialUserModel.objects.get_or_create(social_id=fb_id, net_name="facebook")[0]
				pin_id_list = [p.pin_id for p in ranking_q]
				pin_list = fb_u.pinpersomodel_set.order_by('-score').filter(pin__pin_id__in=pin_id_list)
				
				ranking_list = fun.group(lambda p: p.score, pin_list)
				ranking_list = [[e.pin for e in sub] for sub in ranking_list ]

				if ranking_list:
					nb=3 # Number of pin to post on facebook
					if len(ranking_list[0])>3:
						to_post=random.sample(ranking_list[0],nb)
					else:
						to_post=pin_list[:3]
					for p in to_post:
						p_url = 'http://'+request.get_host() + '/pin?pin_id=' + p.pin_id
						info_open_graph.append({'fb_id':fb_id, 'action':'vote for', 'obj':'pin', 'obj_url':p_url})
			except KeyError:
				# Cookie-based personal ranking
				match_list = request.session['match_list']
				score = {}
				
				for t in match_list:
					pin1, pin2, choice = t
					score.setdefault(pin1, 0)
					score.setdefault(pin2, 0)
					score[pin1], score[pin2] = Pin.getNewScore(score[pin1], score[pin2], choice == pin1)
				
				ranking_list = list(ranking_q.filter(pin_id__in=score.keys()))
				ranking_list.sort(key=lambda pin:-score[pin.pin_id])
				ranking_list = fun.group(lambda p: p.score, ranking_list)
			
			if len(ranking_list) > 0:
				data = dumps({'status':'OK', 'data':{'ranking_list':ranking_list, 'info_open_graph':info_open_graph}}, cls=MyEncoder)
				return HttpResponse(data, mimetype='application/json')
			else:
				d = {'status': 'ERR', 'data':'No pins in this category'}
				data = dumps(d, cls=MyEncoder)
				return HttpResponse(data, mimetype='application/json')
			
		# Overall ranking
		if len(ranking_q) > 0:
			ranking_q = ranking_q.order_by('-score')[:10]
			ranking_list = []
			for pin in ranking_q:
				ranking_list.append(pin)
			
			ranking_list = fun.group(lambda p: p.score, ranking_list)
			data = dumps({'status':'OK', 'data':{'ranking_list':ranking_list}}, cls=MyEncoder)
			return HttpResponse(data, mimetype='application/json')
		else:
			d = {'status': 'ERR', 'data':'Error: No pins in this category'}
			data = dumps(d, cls=MyEncoder)
			return HttpResponse(data, mimetype='application/json')
	else:
		raise Http404