Coverage for docs_src / advanced_tutorial / model_inheritance / tutorial000.py: 100%

43 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-07 08:37 +0000

1import os 1abcdef

2 

3from mongodb_odm import ( 1abcdef

4 ASCENDING, 

5 Document, 

6 Field, 

7 IndexModel, 

8 ODMObjectId, 

9 apply_indexes, 

10 connect, 

11) 

12 

13 

14class Course(Document): 1abcdef

15 title: str 1abcdef

16 

17 

18class Content(Document): 1abcdef

19 course_id: ODMObjectId 1abcdef

20 title: str = Field(max_length=255) 1abcdef

21 

22 class ODMConfig(Document.ODMConfig): 1abcdef

23 allow_inheritance = True 1abcdef

24 indexes = [ 1abcdef

25 IndexModel([("course_id", ASCENDING)]), 

26 ] 

27 

28 

29class Text(Content): 1abcdef

30 text: str 1abcdef

31 

32 class ODMConfig(Document.ODMConfig): 1abcdef

33 allow_inheritance = False 1abcdef

34 

35 

36class Video(Content): 1abcdef

37 video_path: str = Field(max_length=512) 1abcdef

38 

39 class ODMConfig(Document.ODMConfig): 1abcdef

40 allow_inheritance = False 1abcdef

41 

42 

43def configuration(): 1abcdef

44 connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb")) 1abcdef

45 apply_indexes() 1abcdef

46 

47 

48def create_data(): 1abcdef

49 course = Course(title="MongoDB-ODM Tutorial").create() 1abcdef

50 

51 Text(course_id=course.id, title="Introduction", text="Introduction Text").create() 1abcdef

52 Video( 1abcdef

53 course_id=course.id, 

54 title="Environment Setup", 

55 video_path="/media/video_path.mp4", 

56 ).create() 

57 

58 

59def retrieve_content(): 1abcdef

60 for content in Content.find(): 1abcdef

61 print(content) 1abcdef

62 print() 1abcdef

63 

64 

65def retrieve_text(): 1abcdef

66 for text in Text.find(): 1abcdef

67 print(text) 1abcdef

68 print() 1abcdef

69 

70 

71def retrieve_video(): 1abcdef

72 for video in Video.find(): 1abcdef

73 print(video) 1abcdef

74 print() 1abcdef

75 

76 

77def main(): 1abcdef

78 configuration() 1abcdef

79 

80 create_data() 1abcdef

81 

82 retrieve_content() 1abcdef

83 retrieve_text() 1abcdef

84 retrieve_video() 1abcdef

85 

86 

87if __name__ == "__main__": 1abcdef

88 main()