示例#1
0
文件: session.py 项目: zannorman/mol9
def selectclass(request):
	if request.method == "POST":
		form = WhichClassForm(request.user, request.POST)
		if form.is_valid():
			ip = get_ip(request)
			# If they chose to create a new classroom
			if int(form.cleaned_data['which']) == 0:
				# Make the classroom, the relation to the user, and the session.
				classroom = Classroom(name=form.cleaned_data['new_class'], num_students=0)
				classroom.save()
				classroom_log(classroom, "Classroom created from selectclass")
				ctr = ClassroomTeacherRel(user=request.user, classroom=classroom, date=timezone.now())
				ctr.save()
				make_session(request, classroom)
				analytics.person_append(request.user, "Classrooms", classroom.name)
			else:	
				# Find the classroom and make a session if necessary
				classroom = Classroom.objects.get(id=int(form.cleaned_data['which']))
				try:
					session = ClassroomSession.objects.get(classroom=classroom)		
				except ClassroomSession.DoesNotExist:
					make_session(request, classroom)
			# And mark that we're currently managing this one classroom in the web session
			request.session['classroom_id'] = classroom.id
			analytics.person_increment(request.user, "Sessions Started")
			analytics.person_append(request.user, "Sessions", str(timezone.now()))			
			return HttpResponseRedirect("/session/status/")
		else:
			return renderWithNav(request, "teacher/whichclass.html", {"form":form})
	else:
		return renderWithNav(request, "teacher/whichclass.html", {"form":WhichClassForm(request.user)})
示例#2
0
文件: session.py 项目: zannorman/mol9
def start(request):
	if request.method == "POST":
		start_form = StartLobbyForm(request.POST)
		if start_form.is_valid():
			ip = get_ip(request)
			email = start_form.cleaned_data['email']
			password = start_form.cleaned_data['password']
			classroom_name = start_form.cleaned_data['classroom_name']

			# Let's check if they already have an account and they've gone to this page
			# because they forgot / didn't know to log in
			user = perform_signin(request, email, password)
			if user is None:
				# send_mail_threaded("User was none.", "user was none, with email:"+email, "*****@*****.**", ["*****@*****.**"])
				# They don't have an account, let's try to make one
				truncated_username = email[:30] #Django doesn't like usernames over 30 chars.
				try:
					#user = User.objects.create_user(email, email, password)
					user = User.objects.create_user(truncated_username, email, password)
				except:
					# Must be a duplicate account
					errors = start_form._errors.setdefault("email", ErrorList())
					errors.append(u"That email address is already in use")
					return renderWithNav(request, "teacher/nowform.html", {
						"start_form":start_form,
						})

				# This second step (logging in after successfully registering) should not ever fail.
				try:
					user = perform_signin(request, email, password)
				except:
					log_error(request)
					errors = start_form._errors.setdefault("email", ErrorList())
					errors.append(u"Something went wrong! Try a different email address please.")					
					return renderWithNav(request, "teacher/nowform.html", {
						"start_form":start_form,
						})
				analytics.alias(request, user)
				analytics.person_set_once(user, {"$created":timezone.now()})

			if user is None:
				log_and_email(request, "User is None somehow in /session/start/ POST handler")
				errors = start_form._errors.setdefault("email", ErrorList())
				errors.append(u"Something went wrong! Try a different email address please.")					
				return renderWithNav(request, "teacher/nowform.html", {
					"start_form":start_form,
					})
			
			analytics.person_increment(user, "Sessions Started")
			analytics.person_append(user, "Sessions", str(timezone.now()))
			# Now that we're logged in let's make a classroom unless one exists
			ctr = None
			classroom = None
			if ClassroomTeacherRel.objects.filter(user=user).exists():
				# If one exists let's give them the "choose a class" form so they don't
				# make a duplicate classroom! 
				whichclassform = WhichClassForm(user, initial={"new_class":""})
				return renderWithNav(request, "teacher/whichclass.html", {"form":whichclassform})

			else:
				# Okay cool let's make the classroom and relate it to this user with a CTR
				classroom = Classroom(name=classroom_name, num_students=0)
				classroom.save()
				classroom_log(classroom, "Classroom created from start")
				ctr = ClassroomTeacherRel(user=user, classroom=classroom, date=timezone.now())
				ctr.save()

				analytics.person_append(user, "Classrooms", classroom.name)
				# Set the session value so we can keep track of which classroom they're monitoring
				# currently if they have multiple classes
				request.session['classroom_id'] = classroom.id

				send_mail_threaded("New teacher started a session - " + str(timezone.now()), "Username: "******"*****@*****.**", ["*****@*****.**"])

			# Now find and update (or create) the session
			try:
				session = ClassroomSession.objects.get(classroom=classroom)
				session.ip = ip
				session.update_time = timezone.now()
				session.save()				
			except ClassroomSession.DoesNotExist:
				make_session(request, classroom)

			
			analytics.track_event("post_teacher_start", request)
			

			return HttpResponseRedirect("/session/status/")
		else:
			# Something went wrong in the form, give them the form + errors
			return renderWithNav(request, "teacher/nowform.html", {
				"start_form":start_form,
				})	
	else:
		analytics.track_event("visit_teacher_start", request)
		if request.user.is_authenticated() and ClassroomTeacherRel.objects.filter(user=request.user).exists():
			return mbredirect("/session/selectclass")		

		return renderWithNav(request, "teacher/nowform.html", {
			"start_form":StartLobbyForm(),
			})			
