def test_accept_request_on_update_when_coaching_logged_in_user(self): renesmee = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, renesmee) coaches_json = [self.make_user_json(jacob, True)] coaches.update_coaches_and_requests(renesmee, coaches_json) self.assertTrue(renesmee.is_visible_to(jacob)) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000) self.assertEqual(0, len(requests_for_renesmee))
def test_ignore_nonexistent_requester_email_on_update_requests(self): renesmee = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, renesmee) coaches_json = [{ 'email': '*****@*****.**', 'isCoachingLoggedInUser': False, }] coaches.update_requests(renesmee, coaches_json) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000) self.assertEqual(0, len(requests_for_renesmee))
def test_clear_request_on_update_requests_with_no_email(self): bella = self.make_user('*****@*****.**') edward = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(edward, bella) coaches.update_requests(bella, []) requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000) self.assertEqual(0, len(requests_for_bella)) requests_by_edward = CoachRequest.get_for_coach(edward).fetch(1000) self.assertEqual(0, len(requests_by_edward))
def test_accept_request_on_update_when_coaching_logged_in_user(self): renesmee = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, renesmee) coaches_json = [self.make_user_json(jacob, True)] coaches.update_coaches_and_requests(renesmee, coaches_json) self.assertTrue(renesmee.is_visible_to(jacob)) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch( 1000) self.assertEqual(0, len(requests_for_renesmee))
def test_clear_request_on_update_requests_with_no_email(self): bella = self.make_user('*****@*****.**') edward = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(edward, bella) coaches.update_requests(bella, []) requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000) self.assertEqual(0, len(requests_for_bella)) requests_by_edward = CoachRequest.get_for_coach(edward).fetch(1000) self.assertEqual(0, len(requests_by_edward))
def test_noop_on_update_requests_with_email(self): bella = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, bella) coaches.update_requests(bella, [jacob.key_email]) requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000) self.assertEqual(1, len(requests_for_bella)) requests_by_jacob = CoachRequest.get_for_coach(jacob).fetch(1000) self.assertEqual(1, len(requests_by_jacob))
def test_ignore_nonexistent_requester_email_on_update_requests(self): renesmee = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, renesmee) coaches_json = [{ 'email': '*****@*****.**', 'isCoachingLoggedInUser': False, }] coaches.update_requests(renesmee, coaches_json) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch( 1000) self.assertEqual(0, len(requests_for_renesmee))
def test_noop_on_update_requests_with_email(self): bella = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, bella) coaches.update_requests(bella, [jacob.key_email]) requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000) self.assertEqual(1, len(requests_for_bella)) requests_by_jacob = CoachRequest.get_for_coach(jacob).fetch(1000) self.assertEqual(1, len(requests_by_jacob))
def test_noop_on_update_when_not_coaching_logged_in_user(self): # Bella + Edward's daughter, # (Spoiler Alert!) who Jacob falls in love with in Book 4 renesmee = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, renesmee) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000) self.assertEqual(1, len(requests_for_renesmee)) coaches_json = [self.make_user_json(jacob, False)] coaches.update_coaches_and_requests(renesmee, coaches_json) self.assertFalse(renesmee.is_visible_to(jacob)) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch(1000) self.assertEqual(1, len(requests_for_renesmee))
def test_noop_on_update_when_not_coaching_logged_in_user(self): # Bella + Edward's daughter, # (Spoiler Alert!) who Jacob falls in love with in Book 4 renesmee = self.make_user('*****@*****.**') jacob = self.make_user('*****@*****.**') CoachRequest.get_or_insert_for(jacob, renesmee) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch( 1000) self.assertEqual(1, len(requests_for_renesmee)) coaches_json = [self.make_user_json(jacob, False)] coaches.update_coaches_and_requests(renesmee, coaches_json) self.assertFalse(renesmee.is_visible_to(jacob)) requests_for_renesmee = CoachRequest.get_for_student(renesmee).fetch( 1000) self.assertEqual(1, len(requests_for_renesmee))
def test_requesting_by_username_doesnt_leak_email(self): bella = self.make_user('*****@*****.**') self.assertTrue(bella.claim_username('bella')) jacob = self.make_user('*****@*****.**') # Request is made using username. CoachRequest.get_or_insert_for(jacob, bella, 'bella') requests_for_bella = CoachRequest.get_for_student(bella).fetch(1000) self.assertEqual(1, len(requests_for_bella)) self.assertEqual(bella.username, requests_for_bella[0].student_requested_identifier) requests_by_jacob = CoachRequest.get_for_coach(jacob).fetch(1000) self.assertEqual(1, len(requests_by_jacob)) self.assertEqual(bella.username, requests_by_jacob[0].student_requested_identifier)
def update_requests(user_data, requester_emails): """ Remove all CoachRequests not represented by requester_emails. """ current_requests = CoachRequest.get_for_student(user_data) for current_request in current_requests: coach_email = current_request.coach_requesting_data.email if coach_email not in requester_emails: current_request.delete()
def update_requests(user_data, requester_emails): """ Remove all CoachRequests not represented by requester_emails. """ current_requests = CoachRequest.get_for_student(user_data) for current_request in current_requests: coach_email = current_request.coach_requesting_data.email if coach_email not in requester_emails: current_request.delete()
def get(self): user_data = UserData.current() user_data_override = self.request_user_data("coach_email") if user_util.is_current_user_developer() and user_data_override: user_data = user_data_override invalid_student = self.request_bool("invalid_student", default=False) coach_requests = [req.student_requested_identifier for req in CoachRequest.get_for_coach(user_data) if req.student_requested_data] student_lists_models = StudentList.get_for_coach(user_data.key()) student_lists_list = [] for student_list in student_lists_models: student_lists_list.append({ 'key': str(student_list.key()), 'name': student_list.name, }) student_lists_dict = dict((g['key'], g) for g in student_lists_list) def student_to_dict(s): """Convert the UserData s to a dictionary for rendering.""" return { 'key': str(s.key()), 'email': s.email, 'username': s.username, # Note that child users don't have an email. 'identifier': s.username or s.email, 'nickname': s.nickname, 'profile_root': s.profile_root, 'can_modify_coaches': s.can_modify_coaches(), 'studentLists': [l for l in [student_lists_dict.get(str(list_id)) for list_id in s.student_lists] if l], } students_data = user_data.get_students_data() students = map(student_to_dict, students_data) students.sort(key=lambda s: s['nickname']) child_accounts = map(student_to_dict, user_data.get_child_users()) template_values = { 'students': students, 'child_accounts': child_accounts, 'child_account_keyset': set([c['key'] for c in child_accounts]), 'students_json': json.dumps(students), 'student_lists': student_lists_list, 'student_lists_json': json.dumps(student_lists_list), 'invalid_student': invalid_student, 'coach_requests': coach_requests, 'coach_requests_json': json.dumps(coach_requests), 'selected_nav_link': 'coach', 'email': user_data.email, } self.render_jinja2_template('viewstudentlists.html', template_values)
def get_coach_and_requester_profiles_for_student(student_user_data): coach_profiles = [] for coach_user_data in student_user_data.get_coaches_data(): profile = UserProfile._from_coach(coach_user_data, student_user_data) coach_profiles.append(profile) requests = CoachRequest.get_for_student(student_user_data) for request in requests: coach_user_data = request.coach_requesting_data profile = UserProfile._from_coach(coach_user_data, student_user_data) coach_profiles.append(profile) return coach_profiles
def get_coach_and_requester_profiles_for_student(student_user_data): coach_profiles = [] for coach_user_data in student_user_data.get_coaches_data(): profile = UserProfile._from_coach(coach_user_data, student_user_data) coach_profiles.append(profile) requests = CoachRequest.get_for_student(student_user_data) for request in requests: coach_user_data = request.coach_requesting_data profile = UserProfile._from_coach(coach_user_data, student_user_data) coach_profiles.append(profile) return coach_profiles
def post(self): user_data = UserData.current() user_data_student = self.request_student_user_data() if user_data_student: if not user_data_student.is_coached_by(user_data): coach_request = CoachRequest.get_or_insert_for(user_data, user_data_student) if coach_request: if not self.is_ajax_request(): self.redirect("/students") return if self.is_ajax_request(): self.response.set_status(404) else: self.redirect("/students?invalid_student=1")
def get(self): user_data = UserData.current() user_data_override = self.request_user_data("coach_email") if user_util.is_current_user_developer() and user_data_override: user_data = user_data_override invalid_student = self.request_bool("invalid_student", default = False) coach_requests = [x.student_requested_data.email for x in CoachRequest.get_for_coach(user_data) if x.student_requested_data] student_lists_models = StudentList.get_for_coach(user_data.key()) student_lists_list = []; for student_list in student_lists_models: student_lists_list.append({ 'key': str(student_list.key()), 'name': student_list.name, }) student_lists_dict = dict((g['key'], g) for g in student_lists_list) students_data = user_data.get_students_data() students = map(lambda s: { 'key': str(s.key()), 'email': s.email, 'nickname': s.nickname, 'profile_root': s.profile_root, 'studentLists': [l for l in [student_lists_dict.get(str(list_id)) for list_id in s.student_lists] if l], }, students_data) students.sort(key=lambda s: s['nickname']) template_values = { 'students': students, 'students_json': json.dumps(students), 'student_lists': student_lists_list, 'student_lists_json': json.dumps(student_lists_list), 'invalid_student': invalid_student, 'coach_requests': coach_requests, 'coach_requests_json': json.dumps(coach_requests), 'selected_nav_link': 'coach', 'email': user_data.email, } self.render_jinja2_template('viewstudentlists.html', template_values)
def get(self): """ Only used when a coach deletes a request in studentlists.js. """ user_data = UserData.current() accept_coach = self.request_bool("accept", default = False) user_data_student = self.request_student_user_data() if user_data_student: user_data_coach = user_data if (user_data_coach and not user_data_student.is_coached_by(user_data_coach)): coach_request = CoachRequest.get_for(user_data_coach, user_data_student) if coach_request: coach_request.delete() if user_data.key_email == user_data_student.key_email and accept_coach: user_data_student.coaches.append(user_data_coach.key_email) user_data_student.put() if not self.is_ajax_request(): self.redirect("/coaches")
def get(self): user_data = UserData.current() user_data_override = self.request_user_data("coach_email") if user_util.is_current_user_developer() and user_data_override: user_data = user_data_override invalid_student = self.request_bool("invalid_student", default=False) coach_requests = [ req.student_requested_identifier for req in CoachRequest.get_for_coach(user_data) if req.student_requested_data ] student_lists_models = StudentList.get_for_coach(user_data.key()) student_lists_list = [] for student_list in student_lists_models: student_lists_list.append({ 'key': str(student_list.key()), 'name': student_list.name, }) student_lists_dict = dict((g['key'], g) for g in student_lists_list) def student_to_dict(s): """Convert the UserData s to a dictionary for rendering.""" return { 'key': str(s.key()), 'email': s.email, 'username': s.username, # Note that child users don't have an email. 'identifier': s.username or s.email, 'nickname': s.nickname, 'profile_root': s.profile_root, 'can_modify_coaches': s.can_modify_coaches(), 'studentLists': [ l for l in [ student_lists_dict.get(str(list_id)) for list_id in s.student_lists ] if l ], } students_data = user_data.get_students_data() students = map(student_to_dict, students_data) students.sort(key=lambda s: s['nickname']) child_accounts = map(student_to_dict, user_data.get_child_users()) template_values = { 'students': students, 'child_accounts': child_accounts, 'child_account_keyset': set([c['key'] for c in child_accounts]), 'students_json': json.dumps(students), 'student_lists': student_lists_list, 'student_lists_json': json.dumps(student_lists_list), 'invalid_student': invalid_student, 'coach_requests': coach_requests, 'coach_requests_json': json.dumps(coach_requests), 'selected_nav_link': 'coach', 'email': user_data.email, } self.render_jinja2_template('viewstudentlists.html', template_values)