def post(self): student = self.personalize_page_and_get_enrolled() if not student: return if not self.assert_xsrf_token_or_fail(self.request, 'student-edit'): return all_track_label_ids = models.LabelDAO.get_set_of_ids_of_type( models.LabelDTO.LABEL_TYPE_COURSE_TRACK) new_track_label_ids = set( [int(label_id) for label_id in self.request.get_all('labels') if label_id and int(label_id) in all_track_label_ids]) student_label_ids = set( [int(label_id) for label_id in common_utils.text_to_list(student.labels) if label_id]) # Remove all existing track (and only track) labels from student, # then merge in selected set from form. student_label_ids = student_label_ids.difference(all_track_label_ids) student_label_ids = student_label_ids.union(new_track_label_ids) models.Student.set_labels_for_current( common_utils.list_to_text(list(student_label_ids))) self.redirect('/student/home')
def _postprocess_rows(cls, app_context, source_context, schema, log, page_number, rows): ret = super(StudentsDataSource, cls)._postprocess_rows( app_context, source_context, schema, log, page_number, rows) # These don't add any value, and do add substantially to data volume. # (The user_id field is what's valuable for matching to other items # such as StudentAnswersEntity records.) for item in ret: if 'key' in item: del item['key'] if 'key_by_user_id' in item: del item['key_by_user_id'] if 'safe_key' in item: del item['safe_key'] if 'scores' in item: del item['scores'] item['labels'] = ( [x for x in utils.text_to_list(item['labels'])]) if 'scores' in ret: del item['scores'] if item.get('additional_fields'): additional_fields = transforms.loads(item['additional_fields']) item['additional_fields'] = [ {'name': l[0], 'value': l[1]} for l in additional_fields] # Here, run through the Student entities to pick up the email address. # Since the email is not stored as an actual property in the entity, but # instead is just part of the key, we have to manually extract it. Note # that here we are making the entirely reasonable assumption that the # cardinality of the list of Student entity and the dict-of-properties # list in 'ret' is the same. if source_context.send_uncensored_pii_data: for student, output_dict in zip(rows, ret): output_dict['email'] = student.email return ret
def unit_labels_to_dict(cls, course, unit): parent_unit = course.get_parent_unit(unit.unit_id) labels = utils.text_to_list(unit.labels) def should_skip(label_type): return ( parent_unit and label_type.type == models.LabelDTO.LABEL_TYPE_COURSE_TRACK) return cls._all_labels_to_dict(labels, should_skip=should_skip)
def _user_email_in(cls, user, text): if not user: return False email = user.email().lower() if not email: return False match_to = set([email]) if '@' in email: domain = email.split('@')[-1] if domain: match_to.add(domain) match_to.add('@' + domain) allowed = set([email.lower() for email in utils.text_to_list( text, utils.BACKWARD_COMPATIBLE_SPLITTER)]) return bool(match_to & allowed)
def transform_after_editor_hook(self, role_dict): """Edit the dict generated by the role editor.""" role_dict['name'] = role_dict['name'].strip() role_dict['users'] = utils.text_to_list( role_dict['users'], utils.SPLITTER) # Create new entry for the dict to store formatted information role_dict['permissions'] = {} for (module_name, permissions) in role_dict['modules'].iteritems(): assigned_permissions = [] for permission in permissions: if permission['assigned']: assigned_permissions.append(permission['name']) if assigned_permissions: role_dict['permissions'][module_name] = assigned_permissions # Remove obsolete entry del role_dict['modules'] return role_dict
def get(self): """Handles REST GET verb and returns an object as JSON payload.""" key = self.request.get('key') try: entity = AnnouncementEntity.get(key) except db.BadKeyError: entity = None if not entity: transforms.send_json_response( self, 404, 'Object not found.', {'key': key}) return viewable = AnnouncementsRights.apply_rights(self, [entity]) if not viewable: transforms.send_json_response( self, 401, 'Access denied.', {'key': key}) return entity = viewable[0] schema = AnnouncementsItemRESTHandler.SCHEMA() entity_dict = transforms.entity_to_dict(entity) # Format the internal date object as ISO 8601 datetime, with time # defaulting to 00:00:00 date = entity_dict['date'] date = datetime.datetime(date.year, date.month, date.day) entity_dict['date'] = date entity_dict.update( resources_display.LabelGroupsHelper.labels_to_field_data( common_utils.text_to_list(entity.labels))) json_payload = transforms.dict_to_json(entity_dict) transforms.send_json_response( self, 200, 'Success.', payload_dict=json_payload, xsrf_token=utils.XsrfTokenManager.create_xsrf_token( 'announcement-put'))
def _maybe_add_google_produced_course_info(cls, course, message): # If you would like your course to additionally report title, # short-name, and namespace, you may remove lines below, up # through and including the 'if' statement; simply leave the # lines from "app_context = course.app_context" and below. # This will also require fixing up the unit test named # "test_course_message_with_google_admin" in # tests/functional/modules_usage_reporting.py to always expect # these extra data. all_settings = course.get_environ(course.app_context) course_settings = all_settings['course'] admin_email_str = course_settings.get(roles.KEY_ADMIN_USER_EMAILS) addrs = common_utils.text_to_list( admin_email_str, common_utils.BACKWARD_COMPATIBLE_SPLITTER) if addrs and all([addr.endswith('@google.com') for addr in addrs]): app_context = course.app_context message[cls._COURSE_TITLE] = app_context.get_title() message[cls._COURSE_SLUG] = app_context.get_slug() message[cls._COURSE_NAMESPACE] = app_context.get_namespace_name()
def test_none_split(self): self.assertListEqual([], utils.text_to_list(None))
def announcement_labels_to_dict(cls, announcement): return cls._all_labels_to_dict(utils.text_to_list(announcement.labels))
def test_empty_split(self): self.assertListEqual([], utils.text_to_list(''))
def get(self): """Handles REST GET verb and returns an object as JSON payload.""" key = self.request.get('key') try: entity = CourseSectionEntity.get(key) except db.BadKeyError: entity = None if not entity: transforms.send_json_response( self, 404, 'MobileCSP: Course Section not found.', {'key': key}) return viewable = TeacherRights.apply_rights(self, [entity]) if not viewable: transforms.send_json_response(self, 401, 'MobileCSP: Access denied.', {'key': key}) return entity = viewable[0] schema = SectionItemRESTHandler.SCHEMA() entity_dict = transforms.entity_to_dict(entity) if GLOBAL_DEBUG: logging.warning('***RAM*** get entity = ' + str(entity_dict)) # Distinguish between adding a new entity and editing an existing entity # If this is a new Entity, its acadyr field will be blank. if entity_dict['acadyr'] != '': entity_dict['mode'] = 'Edit' else: entity_dict['mode'] = 'Add' # Format the internal date object as ISO 8601 datetime, with time # defaulting to 00:00:00 date = entity_dict['date'] date = datetime.datetime(date.year, date.month, date.day) entity_dict['date'] = date emails = entity_dict['students'] # Student emails are comma-delimited emails = emails.replace(',', '\n') # Replace with new lines for display entity_dict['students'] = emails if entity_dict['mode'] == 'Edit': entity_dict[ 'directions'] = "Edit a section: To add or delete students, add (or remove) their emails to (or from) the Class Roster." else: entity_dict['name'] = "New section" entity_dict['directions'] = "New section: Give the section a name and (optionally) a short description and pick an academic year. " + \ "To create a roster of students, you must use the exact email that the student is registered under. Put one email per line or " + \ "separate emails by commas." entity_dict.update( resources_display.LabelGroupsHelper.labels_to_field_data( common_utils.text_to_list(entity.labels))) json_payload = transforms.dict_to_json(entity_dict) transforms.send_json_response( self, 200, 'Success.', payload_dict=json_payload, xsrf_token=utils.XsrfTokenManager.create_xsrf_token('section-put'))
def _get_existing_label_ids(self, student): # Prune label IDs that no longer refer to a valid Label object. all_label_ids = {label.id for label in models.LabelDAO.get_all()} existing_labels = set([int(label_id) for label_id in common_utils.text_to_list(student.labels)]) return existing_labels.intersection(all_label_ids)
def test_all_separators_split(self): self.assertListEqual([], utils.text_to_list(' ,,, \t\t\n\t '))
def test_split_join(self): text = 'a b c' self.assertEquals(text, utils.list_to_text(utils.text_to_list(text)))
def test_one_item_split(self): self.assertListEqual(['x'], utils.text_to_list('x'))
def _user_email_in(cls, user, text): email_list = [email.lower() for email in utils.text_to_list( text, utils.BACKWARD_COMPATIBLE_SPLITTER)] return bool(user and user.email().lower() in email_list)
def test_list_parsing(self): # pylint: disable-msg=protected-access self.assertListEqual(['foo'], utils.text_to_list('foo')) self.assertListEqual(['foo'], utils.text_to_list(' foo')) self.assertListEqual(['foo'], utils.text_to_list('foo ')) self.assertListEqual(['foo'], utils.text_to_list(' foo ')) self.assertListEqual(['foo'], utils.text_to_list('foo\t')) self.assertListEqual(['foo'], utils.text_to_list('\tfoo')) self.assertListEqual(['foo'], utils.text_to_list('\tfoo\t')) self.assertListEqual(['foo'], utils.text_to_list('foo ')) self.assertListEqual(['foo'], utils.text_to_list(' foo')) self.assertListEqual(['foo'], utils.text_to_list(' foo ')) self.assertListEqual(['foo'], utils.text_to_list('foo\n')) self.assertListEqual(['foo'], utils.text_to_list('\nfoo')) self.assertListEqual(['foo'], utils.text_to_list('\nfoo\n')) self.assertListEqual(['foo'], utils.text_to_list('foo,')) self.assertListEqual(['foo'], utils.text_to_list(',foo')) self.assertListEqual(['foo'], utils.text_to_list(',foo,')) self.assertListEqual(['foo'], utils.text_to_list(' foo ,\n')) self.assertListEqual(['foo'], utils.text_to_list('\tfoo,\t\n')) self.assertListEqual(['foo'], utils.text_to_list(',foo,\n')) self.assertListEqual(['foo'], utils.text_to_list( '[foo]', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo],', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo], ', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo],\n', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo], \n', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo bar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list(' foo bar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo bar ')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo\tbar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('\tfoo\tbar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo\tbar\t')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo\nbar\n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('\nfoo\nbar\n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('\n foo\n bar\n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list(' \n foo \n bar \n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list( '[foo][bar]', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list( ' [foo] [bar] ', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list( '\n[foo]\n[bar]\n', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list( '\n,[foo],\n[bar],\n', utils.BACKWARD_COMPATIBLE_SPLITTER))
def test_join_split(self): l = ['a', 'b', 'c'] self.assertListEqual(l, utils.text_to_list(utils.list_to_text(l)))
def _user_email_in(cls, user, text): return user and user.email() in utils.text_to_list( text, utils.BACKWARD_COMPATIBLE_SPLITTER)
def map(student): for label_id_str in utils.text_to_list(student.labels): yield (label_id_str, 1)
def test_list_parsing(self): self.assertListEqual(['foo'], utils.text_to_list('foo')) self.assertListEqual(['foo'], utils.text_to_list(' foo')) self.assertListEqual(['foo'], utils.text_to_list('foo ')) self.assertListEqual(['foo'], utils.text_to_list(' foo ')) self.assertListEqual(['foo'], utils.text_to_list('foo\t')) self.assertListEqual(['foo'], utils.text_to_list('\tfoo')) self.assertListEqual(['foo'], utils.text_to_list('\tfoo\t')) self.assertListEqual(['foo'], utils.text_to_list('foo ')) self.assertListEqual(['foo'], utils.text_to_list(' foo')) self.assertListEqual(['foo'], utils.text_to_list(' foo ')) self.assertListEqual(['foo'], utils.text_to_list('foo\n')) self.assertListEqual(['foo'], utils.text_to_list('\nfoo')) self.assertListEqual(['foo'], utils.text_to_list('\nfoo\n')) self.assertListEqual(['foo'], utils.text_to_list('foo,')) self.assertListEqual(['foo'], utils.text_to_list(',foo')) self.assertListEqual(['foo'], utils.text_to_list(',foo,')) self.assertListEqual(['foo'], utils.text_to_list(' foo ,\n')) self.assertListEqual(['foo'], utils.text_to_list('\tfoo,\t\n')) self.assertListEqual(['foo'], utils.text_to_list(',foo,\n')) self.assertListEqual(['foo'], utils.text_to_list( '[foo]', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo],', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo], ', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo],\n', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo'], utils.text_to_list( '[foo], \n', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo bar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list(' foo bar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo bar ')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo\tbar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('\tfoo\tbar')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo\tbar\t')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('foo\nbar\n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('\nfoo\nbar\n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list('\n foo\n bar\n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list(' \n foo \n bar \n')) self.assertListEqual(['foo', 'bar'], utils.text_to_list( '[foo][bar]', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list( ' [foo] [bar] ', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list( '\n[foo]\n[bar]\n', utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(['foo', 'bar'], utils.text_to_list( '\n,[foo],\n[bar],\n', utils.BACKWARD_COMPATIBLE_SPLITTER))
def test_join_split(self): l = ["a", "b", "c"] self.assertListEqual(l, utils.text_to_list(utils.list_to_text(l)))
def _get_request_label_ids(self): return set([int(l) for l in common_utils.text_to_list( self.request.get(PARAMETER_LABELS))])
def test_list_parsing(self): # pylint: disable-msg=protected-access self.assertListEqual(["foo"], utils.text_to_list("foo")) self.assertListEqual(["foo"], utils.text_to_list(" foo")) self.assertListEqual(["foo"], utils.text_to_list("foo ")) self.assertListEqual(["foo"], utils.text_to_list(" foo ")) self.assertListEqual(["foo"], utils.text_to_list("foo\t")) self.assertListEqual(["foo"], utils.text_to_list("\tfoo")) self.assertListEqual(["foo"], utils.text_to_list("\tfoo\t")) self.assertListEqual(["foo"], utils.text_to_list("foo ")) self.assertListEqual(["foo"], utils.text_to_list(" foo")) self.assertListEqual(["foo"], utils.text_to_list(" foo ")) self.assertListEqual(["foo"], utils.text_to_list("foo\n")) self.assertListEqual(["foo"], utils.text_to_list("\nfoo")) self.assertListEqual(["foo"], utils.text_to_list("\nfoo\n")) self.assertListEqual(["foo"], utils.text_to_list("foo,")) self.assertListEqual(["foo"], utils.text_to_list(",foo")) self.assertListEqual(["foo"], utils.text_to_list(",foo,")) self.assertListEqual(["foo"], utils.text_to_list(" foo ,\n")) self.assertListEqual(["foo"], utils.text_to_list("\tfoo,\t\n")) self.assertListEqual(["foo"], utils.text_to_list(",foo,\n")) self.assertListEqual(["foo"], utils.text_to_list("[foo]", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(["foo"], utils.text_to_list("[foo],", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(["foo"], utils.text_to_list("[foo], ", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(["foo"], utils.text_to_list("[foo],\n", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(["foo"], utils.text_to_list("[foo], \n", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(["foo", "bar"], utils.text_to_list("foo bar")) self.assertListEqual(["foo", "bar"], utils.text_to_list(" foo bar")) self.assertListEqual(["foo", "bar"], utils.text_to_list("foo bar ")) self.assertListEqual(["foo", "bar"], utils.text_to_list("foo\tbar")) self.assertListEqual(["foo", "bar"], utils.text_to_list("\tfoo\tbar")) self.assertListEqual(["foo", "bar"], utils.text_to_list("foo\tbar\t")) self.assertListEqual(["foo", "bar"], utils.text_to_list("foo\nbar\n")) self.assertListEqual(["foo", "bar"], utils.text_to_list("\nfoo\nbar\n")) self.assertListEqual(["foo", "bar"], utils.text_to_list("\n foo\n bar\n")) self.assertListEqual(["foo", "bar"], utils.text_to_list(" \n foo \n bar \n")) self.assertListEqual(["foo", "bar"], utils.text_to_list("[foo][bar]", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(["foo", "bar"], utils.text_to_list(" [foo] [bar] ", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual(["foo", "bar"], utils.text_to_list("\n[foo]\n[bar]\n", utils.BACKWARD_COMPATIBLE_SPLITTER)) self.assertListEqual( ["foo", "bar"], utils.text_to_list("\n,[foo],\n[bar],\n", utils.BACKWARD_COMPATIBLE_SPLITTER) )
def test_one_item_split(self): self.assertListEqual(["x"], utils.text_to_list("x"))
def _user_email_in(cls, user, text): email_list = [ email.lower() for email in utils.text_to_list( text, utils.BACKWARD_COMPATIBLE_SPLITTER) ] return bool(user and user.email().lower() in email_list)
def _get_request_label_ids(self): return set([ int(l) for l in common_utils.text_to_list( self.request.get(PARAMETER_LABELS)) ])
def get(self): """Handles REST GET verb and returns an object as JSON payload.""" key = self.request.get('key') try: entity = CourseSectionEntity.get(key) except db.BadKeyError: entity = None if not entity: transforms.send_json_response( self, 404, 'MobileCSP: Course Section not found.', {'key': key}) return viewable = TeacherRights.apply_rights(self, [entity]) if not viewable: transforms.send_json_response( self, 401, 'MobileCSP: Access denied.', {'key': key}) return entity = viewable[0] schema = SectionItemRESTHandler.SCHEMA() entity_dict = transforms.entity_to_dict(entity) if GLOBAL_DEBUG: logging.warning('***RAM*** get entity = ' + str(entity_dict)) # Distinguish between adding a new entity and editing an existing entity # If this is a new Entity, its acadyr field will be blank. if entity_dict['acadyr'] != '': entity_dict['mode'] = 'Edit' else: entity_dict['mode'] = 'Add' # Format the internal date object as ISO 8601 datetime, with time # defaulting to 00:00:00 date = entity_dict['date'] date = datetime.datetime(date.year, date.month, date.day) entity_dict['date'] = date emails = entity_dict['students'] # Student emails are comma-delimited emails = emails.replace(',', '\n') # Replace with new lines for display entity_dict['students'] = emails if entity_dict['mode'] == 'Edit': entity_dict['directions'] = "Edit a section: To add or delete students, add (or remove) their emails to (or from) the Class Roster." else: entity_dict['name'] = "New section" entity_dict['directions'] = "New section: Give the section a name and (optionally) a short description and pick an academic year. " + \ "To create a roster of students, you must use the exact email that the student is registered under. Put one email per line or " + \ "separate emails by commas." entity_dict.update( resources_display.LabelGroupsHelper.labels_to_field_data( common_utils.text_to_list(entity.labels))) json_payload = transforms.dict_to_json(entity_dict) transforms.send_json_response( self, 200, 'Success.', payload_dict=json_payload, xsrf_token=utils.XsrfTokenManager.create_xsrf_token( 'section-put'))