Example #1
0
	def setUp(self):

		self.type = ProductType.objects.create(text = "male_shoes", count = 0)
		self.product1 = Product.objects.create(type = self.type, on = True, description = "yessir", fileURL = "image1.jpg")
		self.product2 = Product.objects.create(type = self.type, on = True, description = "yessir", fileURL = "image2.jpg")
		profile_string = json.dumps({
		  "pictureURL" : "https:\/\/graph.facebook.com\/551733910\/picture?type=large&return_ssl_resources=1",
		  "name" : "Nithin Tumma",
		  "gender" : "male",
		  "birthday" : "03\/28\/1994",
		  "location" : "Port Huron, Michigan",
		  "relationship" : "Single",
		  "facebookId" : "551733910"
		})
		self.user = User.objects.create(
				facebookID='551733910', 
				profile = profile_string, 
				socialIdentity = '', 
				friends = [123, 234, 345], 
				names = ["Side", 'Nithin', 'James'], 
				genders = ["male", 'male', 'female'], 
				friendsInApp = [],
				topFriends = []
		)
		self.question = Question.objects.create(type = self.type, text = "Which one", on=True)
		self.answer = Answer.objects.create(
				forFacebookId = '1', 
				chosenProduct = self.product1, 
				wrongProduct = self.product2, 
				fromUser = self.user, 
				question= self.question
		)
		self.qq = updateQuestionObjectQueue(self.user)
		self.client = Client()
def getGroupListQuestions(request, user_id):
	if request.method == "POST":	
		NUM_OBJECTS = 100	
		current_user = User.objects.get(pk=user_id)
		group = json.loads(request.POST["group"])
		genders = {}
		names = {}
		males = []
		females = []
		for friend in group:
			# get the gender	
			index = current_user.friends.index(int(friend))
			gender = current_user.genders[index]
			name = current_user.names[index]
			names[friend] = name
			genders[friend] = gender
			if gender == "male":
				males.append(friend)
			else:
				females.append(friend)


		no_males = False
		no_females = False
		if (len(males) == 0):
			no_males = True
		if (len(females) == 0):
			no_females = True

		#get all feed items by friends
		# get Question Objects
		num_q_objects = QuestionObject.objects.filter(toUser=current_user).count()
		if num_q_objects < NUM_OBJECTS:
			q_objects = updateQuestionObjectQueue(current_user, NUM_OBJECTS)
		else:
			q_objects = QuestionObject.objects.filter(toUser=current_user)[:NUM_OBJECTS]
		
		list_question_objects = []
		for q in q_objects:
			# randomly pick a friend from the group 
			# how to get the gender of the friend?
			q_gender = getGender(q)
			if q_gender:
				if q_gender == "male":
					if (no_males):
						continue
					rand_friend = choice(males)
				else:
					if (no_females):
						continue
					rand_friend = choice(females)	

			else:
				rand_friend = choice(group)
			
			
			try:
				question_text = q.currentQuestion.text.replace("%n", names[rand_friend].split()[0])
			except:
				question_text = q.currentQuestion.text



			p1_url = PRODUCT_IMAGES_BASE_URL + q.image1
			p2_url = PRODUCT_IMAGES_BASE_URL + q.image2					
			json_q = {
				"fbFriend1": [], 
				"fbFriend2": [], 
				"product1Count": q.product1Count, 
				"product2Count": q.product2Count, 
				"currentQuestion": q.currentQuestion.id,
				"name": names[rand_friend],
				"product1": q.product1.id,
				"product2": q.product2.id,
				"image1": p1_url,
				"image2": p2_url,
				"friend": str(rand_friend),
				"questionText": question_text
			}
			list_question_objects.append(json_q)
			# delete the q_object
			q.delete()

		response = HttpResponse(json.dumps(list_question_objects), mimetype='application/json')
		response["Access-Control-Allow-Origin"] = "*"
		response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
		response["Access-Control-Max-Age"] = "1000"
		response["Access-Control-Allow-Headers"] = "*"
		return response