def home(request): ip = request.META.get('REMOTE_ADDR', None) if request.user.is_authenticated(): bl = ButtonLog(page="home", name="visit", user=request.user, ip=ip, time=timezone.now()) else: bl = ButtonLog(page="home", name="visit", ip=ip, time=timezone.now()) bl.save() analytics.track_event("visit_home", request) obj = {} obj['bought'] = False obj['have_email'] = "false" obj['newslettered'] = False if request.GET.has_key('yup'): obj['newslettered'] = True if not request.user.is_anonymous(): #ew hack if request.user.email == "*****@*****.**" or request.user.email == "": obj['have_email'] = "false" else: obj['have_email'] = "true" if GamePurchase.objects.filter(user=request.user).exists(): obj['bought'] = True return renderWithNav(request, 'home.html', obj)
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!"})
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!"})
def purchase_stripe(request, code): token = request.POST['stripeToken'] email = request.POST['stripeEmail'] purchase_class = purchase_classes[code] price_cents = purchase_class.get_price_cents(request.POST) email_body = purchase_class.get_email_body(token) if request.user.is_authenticated(): user = request.user username = user.username else: user = None username = "" pr = PurchaseRecord( user = user, stripe_token=token, email=email, code=code, params=str(request.POST), price=price_cents, date=timezone.now()) pr.save() try: charge = stripe.Charge.create( amount=int(price_cents), # amount in cents, again currency="usd", card=token, description=username, ) except stripe.CardError: return renderWithNav(request, "error.html", {"body":"Your card was declined."}) analytics.track_event("purchase_" + code, request=request, data={"price_cents":int(price_cents)}) purchase_class.purchase_action(user, request.POST) send_mail_threaded("Mathbreakers.com Purchase Receipt", email_body, "*****@*****.**", [request.POST['stripeEmail']]) message = "email %s made a purchase of %s for $%.2f" % (email, code, int(price_cents) / 100.0,) subject = "Payment received " + str(timezone.now()) send_mail_threaded(subject, message, "*****@*****.**", ["*****@*****.**"]) return HttpResponseRedirect(purchase_class.post_purchase_page)
def buttonlog(request, name): if "fakelogin" in request.COOKIES: return json_response({'result':'success'}) ip = request.META.get('REMOTE_ADDR', None) user = None if request.user.is_authenticated(): user = request.user bl = ButtonLog( page="", name=name, user=user, ip=ip, time=timezone.now(), tracking_cookie=tracking_cookie(request)) bl.save() if name in button_analytics_tracking: event = button_analytics_tracking[name] analytics.track_event(event, request=request) return json_response({'result':'success'})
def later(request): if request.method == "POST": later_form = LobbyLaterForm(request.POST) valid, desc = process_later_form(later_form, request) if valid: analytics.track_event("post_teacher_later", request) return renderWithNav(request, "postlater.html", {"description":desc}) else: return renderWithNav(request, "teacher/laterform.html", { "later_form":later_form }) else: analytics.track_event("visit_teacher_later", request) if request.user.is_authenticated(): later_form = LobbyLaterForm(initial={"email":request.user.email}) else: later_form = LobbyLaterForm() return renderWithNav(request, "teacher/laterform.html", { "later_form":later_form })
def status(request): # Some people are finding this page from google somehow, so let's make sure it # doesn't throw a 500 error if they don't have a login! if not request.user.is_authenticated(): return HttpResponseRedirect("/session/try/") analytics.track_event("visit_teacher_status", request=request) handle_class_selection(request) classroom, all_classes = get_classroom(request) ip = get_ip(request) classroom_log(classroom, "Visit status page, ip = '%s'" % ip) try: session = ClassroomSession.objects.get(classroom=classroom) if not request.COOKIES.has_key("fakelogin"): session.ip = ip session.save() except ClassroomSession.DoesNotExist: session = make_session(request, classroom) return renderWithNav(request, "teacher/session.html", {"profile":request.user.profile, "classroom":classroom, "classrooms":all_classes, "session":session})
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(), })
def teacher_try(request): analytics.track_event("visit_teacher_try", request) if request.user.is_authenticated() and ClassroomTeacherRel.objects.filter(user=request.user).exists(): return mbredirect("/session/selectclass") return renderWithNav(request, "teacher/try.html")
def handler(request): event_name = "visit_" + request.path.strip("/").replace("/","_") analytics.track_event(event_name, request) return renderWithNav(request, template)
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})