示例#3
0
文件: views.py 项目: zannorman/mol9
def educents(request, code):
	try:
		code_model = EducentsCode.objects.get(code=code)
	except EducentsCode.DoesNotExist:
		form = EducentsCodeForm()
		return renderWithNav(request, "educents.html", {"form":form,"code":code})
	if request.method=="POST":
		form = EducentsCodeForm(request.POST)
		if form.is_valid():
			ip = get_ip(request)
			email = form.cleaned_data['email']
			#truncated_username = email[:20]
			password = form.cleaned_data['password']
			classroom_name = form.cleaned_data['classroom_name']

			user = None
			# Let's check if they already have an account and they've gone to this page
			# because they forgot / didn't know to log in
			matchusers = User.objects.filter(email=email)
			if matchusers.count() == 0:
				# They don't have an account, let's try to make one
				try:
					#user = User.objects.create_user(email, email, password)
					user = User.objects.create_user(truncated_username, email, password)
				except:
					# Must be a duplicate account
					errors = form._errors.setdefault("email", ErrorList())
					errors.append(u"That email address is already in use")
					return renderWithNav(request, "educents.html", {"form":form,"code":code})

				# This second step (logging in after successfully registering) should not ever fail.
				try:
					user = perform_signin(request, email, password)
				except:
					log_error(request)
					errors = form._errors.setdefault("email", ErrorList())
					errors.append(u"Something went wrong! Try a different email address please.")					
					return renderWithNav(request, "educents.html", {"form":form,"code":code})
				analytics.alias(request, user)
				analytics.person_set_once(user, {"$created":timezone.now()})
			else:
				errors = form._errors.setdefault("email", ErrorList())
				errors.append(u"That email address is in use, please use a different one.")					
				return renderWithNav(request, "educents.html", {"form":form,"code":code})				

			if user is None:
				log_and_email(request, "User is None somehow in educents POST handler")
				errors = form._errors.setdefault("email", ErrorList())
				errors.append(u"Something went wrong! Try a different email address please.")					
				return renderWithNav(request, "educents.html", {"form":form,"code":code})
			
			analytics.person_increment(user, "Sessions Started")
			analytics.person_append(user, "Sessions", str(timezone.now()))
			# Now that we're logged in let's make a classroom unless one exists
			ctr = None
			classroom = None

			# Okay cool let's make the classroom and relate it to this user with a CTR
			classroom = Classroom(name=classroom_name, num_students=0)
			classroom.save()
			classroom_log(classroom, "Classroom created from start")
			ctr = ClassroomTeacherRel(user=user, classroom=classroom, date=timezone.now())
			ctr.save()

			analytics.person_append(user, "Classrooms", classroom.name)
			# Set the session value so we can keep track of which classroom they're monitoring
			# currently if they have multiple classes
			request.session['classroom_id'] = classroom.id

			send_mail_threaded("New educents teacher started a session - " + str(timezone.now()), "Username: "******"*****@*****.**", ["*****@*****.**"])

			# Now find and update (or create) the session
			try:
				session = ClassroomSession.objects.get(classroom=classroom)
				session.ip = ip
				session.update_time = timezone.now()
				session.save()				
			except ClassroomSession.DoesNotExist:
				make_session(request, classroom)

			# And give them the 20
			prof = user.profile
			prof.upgraded = True
			prof.num_licenses += 20
			prof.save()

			analytics.track_event("post_teacher_start", request)

			code_model.user = user
			code_model.save()
			
			return HttpResponseRedirect("/session/status/")			
	else:
		form = EducentsCodeForm()
		return renderWithNav(request, "educents.html", {"form":form, "code":code})