def save(self, *args, **kwargs): """Save a Candidate object along with the user new account.""" cand_user = super(CandidateCreationForm, self).save(*args, **kwargs) candidate = Candidate(user=cand_user, term=Term.objects.get_current_term()) candidate.save() return candidate
def save(self, *args, **kwargs): """Save a Candidate object along with the user new account.""" cand_user = super(CandidateCreationForm, self).save(*args, **kwargs) candidate = Candidate(user=cand_user, term=Term.objects.get_current_term()) candidate.save() return candidate
def test_get_first_term_as_candidate(self): # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) self.assertIsNone(self.profile.get_first_term_as_candidate()) # Create Candidate for user in a past term: candidate = Candidate(user=self.user, term=self.term_old) candidate.save() self.assertEqual(self.profile.get_first_term_as_candidate(), self.term_old) # Create Candidate for user in current term, and past term should # still be marked as first term as candidate: candidate = Candidate(user=self.user, term=self.term) candidate.save() self.assertEqual(self.profile.get_first_term_as_candidate(), self.term_old) # Create user who only has initiation term data and no Candidate # objects: temp_user = get_user_model().objects.create_user( 'tester', '*****@*****.**', 'testpw') temp_user.first_name = 'Bentley' temp_user.last_name = 'Bent' temp_user.save() profile = self.model(user=temp_user) profile.save() self.assertIsNone(profile.get_first_term_as_candidate()) profile.initiation_term = self.term profile.save() self.assertEqual(profile.get_first_term_as_candidate(), self.term)
def get_context_data(self, **kwargs): context = super(CandidateProgressMixin, self).get_context_data(**kwargs) context['houses'] = House.objects.all() context['display_house'] = self.display_house candidates = Candidate.objects.filter(term=self.display_term) progress_by_candidate = Candidate.get_progress_by_candidate( candidates, self.display_term) # Exclude candidates not in the selected house for candidate in progress_by_candidate.keys(): if self.display_house and \ self.display_house.id not in \ candidate.user.housemember.filter( term=self.display_term).values_list( 'house', flat=True): del progress_by_candidate[candidate] context['progress'] = {} for candidate, progress in progress_by_candidate.items(): candidate_progress = { 'requirements': progress, 'all_requirements_complete': True } context['progress'][candidate] = candidate_progress for progress_item in progress: if progress_item['completed'] < progress_item['required']: candidate_progress['all_requirements_complete'] = False break return context
def get_context_data(self, **kwargs): kwargs['candidate'] = self.candidate context = super(CandidatePortalView, self).get_context_data(**kwargs) requirements = CandidateRequirement.objects.filter( term=self.current_term).select_related( 'eventcandidaterequirement', 'eventcandidaterequirement__event_type', 'challengecandidaterequirement', 'challengecandidaterequirement__challenge_type', 'examfilecandidaterequirement') # Initialize req_types to contain lists for every requirement type req_types = {} for req in CandidateRequirement.REQUIREMENT_TYPE_CHOICES: req_types[req[0]] = [] electives_required = Candidate.are_electives_required(self.candidate) for req in requirements: req_progress = req.get_progress(self.candidate) completed = req_progress['completed'] credits_needed = req_progress['required'] if electives_required and completed > credits_needed: completed = credits_needed entry = { 'completed': completed, 'credits_needed': credits_needed, 'requirement': req } req_type = req.requirement_type req_types[req_type].append(entry) context['req_types'] = req_types return context
def get_context_data(self, **kwargs): kwargs['candidate'] = self.candidate context = super(CandidatePortalView, self).get_context_data(**kwargs) requirements = CandidateRequirement.objects.filter( term=self.current_term).select_related( 'eventcandidaterequirement', 'eventcandidaterequirement__event_type', 'challengecandidaterequirement', 'challengecandidaterequirement__challenge_type', 'examfilecandidaterequirement') # Initialize req_types to contain lists for every requirement type req_types = {} for req in CandidateRequirement.REQUIREMENT_TYPE_CHOICES: req_types[req[0]] = [] electives_required = Candidate.are_electives_required(self.candidate) for req in requirements: req_progress = req.get_progress(self.candidate) completed = req_progress['completed'] credits_needed = req_progress['required'] if electives_required and completed > credits_needed: completed = credits_needed entry = { 'completed': completed, 'credits_needed': credits_needed, 'requirement': req } req_type = req.requirement_type req_types[req_type].append(entry) context['req_types'] = req_types return context
def test_get_first_term_as_candidate(self): # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) self.assertIsNone(self.profile.get_first_term_as_candidate()) # Create Candidate for user in a past term: candidate = Candidate(user=self.user, term=self.term_old) candidate.save() self.assertEqual(self.profile.get_first_term_as_candidate(), self.term_old) # Create Candidate for user in current term, and past term should # still be marked as first term as candidate: candidate = Candidate(user=self.user, term=self.term) candidate.save() self.assertEqual(self.profile.get_first_term_as_candidate(), self.term_old) # Create user who only has initiation term data and no Candidate # objects: temp_user = get_user_model().objects.create_user("tester", "*****@*****.**", "testpw") temp_user.first_name = "Bentley" temp_user.last_name = "Bent" temp_user.save() profile = self.model(user=temp_user) profile.save() self.assertIsNone(profile.get_first_term_as_candidate()) profile.initiation_term = self.term profile.save() self.assertEqual(profile.get_first_term_as_candidate(), self.term)
def test_is_candidate(self): """Ensure that basic is_candidate usage works as expected. See tests for StudentOrgUserProfilesTest for more extensive testing. """ # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) # Create Candidate for user in the current term: Candidate(user=self.user, term=self.term).save() # Should now be considered a candidate: self.assertTrue(self.profile.is_candidate())
def test_get_user_viewable(self): # self.user is just an ordinary user with no groups or special # permissions, so they should be able to view public, open, and # candidate events, but not member and officer events start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event_public = self.create_event(start_time, end_time, restriction=Event.PUBLIC) event_open = self.create_event(start_time, end_time, restriction=Event.OPEN) event_candidate = self.create_event(start_time, end_time, restriction=Event.CANDIDATE) event_member = self.create_event(start_time, end_time, restriction=Event.MEMBER) event_officer = self.create_event(start_time, end_time, restriction=Event.OFFICER) visible_events = Event.objects.get_user_viewable(self.user) expected_events = [event_public, event_open, event_candidate] self.assertQuerysetEqual(visible_events, [repr(event) for event in expected_events], ordered=False) # Make this user a candidate, and view the permissions should stay # the same Candidate(user=self.user, term=self.term).save() visible_events = Event.objects.get_user_viewable(self.user) self.assertQuerysetEqual(visible_events, [repr(event) for event in expected_events], ordered=False) # Make this user an officer, and they should be able to see all # events Officer(user=self.user, position=self.committee, term=self.term).save() visible_events = Event.objects.get_user_viewable(self.user) expected_events.extend([event_member, event_officer]) self.assertQuerysetEqual(visible_events, [repr(event) for event in expected_events], ordered=False)
def get_context_data(self, **kwargs): kwargs['candidate'] = self.candidate context = super(CandidateEditView, self).get_context_data(**kwargs) formset = self.get_form(self.form_class) # Initialize req_types to contain lists for every requirement type req_types = {} for req in CandidateRequirement.REQUIREMENT_TYPE_CHOICES: req_types[req[0]] = [] electives_required = Candidate.are_electives_required(self.candidate) for i, req in enumerate(self.requirements): progress = self.progress_dict.get(req.pk) form = formset[i] req_progress = req.get_progress(self.candidate) completed = req_progress['completed'] credits_needed = req_progress['required'] # this is here so on the candidate portal, it will only show # something like 3/3 events completed instead of 5/3 if # they have more than needed, since 2 of those 5 would be in # the electives section (unless electives arent required) if electives_required and completed > credits_needed: completed = credits_needed entry = { 'completed': completed, 'credits_needed': credits_needed, 'requirement': req, 'form': form } form.initial['alternate_credits_needed'] = credits_needed form.initial['manually_recorded_credits'] = 0 if progress: form.instance = progress form.initial['comments'] = progress.comments form.initial['manually_recorded_credits'] = completed req_type = req.requirement_type req_types[req_type].append(entry) context['req_types'] = req_types return context
def get_context_data(self, **kwargs): kwargs['candidate'] = self.candidate context = super(CandidateEditView, self).get_context_data(**kwargs) formset = self.get_form(self.form_class) # Initialize req_types to contain lists for every requirement type req_types = {} for req in CandidateRequirement.REQUIREMENT_TYPE_CHOICES: req_types[req[0]] = [] electives_required = Candidate.are_electives_required(self.candidate) for i, req in enumerate(self.requirements): progress = self.progress_dict.get(req.pk) form = formset[i] req_progress = req.get_progress(self.candidate) completed = req_progress['completed'] credits_needed = req_progress['required'] # this is here so on the candidate portal, it will only show # something like 3/3 events completed instead of 5/3 if # they have more than needed, since 2 of those 5 would be in # the electives section (unless electives arent required) if electives_required and completed > credits_needed: completed = credits_needed entry = { 'completed': completed, 'credits_needed': credits_needed, 'requirement': req, 'form': form } form.initial['alternate_credits_needed'] = credits_needed form.initial['manually_recorded_credits'] = 0 if progress: form.instance = progress form.initial['comments'] = progress.comments form.initial['manually_recorded_credits'] = completed req_type = req.requirement_type req_types[req_type].append(entry) context['req_types'] = req_types return context
def test_can_user_sign_up(self): # self.user is just an ordinary user with no groups or special # permissions, so the user should only be able to sign up for public # events restrictions = [ Event.PUBLIC, Event.OPEN, Event.CANDIDATE, Event.MEMBER, Event.OFFICER ] start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) for restriction in restrictions: event.restriction = restriction event.save() if restriction == Event.PUBLIC: self.assert_can_sign_up(event, self.user) else: self.assert_cannot_sign_up(event, self.user) # Make this user a candidate, so the user should be able to sign up # for public and candidate events Candidate(user=self.user, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() if restriction == Event.PUBLIC or restriction == Event.CANDIDATE: self.assert_can_sign_up(event, self.user) else: self.assert_cannot_sign_up(event, self.user) # Make this user an officer, so the user should be able to sign up # for all events except open events (which don't allow signups) Officer(user=self.user, position=self.committee, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() if restriction == Event.OPEN: self.assert_cannot_sign_up(event, self.user) else: self.assert_can_sign_up(event, self.user)
def test_can_user_view(self): # self.user is just an ordinary user with no groups or special # permissions, so they should be able to view public, open, and # candidate events, but not member and officer events restrictions = [ Event.PUBLIC, Event.OPEN, Event.CANDIDATE, Event.MEMBER, Event.OFFICER ] start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) for restriction in restrictions: event.restriction = restriction event.save() if restriction in Event.VISIBLE_TO_EVERYONE: self.assert_can_view(event, self.user) else: self.assert_cannot_view(event, self.user) # Make this user a candidate, and view the permissions should stay # the same Candidate(user=self.user, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() if restriction in Event.VISIBLE_TO_EVERYONE: self.assert_can_view(event, self.user) else: self.assert_cannot_view(event, self.user) # Make this user an officer, and they should be able to see all # events Officer(user=self.user, position=self.committee, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() self.assert_can_view(event, self.user)
def setUp(self): self.candidate_group = Group.objects.create(name='Current Candidate') self.member_group = Group.objects.create(name='Member') user_model = get_user_model() # Create candidate self.user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Random', last_name='Candidate') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.candidate = Candidate(user=self.user, term=self.term) self.candidate.save() # Create officer officer_user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Joe', last_name='Officer') committee = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') committee.save() self.officer = Officer(user=officer_user, position=committee, term=self.term, is_chair=False) self.officer.save() # Create some manual candidate requirements self.manual_req1 = ManualCandidateRequirement( name='Manual 1', requirement_type=CandidateRequirement.MANUAL, credits_needed=2, term=self.term) self.manual_req1.save() self.manual_req2 = ManualCandidateRequirement( name='Manual 2', requirement_type=CandidateRequirement.MANUAL, credits_needed=5, term=self.term) self.manual_req2.save() # Create a challenge type self.individual_challenge_type = ChallengeType(name='Individual') self.individual_challenge_type.save() # Create a challenge requirement self.challenge_req = ChallengeCandidateRequirement( challenge_type=self.individual_challenge_type, credits_needed=3, term=self.term) self.challenge_req.save() # Create some events and event requirement self.event_type1 = EventType(name='Fun') self.event_type1.save() self.event_type2 = EventType(name='Not Fun') self.event_type2.save() self.fun_event1 = Event(name='Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event1.save() self.fun_event2 = Event(name='Big Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), requirements_credit=2, term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event2.save() self.notfun_event = Event(name='Not Fun Event', event_type=self.event_type2, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.notfun_event.save() self.event_req = EventCandidateRequirement( event_type=self.event_type1, credits_needed=4, term=self.term) self.event_req.save() # Create some exam files and exam files requirement test_file = open('test.txt', 'w+') test_file.write('This is a test file.') self.test_exam1 = Exam( course_instance=CourseInstance.objects.get(pk=10000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam1.save() self.test_exam1.course_instance.course.department.save() self.test_exam2 = Exam( course_instance=CourseInstance.objects.get(pk=20000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam2.save() self.test_exam2.course_instance.course.department.save() self.exam_req = ExamFileCandidateRequirement( credits_needed=2, term=self.term) self.exam_req.save()
class CandidateTest(TestCase): fixtures = ['test/course_instance.yaml'] def setUp(self): self.candidate_group = Group.objects.create(name='Current Candidate') self.member_group = Group.objects.create(name='Member') user_model = get_user_model() # Create candidate self.user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Random', last_name='Candidate') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.candidate = Candidate(user=self.user, term=self.term) self.candidate.save() # Create officer officer_user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Joe', last_name='Officer') committee = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') committee.save() self.officer = Officer(user=officer_user, position=committee, term=self.term, is_chair=False) self.officer.save() # Create some manual candidate requirements self.manual_req1 = ManualCandidateRequirement( name='Manual 1', requirement_type=CandidateRequirement.MANUAL, credits_needed=2, term=self.term) self.manual_req1.save() self.manual_req2 = ManualCandidateRequirement( name='Manual 2', requirement_type=CandidateRequirement.MANUAL, credits_needed=5, term=self.term) self.manual_req2.save() # Create a challenge type self.individual_challenge_type = ChallengeType(name='Individual') self.individual_challenge_type.save() # Create a challenge requirement self.challenge_req = ChallengeCandidateRequirement( challenge_type=self.individual_challenge_type, credits_needed=3, term=self.term) self.challenge_req.save() # Create some events and event requirement self.event_type1 = EventType(name='Fun') self.event_type1.save() self.event_type2 = EventType(name='Not Fun') self.event_type2.save() self.fun_event1 = Event(name='Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event1.save() self.fun_event2 = Event(name='Big Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), requirements_credit=2, term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event2.save() self.notfun_event = Event(name='Not Fun Event', event_type=self.event_type2, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.notfun_event.save() self.event_req = EventCandidateRequirement( event_type=self.event_type1, credits_needed=4, term=self.term) self.event_req.save() # Create some exam files and exam files requirement test_file = open('test.txt', 'w+') test_file.write('This is a test file.') self.test_exam1 = Exam( course_instance=CourseInstance.objects.get(pk=10000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam1.save() self.test_exam1.course_instance.course.department.save() self.test_exam2 = Exam( course_instance=CourseInstance.objects.get(pk=20000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam2.save() self.test_exam2.course_instance.course.department.save() self.exam_req = ExamFileCandidateRequirement( credits_needed=2, term=self.term) self.exam_req.save() @classmethod def tearDownClass(cls): os.remove('test.txt') shutil.rmtree(os.path.join(settings.WORKSPACE_ROOT, 'media', 'tests'), ignore_errors=True) def test_candidate_post_save(self): student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) # Candidate has already been saved for this user created above, so # StudentOrgUserProfile should exist: self.assertIsNotNone(student_org_profile) # Candidate has not been marked as initiated, so initiation_term should # be None in their profile, and the candidate should be in the Current # Candidate group and not in the Member group: self.assertIsNone(student_org_profile.initiation_term) candidate_groups = self.candidate.user.groups.all() self.assertIn(self.candidate_group, candidate_groups) self.assertNotIn(self.member_group, candidate_groups) # Mark candidate as initiated, so profile and groups should update to # match: self.candidate.initiated = True self.candidate.save() student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) self.assertEqual(student_org_profile.initiation_term, self.term) candidate_groups = self.candidate.user.groups.all() self.assertNotIn(self.candidate_group, candidate_groups) self.assertIn(self.member_group, candidate_groups) def test_manual_requirements(self): """Test that credits for manual requirements are counted correctly.""" # Create some candidate progress CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req1, manually_recorded_credits=2, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 2) self.assertEqual(progress['required'], 7) # Test the simple summing of credits progress2 = CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req2, manually_recorded_credits=3, alternate_credits_needed=5) progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 7) # Test alternate credit requirement progress2.alternate_credits_needed = 4 progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 6) def test_challenge_requirements(self): """Test that credits for challenges are counted correctly.""" # Make sure unverified challenges don't count challenge = Challenge( candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, challenge_type=self.individual_challenge_type) challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 0) self.assertEqual(progress['required'], 3) # ...and verified challenges do count challenge.verified = True challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 3) # Test alternate credit requirement CandidateRequirementProgress( candidate=self.candidate, requirement=self.challenge_req, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 2) def test_event_requirements(self): """Test that credits for events are counted correctly based on attendance. """ # Attend Fun Event EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 1) # Attend Big Fun Event (worth 2) EventAttendance(event=self.fun_event2, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) # Attend Not Fun Event (not worth any requirements) EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) def test_exams_requirements(self): """Test that credits for exam files are counted correctly.""" # Upload 1 verified exam self.test_exam1.submitter = self.user self.test_exam1.save() self.assertEqual(self.user, self.test_exam1.submitter) progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) # Upload 2 verified exams self.test_exam2.submitter = self.user self.test_exam2.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 2) # Unverify an exam (doesn't count anymore) self.test_exam1.verified = False self.test_exam1.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) def test_get_total_progress(self): """Test get_progress where requirement_type is not specified and total progress for all requirements is returned. """ # Get all the requirements that were created: requirements = CandidateRequirement.objects.filter(term=self.term) num_required = 0 for requirement in requirements: num_required += requirement.credits_needed progress = self.candidate.get_progress() # Ensure that the method returns the same value as calculated manually. # This value should not change over the course of the tests. self.assertEqual(progress['required'], num_required) # Haven't assigned any completions yet: total_completed = 0 self.assertEqual(progress['completed'], total_completed) # Attend Fun Event for fun event requirement EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) total_completed += 1 self.assertEqual(progress['completed'], total_completed) # Complete a challenge: Challenge(candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, verified=True, challenge_type=self.individual_challenge_type).save() progress = self.candidate.get_progress() total_completed += 1 self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed) # Attend an event not worth any requirements EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed)
def test_is_candidate(self): # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) # Create Candidate for user in a past term: candidate = Candidate(user=self.user, term=self.term_old) candidate.save() # Should now be considered a candidate only if current=False: self.assertTrue(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Mark that candidate as initiated: candidate.initiated = True candidate.save() # Re-fetch profile since candidate save affects StudentOrgUserProfile # initiation_term field: self.profile = get_object_or_none(self.model, user=self.user) self.assertFalse(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Mark initiated as False, and create new Candidate for the current # Term, as is the case for a candidate who doesn't initiate one term # and returns as a candidate a later term: candidate.initiated = False candidate.save() candidate = Candidate(user=self.user, term=self.term) candidate.save() self.profile = get_object_or_none(self.model, user=self.user) # Should be now considered a candidate (initiated not True): self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True)) # Mark that candidate as initiated: candidate.initiated = True candidate.save() self.profile = get_object_or_none(self.model, user=self.user) self.assertFalse(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Change the 'current' semester to an old semester: self.term_old.current = True self.term_old.save() # Now the candidate should be considered a candidate since they have # not _yet_ initiated based on what the current semester is, but # they are in the database as a candidate for the current semester: self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True)) # Ensure that they are also marked as a candidate if initiated = False candidate.initiated = False candidate.save() self.profile = get_object_or_none(self.model, user=self.user) self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True))
def test_is_candidate(self): # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) # Create Candidate for user in a past term: candidate = Candidate(user=self.user, term=self.term_old) candidate.save() # Should now be considered a candidate only if current=False: self.assertTrue(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Mark that candidate as initiated: candidate.initiated = True candidate.save() # Re-fetch profile since candidate save affects StudentOrgUserProfile # initiation_term field: self.profile = get_object_or_none(self.model, user=self.user) self.assertFalse(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Mark initiated as False, and create new Candidate for the current # Term, as is the case for a candidate who doesn't initiate one term # and returns as a candidate a later term: candidate.initiated = False candidate.save() candidate = Candidate(user=self.user, term=self.term) candidate.save() self.profile = get_object_or_none(self.model, user=self.user) # Should be now considered a candidate (initiated not True): self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True)) # Mark that candidate as initiated: candidate.initiated = True candidate.save() self.profile = get_object_or_none(self.model, user=self.user) self.assertFalse(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Change the 'current' semester to an old semester: self.term_old.current = True self.term_old.save() # Now the candidate should be considered a candidate since they have # not _yet_ initiated based on what the current semester is, but # they are in the database as a candidate for the current semester: self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True)) # Ensure that they are also marked as a candidate if initiated = False candidate.initiated = False candidate.save() self.profile = get_object_or_none(self.model, user=self.user) self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True))
def setUp(self): self.candidate_group = Group.objects.create(name='Current Candidate') self.member_group = Group.objects.create(name='Member') user_model = get_user_model() # Create candidate self.user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Random', last_name='Candidate') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.candidate = Candidate(user=self.user, term=self.term) self.candidate.save() # Create officer officer_user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Joe', last_name='Officer') committee = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') committee.save() self.officer = Officer(user=officer_user, position=committee, term=self.term, is_chair=False) self.officer.save() # Create some manual candidate requirements self.manual_req1 = ManualCandidateRequirement( name='Manual 1', requirement_type=CandidateRequirement.MANUAL, credits_needed=2, term=self.term) self.manual_req1.save() self.manual_req2 = ManualCandidateRequirement( name='Manual 2', requirement_type=CandidateRequirement.MANUAL, credits_needed=5, term=self.term) self.manual_req2.save() # Create a challenge type self.individual_challenge_type = ChallengeType(name='Individual') self.individual_challenge_type.save() # Create a challenge requirement self.challenge_req = ChallengeCandidateRequirement( challenge_type=self.individual_challenge_type, credits_needed=3, term=self.term) self.challenge_req.save() # Create some events and event requirement self.event_type1 = EventType(name='Fun') self.event_type1.save() self.event_type2 = EventType(name='Not Fun') self.event_type2.save() self.fun_event1 = Event(name='Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event1.save() self.fun_event2 = Event(name='Big Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), requirements_credit=2, term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event2.save() self.notfun_event = Event(name='Not Fun Event', event_type=self.event_type2, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.notfun_event.save() self.event_req = EventCandidateRequirement( event_type=self.event_type1, credits_needed=4, term=self.term) self.event_req.save() # Create some exam files and exam files requirement test_file = open('test.txt', 'w+') test_file.write('This is a test file.') self.test_exam1 = Exam( course_instance=CourseInstance.objects.get(pk=10000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam1.save() self.test_exam1.course_instance.course.department.save() self.test_exam2 = Exam( course_instance=CourseInstance.objects.get(pk=20000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam2.save() self.test_exam2.course_instance.course.department.save() self.exam_req = ExamFileCandidateRequirement( credits_needed=2, term=self.term) self.exam_req.save()
class CandidateTest(TestCase): fixtures = ['test/course_instance.yaml'] def setUp(self): self.candidate_group = Group.objects.create(name='Current Candidate') self.member_group = Group.objects.create(name='Member') user_model = get_user_model() # Create candidate self.user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Random', last_name='Candidate') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.candidate = Candidate(user=self.user, term=self.term) self.candidate.save() # Create officer officer_user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Joe', last_name='Officer') committee = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') committee.save() self.officer = Officer(user=officer_user, position=committee, term=self.term, is_chair=False) self.officer.save() # Create some manual candidate requirements self.manual_req1 = ManualCandidateRequirement( name='Manual 1', requirement_type=CandidateRequirement.MANUAL, credits_needed=2, term=self.term) self.manual_req1.save() self.manual_req2 = ManualCandidateRequirement( name='Manual 2', requirement_type=CandidateRequirement.MANUAL, credits_needed=5, term=self.term) self.manual_req2.save() # Create a challenge type self.individual_challenge_type = ChallengeType(name='Individual') self.individual_challenge_type.save() # Create a challenge requirement self.challenge_req = ChallengeCandidateRequirement( challenge_type=self.individual_challenge_type, credits_needed=3, term=self.term) self.challenge_req.save() # Create some events and event requirement self.event_type1 = EventType(name='Fun') self.event_type1.save() self.event_type2 = EventType(name='Not Fun') self.event_type2.save() self.fun_event1 = Event(name='Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event1.save() self.fun_event2 = Event(name='Big Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), requirements_credit=2, term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event2.save() self.notfun_event = Event(name='Not Fun Event', event_type=self.event_type2, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.notfun_event.save() self.event_req = EventCandidateRequirement( event_type=self.event_type1, credits_needed=4, term=self.term) self.event_req.save() # Create some exam files and exam files requirement test_file = open('test.txt', 'w+') test_file.write('This is a test file.') self.test_exam1 = Exam( course_instance=CourseInstance.objects.get(pk=10000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam1.save() self.test_exam1.course_instance.course.department.save() self.test_exam2 = Exam( course_instance=CourseInstance.objects.get(pk=20000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam2.save() self.test_exam2.course_instance.course.department.save() self.exam_req = ExamFileCandidateRequirement( credits_needed=2, term=self.term) self.exam_req.save() @classmethod def tearDownClass(cls): os.remove('test.txt') shutil.rmtree(os.path.join(settings.WORKSPACE_ROOT, 'media', 'tests'), ignore_errors=True) def test_candidate_post_save(self): student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) # Candidate has already been saved for this user created above, so # StudentOrgUserProfile should exist: self.assertIsNotNone(student_org_profile) # Candidate has not been marked as initiated, so initiation_term should # be None in their profile, and the candidate should be in the Current # Candidate group and not in the Member group: self.assertIsNone(student_org_profile.initiation_term) candidate_groups = self.candidate.user.groups.all() self.assertIn(self.candidate_group, candidate_groups) self.assertNotIn(self.member_group, candidate_groups) # Mark candidate as initiated, so profile and groups should update to # match: self.candidate.initiated = True self.candidate.save() student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) self.assertEqual(student_org_profile.initiation_term, self.term) candidate_groups = self.candidate.user.groups.all() self.assertNotIn(self.candidate_group, candidate_groups) self.assertIn(self.member_group, candidate_groups) def test_manual_requirements(self): """Test that credits for manual requirements are counted correctly.""" # Create some candidate progress CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req1, manually_recorded_credits=2, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 2) self.assertEqual(progress['required'], 7) # Test the simple summing of credits progress2 = CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req2, manually_recorded_credits=3, alternate_credits_needed=5) progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 7) # Test alternate credit requirement progress2.alternate_credits_needed = 4 progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 6) def test_challenge_requirements(self): """Test that credits for challenges are counted correctly.""" # Make sure unverified challenges don't count challenge = Challenge( candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, challenge_type=self.individual_challenge_type) challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 0) self.assertEqual(progress['required'], 3) # ...and verified challenges do count challenge.verified = True challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 3) # Test alternate credit requirement CandidateRequirementProgress( candidate=self.candidate, requirement=self.challenge_req, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 2) def test_event_requirements(self): """Test that credits for events are counted correctly based on attendance. """ # Attend Fun Event EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 1) # Attend Big Fun Event (worth 2) EventAttendance(event=self.fun_event2, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) # Attend Not Fun Event (not worth any requirements) EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) def test_exams_requirements(self): """Test that credits for exam files are counted correctly.""" # Upload 1 verified exam self.test_exam1.submitter = self.user self.test_exam1.save() self.assertEqual(self.user, self.test_exam1.submitter) progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) # Upload 2 verified exams self.test_exam2.submitter = self.user self.test_exam2.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 2) # Unverify an exam (doesn't count anymore) self.test_exam1.verified = False self.test_exam1.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) def test_get_total_progress(self): """Test get_progress where requirement_type is not specified and total progress for all requirements is returned. """ # Get all the requirements that were created: requirements = CandidateRequirement.objects.filter(term=self.term) num_required = 0 for requirement in requirements: num_required += requirement.credits_needed progress = self.candidate.get_progress() # Ensure that the method returns the same value as calculated manually. # This value should not change over the course of the tests. self.assertEqual(progress['required'], num_required) # Haven't assigned any completions yet: total_completed = 0 self.assertEqual(progress['completed'], total_completed) # Attend Fun Event for fun event requirement EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) total_completed += 1 self.assertEqual(progress['completed'], total_completed) # Complete a challenge: Challenge(candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, verified=True, challenge_type=self.individual_challenge_type).save() progress = self.candidate.get_progress() total_completed += 1 self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed) # Attend an event not worth any requirements EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed)
def test_multiple_project_report_attendances(self): start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) project_report = ProjectReport.objects.create( term=self.term, date=datetime.date.today(), title='Test project report', author=self.user, committee=self.committee) event.project_report = project_report event.save() candidate1 = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Fake', last_name='Candidate1') Candidate(user=candidate1, term=self.term).save() candidate2 = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Fake', last_name='Candidate2') Candidate(user=candidate2, term=self.term).save() officer = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Fake', last_name='Officer') Officer(user=officer, position=self.committee, term=self.term).save() member = self.user StudentOrgUserProfile(user=member, initiation_term=self.term).save() member.save() officer_attendance = EventAttendance.objects.create(user=officer, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) candidate1_attendance = EventAttendance.objects.create(user=candidate1, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate1)]) self.assertQuerysetEqual(project_report.member_list.all(), []) member_attendance = EventAttendance.objects.create(user=member, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate1)]) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) candidate2_attendance = EventAttendance.objects.create(user=candidate2, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual( project_report.candidate_list.all(), [repr(candidate1), repr(candidate2)], ordered=False) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) candidate1_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate2)]) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) officer_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate2)]) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) candidate2_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) member_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [])
def test_project_report_attendance(self): start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) project_report = ProjectReport.objects.create( term=self.term, date=datetime.date.today(), title='Test project report', author=self.user, committee=self.committee) event.project_report = project_report event.save() # self.user is neither an officer, a candidate, nor a member, so # recording attendance should not affect any attendance list attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) # Make this user a candidate, so recording attendance should affect # the candidate list Candidate(user=self.user, term=self.term).save() attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(self.user)]) self.assertQuerysetEqual(project_report.member_list.all(), []) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) # Make this user a member, so recording attendance should affect # the member list self.user.studentorguserprofile.initiation_term = self.term self.user.save() attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [repr(self.user)]) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) # Make this user an officer, so recording attendance should affect the # officer list Officer(user=self.user, position=self.committee, term=self.term).save() attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(self.user)]) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [])