def join_school(request, school_id, referral_id=None): """ Makes an existing user into a Student, linked to the given school :param request: request to join school :param school_id: ID of school :return: redirect to school page """ sch = School.objects.get(id=school_id) stu = Student(user=request.user, school=sch) if sch.num_students() == 0: stu.isHead = True stu.save() # Referral points get added after object saved, so put all referral # stuff in the same place if referral_id: referrer = Student.objects.get(id=referral_id) stu.referred_by = referrer.user stu.save() recruited_new_student(referrer, sch) return HttpResponseRedirect('/cern')
def create_student(user, school_id, referrer_id): """ Creates a Student object to go with a new user. :param user: the User object linked to the Student :param school_id: the id of the School object :param referrer_id: a string containing the ID of the student who referred the new student equal to '' if no one referred new student """ school = School.objects.get(id=school_id) student = Student(user=user, school=school) if school.num_students() == 0: student.isHead = True student.save() school.save() signed_up(student) # Referral points happen only after student has been saved to db, # so put all referral stuff together if referrer_id: referrer = Student.objects.get(id=referrer_id) student.referred_id = referrer_id student.save() recruited_new_student(referrer, school) return student
class StudentTest(TestCase): def setUp(self): self.school1 = School(state = "AK", name = "A Fine University") self.school2 = School(state = "OH", name = "The Finest University") self.school1.save() self.school2.save() self.user1 = User(username = "******") self.user2 = User(username = "******") self.user3 = User(username = "******") self.user1.save() self.user2.save() self.user3.save() self.student1 = Student(user = self.user1, school = self.school1) self.student2 = Student(user = self.user2, school = self.school1, referred_by = self.user1) self.student3 = Student(user = self.user3, school = self.school2) self.student1.save() self.student2.save() self.student3.save() unittest.TestCase.setUp(self) def test_students_link_to_schools(self): self.assertEqual(self.school1, self.student1.school) self.assertEqual(self.school2, self.student3.school, "Student.school does not correctly point to school") def test_referrer_rep_link(self): old_rep = self.student1.rep points = random.randint(0, 100) self.student2.add_rep(points) rep_change = self.student1.rep - old_rep print rep_change self.assertEqual(rep_change, points, "Referrer does not get correct points added when referree gains points.")
class StudentTest(TestCase): def setUp(self): self.school1 = School(state="AK", name="A Fine University") self.school2 = School(state="OH", name="The Finest University") self.school1.save() self.school2.save() self.user1 = User(username="******") self.user2 = User(username="******") self.user3 = User(username="******") self.user1.save() self.user2.save() self.user3.save() self.student1 = Student(user=self.user1, school=self.school1) self.student2 = Student(user=self.user2, school=self.school1, referred_by=self.user1) self.student3 = Student(user=self.user3, school=self.school2) self.student1.save() self.student2.save() self.student3.save() unittest.TestCase.setUp(self) def test_students_link_to_schools(self): self.assertEqual(self.school1, self.student1.school) self.assertEqual(self.school2, self.student3.school, "Student.school does not correctly point to school") def test_referrer_rep_link(self): old_rep = self.student1.rep points = random.randint(0, 100) self.student2.add_rep(points) rep_change = self.student1.rep - old_rep print rep_change self.assertEqual( rep_change, points, "Referrer does not get correct points added when referree gains points." )
def register_school(request, school_id, referral_id=None): """ Renders the signup page for a new student at a certain school :param request: request to render page :param school_id: the ID of the school which the student will register with :param referral_id: an optional param which indicates a referral by another student :return: a simple register form (email + password) """ try: school = School.objects.get(id=school_id) except ObjectDoesNotExist: return HttpResponseRedirect('/cern/register/') else: referrer = None if referral_id: try: referrer = Student.objects.get(id=referral_id) except ObjectDoesNotExist: referral_id = None form = StudentRegistrationForm() if request.method == 'POST': form = StudentRegistrationForm(request.POST) if form.is_valid(): # Create and login user email = form.cleaned_data.get('email_address') password = form.cleaned_data.get('password') user = User(username=email, email=email, password=password) user.save() # Create student stu = Student(school=school, referred_id=referral_id, user=user) if school.num_students() == 0: stu.isHead = True stu.save() signed_up(stu) if referral_id: referrer = Student.objects.get(id=referral_id) recruited_new_student(referrer, school) # Create fan for user username = "******" % (user_name(user), "Head " if stu.isHead else "", stu.school.name) fan = FanPage(fan=user, username=username) fan.save() user.backend = "django.contrib.auth.backends.ModelBackend" login(request, user) role_controller = RoleController(user) # create the student role student_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_STUDENT, stu.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_STUDENT) ) # create fan role fan_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType(RoleController.ENTITY_FAN) ) # activate highest priority role (fan) change_current_role(request) return HttpResponseRedirect('/challenges/students') return render(request, 'spuddercern/pages/register_login.html', { 'school': school, 'referrer': referrer, 'form': form, 'base_url': settings.SPUDMART_BASE_URL })
def register_school(request, school_id, referral_id=None): """ Renders the signup page for a new student at a certain school :param request: request to render page :param school_id: the ID of the school which the student will register with :param referral_id: an optional param which indicates a referral by another student :return: a simple register form (email + password) """ try: school = School.objects.get(id=school_id) except ObjectDoesNotExist: return HttpResponseRedirect('/cern/register/') else: referrer = None if referral_id: try: referrer = Student.objects.get(id=referral_id) except ObjectDoesNotExist: referral_id = None form = StudentRegistrationForm() if request.method == 'POST': form = StudentRegistrationForm(request.POST) if form.is_valid(): # Create and login user email = form.cleaned_data.get('email_address') password = form.cleaned_data.get('password') user = User(username=email, email=email, password=password) user.save() # Create student stu = Student(school=school, referred_id=referral_id, user=user) if school.num_students() == 0: stu.isHead = True stu.save() signed_up(stu) if referral_id: referrer = Student.objects.get(id=referral_id) recruited_new_student(referrer, school) # Create fan for user username = "******" % (user_name( user), "Head " if stu.isHead else "", stu.school.name) fan = FanPage(fan=user, username=username) fan.save() user.backend = "django.contrib.auth.backends.ModelBackend" login(request, user) role_controller = RoleController(user) # create the student role student_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_STUDENT, stu.id, RoleBase.RoleWrapperByEntityType( RoleController.ENTITY_STUDENT)) # create fan role fan_role = role_controller.role_by_entity_type_and_entity_id( RoleController.ENTITY_FAN, fan.id, RoleBase.RoleWrapperByEntityType( RoleController.ENTITY_FAN)) # activate highest priority role (fan) change_current_role(request) return HttpResponseRedirect('/challenges/students') return render( request, 'spuddercern/pages/register_login.html', { 'school': school, 'referrer': referrer, 'form': form, 'base_url': settings.SPUDMART_BASE_URL })