def create_class(self, new_class: NewClass) -> None: """ Take a Class object and create/write the class in the database. Creates class data in persistence. Calls setup_class to create any needed files, then writes data to file. :param new_class: Class object :return: None """ new_class.id = new_class.name self._setup_class(new_class.id) self._write_classlist_to_file(new_class) self._move_avatars_to_class_data(new_class)
def create_class(self, new_class: NewClass) -> None: """ Create class data in database. Moves avatars for each student to db, changes student.avatar_id to id of avatar image in db. If no avatar, this remains None/null, and that is stored as student's avatar_id in db. :param new_class: :return: None """ with self._connection() as conn: cursor = conn.cursor() # insert class into class cursor.execute( """ INSERT INTO class(name) VALUES(?); """, (new_class.name, )) # Add id to class: new_class.id = cursor.lastrowid for student in new_class: # insert student if student.avatar_id: # Move avatar from temp to db: avatar_blob = new_class.temp_avatars_dir.joinpath( student.avatar_id).read_bytes() cursor.execute( """ INSERT INTO avatar(image) VALUES(?); """, (avatar_blob, )) # Change avatar_id to id of avatar in db. student.avatar_id = cursor.lastrowid cursor.execute( """ INSERT INTO student(name, class_id, avatar_id) VALUES(?,?,?); """, (student.name, new_class.id, student.avatar_id)) # Add id to student: student.id = cursor.lastrowid conn.commit() conn.close()
def test_move_avatar_to_class_data(self, monkeypatch, empty_json_database): """Avatar moved from original location to class data.""" test_class = NewClass('test_class') test_class.id = test_class.name # Set NewClass id to save in db. test_filename = 'test avatar filename' test_json_database = empty_json_database def mock_move_file(origin_path: Path, destination_path: Path): if origin_path != test_class.temp_dir.joinpath( 'avatars', test_filename): raise ValueError("Origin path incorrect.") if destination_path != test_json_database.class_data_path.joinpath( test_class.name, 'avatars', test_filename): raise ValueError("Destination path incorrect") monkeypatch.setattr(json, 'move_file', mock_move_file) assert test_json_database._move_avatar_to_class_data( test_class, test_filename) is None
def test_move_avatar_to_class_data_avatar_preexisting( self, monkeypatch, empty_json_database): """No attempt to move avatar that already exists in class_data.""" test_class = NewClass('test_class') test_class.id = test_class.name # Set NewClass id to save in db. test_json_database = empty_json_database # Make existing avatar in tmpdir test_class class data: destination_avatar_path = test_json_database.class_data_path.joinpath( test_class.name, 'avatars', 'test_avatar_filename') Path.mkdir(destination_avatar_path.parent, parents=True) with open(destination_avatar_path, 'w'): pass def mock_move_file(origin_path: Path, destination_path: Path): raise ValueError("Move file should not be called.") monkeypatch.setattr(json, 'move_file', mock_move_file) assert test_json_database._move_avatar_to_class_data( test_class, destination_avatar_path.name) is None
def create_class(self, new_class: NewClass) -> None: """ Create class data in database. Moves avatars for each student to db, changes student.avatar_id to id of avatar image in db. If no avatar, this remains None/null, and that is stored as student's avatar_id in db. :param new_class: :return: None """ with self.session_scope() as session: added_class = self.Class(name=new_class.name) session.add(added_class) session.flush() # Commit to get a class id new_class.id = added_class.id # Add students: for student in new_class: if student.avatar_id: # Move avatar from temp to db: avatar_blob = new_class.temp_avatars_dir.joinpath( student.avatar_id).read_bytes() added_avatar = self.Avatar(image=avatar_blob) session.add(added_avatar) session.flush( ) # Must flush after each avatar to get the avatar id. student.avatar_id = added_avatar.id added_student = self.Student( name=student.name, class_id=new_class.id, avatar_id=student.avatar_id, ) session.add(added_student) session.flush( ) # Must flush after each student to get the avatar id. # Add id to student: student.id = added_student.id