-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
158 lines (113 loc) · 3.54 KB
/
models.py
File metadata and controls
158 lines (113 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from reusepatterns.prototypes import PrototypeMixin
from reusepatterns.observer import Subject, Observer
import jsonpickle
from my_orm import DomainObject
# абстрактный пользователь
class User:
def __init__(self, name):
self.name = name
# преподаватель
class Teacher(User):
pass
# студент
class Student(User, DomainObject):
def __init__(self, name):
self.courses = []
super().__init__(name)
# Фабрика пользователей
class UserFactory:
types = {
'student': Student,
'teacher': Teacher
}
@classmethod
def create(cls, type_, name):
return cls.types[type_](name)
# Категория
class Category:
# реестр?
auto_id = 0
def __init__(self, name, category):
self.id = Category.auto_id
Category.auto_id += 1
self.name = name
self.category = category
self.courses = []
def course_count(self):
result = len(self.courses)
if self.category:
result += self.category.course_count()
return result
# Курс
class Course(PrototypeMixin, Subject):
def __init__(self, name, category):
self.name = name
self.category = category
self.category.courses.append(self)
self.students = []
super().__init__()
def __getitem__(self, item):
return self.students[item]
def add_student(self, student: Student):
self.students.append(student)
student.courses.append(self)
self.notify()
class SmsNotifier(Observer):
def update(self, subject: Course):
print('SMS->', 'к нам присоединился', subject.students[-1].name)
class EmailNotifier(Observer):
def update(self, subject: Course):
print(('EMAIL->', 'к нам присоединился', subject.students[-1].name))
class BaseSerializer:
def __init__(self, obj):
self.obj = obj
def save(self):
return jsonpickle.dumps(self.obj)
def load(self, data):
return jsonpickle.loads(data)
# Интерактивный курс
class InteractiveCourse(Course):
pass
# Курс в записи
class RecordCourse(Course):
pass
# Фабрика курсов
class CourseFactory:
types = {
'interactive': InteractiveCourse,
'record': RecordCourse
}
@classmethod
def create(cls, type_, name, category):
return cls.types[type_](name, category)
# Основной класс - интерфейс проекта
class TrainingSite:
def __init__(self):
# Интерфейс
self.teachers = []
self.students = []
self.courses = []
self.categories = []
@staticmethod
def create_user(type_, name):
return UserFactory.create(type_, name)
@staticmethod
def create_category(name, category=None):
return Category(name, category)
def find_category_by_id(self, id):
for item in self.categories:
print('item', item.id)
if item.id == id:
return item
raise Exception(f'Нет категории с id = {id}')
@staticmethod
def create_course(type_, name, category):
return CourseFactory.create(type_, name, category)
def get_course(self, name) -> Course:
for item in self.courses:
if item.name == name:
return item
def get_student(self, name) -> Student:
for item in self.students:
if item.name == name:
return item