Exemplo n.º 1
0
def client_register(request):
	classroom_id = request.POST['classroom']
	try:
		classroom = Classroom.objects.get(id=classroom_id)
	except Classroom.DoesNotExist:
		return json_response({"success":False, "error":"Error: No classroom with that id"})	
	try:
		ctr = ClassroomTeacherRel.objects.get(classroom=classroom)
	except ClassroomTeacherRel.DoesNotExist:
		classroom_log(classroom, "Client register error: No teacher for class.")
		return json_response({"success":False, "error":"Error: No teacher for that classroom"})	
	teacher = ctr.user
	total_students = get_num_teacher_students(teacher)
	analytics.track_event("student_first_login", user=teacher, data={"total_students":total_students+1})
	analytics.person_update(teacher, {"num_students":total_students+1})

	real_username = get_real_username(classroom_id, request.POST['name'])

	try:
		user = User.objects.get(username=real_username)
	except User.DoesNotExist:		
		user = None

	if user:
		if UserClassroomAssignment.objects.filter(user=user, classroom=classroom).exists():
			classroom_log(classroom, "Client register error: User already exists: %s" % real_username)
			return json_response({"success":False, "error":"Error: User exists already!"})

		elif UserClassroomAssignment.objects.filter(user=user).exists():
			classroom_log(classroom, "Client register error: User exists in another class: %s" % real_username)
			return json_response({"success":False, "error":"Error: User exists in another class!"})

		else:
			uca = UserClassroomAssignment(user=user, classroom=classroom)
			uca.save()
			user = perform_signin(request, real_username, "default")
			classroom_log(classroom, "Client register (already exists, logging in): %s" % real_username)
			return json_response({"success":True, "sessionid":request.session.session_key})			
			
	else:
		try:
			user = User.objects.create_user(real_username, "", "default")
		except:
			classroom_log(classroom, "Client register error: Username taken: %s" % real_username)
			return json_response({"success":False, "error":"Error: Username taken"})	
		user = perform_signin(request, real_username, "default")
		if user:
			user.first_name = request.POST['name']
			user.save()

			uca = UserClassroomAssignment(user=user, classroom=classroom)
			uca.save()

			classroom_log(classroom, "Client register: %s" % real_username)
			return json_response({"success":True, "sessionid":request.session.session_key})
		else:
			log_and_email(request, "User successfully created but couldn't sign in!")
			return json_response({"success":False, "error":"Error: Something went wrong. Try again!"})	
Exemplo n.º 2
0
def client_login(request):
	classroom_id = request.POST['classroom']
	username = request.POST['name']
	password = request.POST.get("password","default")

	# Make sure the class is real
	try:
		classroom = Classroom.objects.get(id=classroom_id)
	except Classroom.DoesNotExist:
		return json_response({"success":False, "error":"Error: No classroom with that id"})	

	# Make sure there's a teacher for it
	try:
		ctr = ClassroomTeacherRel.objects.get(classroom=classroom)
	except ClassroomTeacherRel.DoesNotExist:
		classroom_log(classroom, "Client login error: No teacher for classroom, reported for user: %s", username)
		return json_response({"success":False, "error":"Error: No teacher for that classroom"})	

	teacher = ctr.user
	total_students = get_num_teacher_students(teacher)

	analytics.track_event("student_second_login", user=teacher, data={"total_students":total_students})
	analytics.person_update(teacher, {"num_students":total_students})
	
	# Get the user who's going to sign in
	try:
		user_to_signin = get_user_from_name(classroom.id, username)
	except UserClassroomAssignment.DoesNotExist:
		classroom_log(classroom, "Client login error: Not a real user: %s" % username)
		return json_response({"success":False, "error":"Error: Not a real user!"})	

	# Check if we have enough licenses or if our trial is still going
	num_exp = get_num_playing_expired_students(teacher)
	num_lic = teacher.profile.num_licenses

	if user_to_signin.profile.playtime > TRIAL_PLAYTIME_SECONDS:
		# If the teacher has no licenses then don't let them log in after the trial time expires
		if num_lic == 0:
			classroom_log(classroom, "Client login error: Trial time expired for user: %s" % username)
			return json_response({"success":False, "error":"Error: your trial time expired!"})

		# If they have licenses but not enough, tell the student about it
		elif num_exp >= num_lic:
			classroom_log(classroom, "Client login error: Not enough licenses, reported for user: %s" % username)
			return json_response({"success":False, "error":"Error: %d students but only %d licenses!" % (num_exp+1, num_lic)})

	# Try to sign in!
	user = perform_signin_no_password(request, user_to_signin.username)
	if user:
		classroom_log(classroom, "Client login: %s" % username)
		return json_response({"success":True, "sessionid":request.session.session_key})

	classroom_log(classroom, "Client login error: Not a real user: %s" % username)
	return json_response({"success":False, "error":"Error: Not a real user!"})