parent
01f7521d98
commit
3bb880a481
4 changed files with 140 additions and 138 deletions
@ -1,136 +0,0 @@ |
||||
from courses.models import CourseTheme, Lesson, CourseMap, Homework, Exam |
||||
|
||||
|
||||
class CourseBuilder: |
||||
def __init__(self, course, material=None): |
||||
self.course = course |
||||
self.course_public = self.course.public |
||||
self.material = material # Вернуть токен вызвавшего материала |
||||
self.ready = [] |
||||
self.materials = [] |
||||
self.token = '' |
||||
|
||||
def append_in_price(self, map): |
||||
# Добавить карту в счета, куда включены все карты курса в типе, исключая текущий |
||||
#Price.objects.filter(included=map.get_befor) |
||||
pass |
||||
|
||||
def rebuild_map(self): |
||||
sort = 0 |
||||
for theme in CourseTheme.objects.filter(course=self.course): |
||||
for lesson in Lesson.objects.filter(theme=theme): |
||||
must_save = False |
||||
try: |
||||
token = '{0}#{1}'.format(self.course.id, sort) |
||||
tmp = CourseMap.objects.get(token=token) |
||||
except CourseMap.DoesNotExist: |
||||
tmp = CourseMap.objects.create(lesson=lesson, sort=sort, course=self.course) |
||||
self.append_in_price(tmp) |
||||
|
||||
if tmp.homework: |
||||
tmp.homework = None |
||||
must_save = True |
||||
|
||||
if tmp.exam: |
||||
tmp.exam = None |
||||
must_save = True |
||||
|
||||
if tmp.lesson != lesson: |
||||
tmp.lesson = lesson |
||||
must_save = True |
||||
|
||||
if lesson.token != token: |
||||
lesson.token = token |
||||
lesson.save() |
||||
|
||||
if must_save: tmp.save() |
||||
|
||||
if lesson == self.material: |
||||
self.token = tmp.token |
||||
self.materials.append(tmp.id) |
||||
sort += 1 |
||||
|
||||
for homework in Homework.objects.filter(theme=theme): |
||||
must_save = False |
||||
try: |
||||
token = '{0}#{1}'.format(self.course.id, sort) |
||||
tmp = CourseMap.objects.get(token=token) |
||||
except CourseMap.DoesNotExist: |
||||
tmp = CourseMap.objects.create(homework=homework, sort=sort, course=self.course) |
||||
self.append_in_price(tmp) |
||||
|
||||
if tmp.homework != homework: |
||||
tmp.homework = homework |
||||
must_save = True |
||||
|
||||
if tmp.exam: |
||||
tmp.exam = None |
||||
must_save = True |
||||
|
||||
if tmp.lesson: |
||||
tmp.lesson = None |
||||
must_save = True |
||||
|
||||
if homework.token != token: |
||||
homework.token = token |
||||
homework.save() |
||||
|
||||
if must_save: tmp.save() |
||||
|
||||
if homework == self.material: |
||||
self.token = tmp.token |
||||
|
||||
self.materials.append(tmp.id) |
||||
sort += 1 |
||||
|
||||
for exam in Exam.objects.filter(theme=theme): |
||||
must_save = False |
||||
try: |
||||
token = '{0}#{1}'.format(self.course.id, sort) |
||||
tmp = CourseMap.objects.get(token=token) |
||||
except CourseMap.DoesNotExist: |
||||
tmp = CourseMap.objects.create(exam=exam, sort=sort, course=self.course) |
||||
self.append_in_price(tmp) |
||||
|
||||
if tmp.exam != exam: |
||||
tmp.exam = exam |
||||
must_save = True |
||||
|
||||
if tmp.homework: |
||||
tmp.homework = None |
||||
must_save = True |
||||
|
||||
if tmp.lesson: |
||||
tmp.lesson = None |
||||
must_save = True |
||||
|
||||
if exam.token != token: |
||||
exam.token = token |
||||
exam.save() |
||||
|
||||
if must_save: tmp.save() |
||||
|
||||
if exam == self.material: |
||||
self.token = tmp.token |
||||
self.materials.append(tmp.id) |
||||
sort += 1 |
||||
|
||||
if sort != CourseMap.objects.filter(course=self.course).count(): |
||||
CourseMap.objects.filter(course=self.course).exclude(id__in=self.materials).delete() |
||||
|
||||
def course_switcher(self): |
||||
# Блокировка курса на время обработки |
||||
if self.course_public: |
||||
if self.course.public: |
||||
self.course.public = False |
||||
else: |
||||
self.course.public = True |
||||
self.course.save() |
||||
|
||||
def main(self): |
||||
self.course_switcher() |
||||
## |
||||
self.rebuild_map() |
||||
## |
||||
self.course_switcher() |
||||
return self.token if self.material else None # Вернуть токен для материала если он есть |
||||
Loading…
Reference in new issue