parent
fdeed92ab6
commit
7b56bf0d50
6 changed files with 142 additions and 7 deletions
@ -0,0 +1,114 @@ |
|||||||
|
from django.contrib.auth import get_user_model |
||||||
|
from django.core.management.base import BaseCommand, CommandError |
||||||
|
from progress.models import Progress, ProgressLesson |
||||||
|
from courses.models import Course |
||||||
|
from django.db.models import Q |
||||||
|
import datetime |
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand): |
||||||
|
help = 'Добавляет 1 или нескольких юзеров в указанные группы' |
||||||
|
|
||||||
|
def add_arguments(self, parser): |
||||||
|
parser.add_argument( |
||||||
|
'--u', |
||||||
|
type=str, |
||||||
|
dest='user_email', |
||||||
|
help='Емейл пользователя' |
||||||
|
) |
||||||
|
parser.add_argument( |
||||||
|
'--c', |
||||||
|
type=str, |
||||||
|
dest='course_slug', |
||||||
|
help='Слаг курса' |
||||||
|
) |
||||||
|
parser.add_argument( |
||||||
|
'--t', |
||||||
|
type=int, |
||||||
|
dest='topic_idx', |
||||||
|
help='Порядковый номер темы' |
||||||
|
) |
||||||
|
parser.add_argument( |
||||||
|
'--l', |
||||||
|
type=int, |
||||||
|
dest='lesson_idx', |
||||||
|
help='Порядковый номер урока' |
||||||
|
) |
||||||
|
parser.add_argument( |
||||||
|
'--f', |
||||||
|
type=bool, |
||||||
|
dest='force_start', |
||||||
|
help='Игнорировать предупреждения' |
||||||
|
) |
||||||
|
|
||||||
|
def handle(self, *args, **options): |
||||||
|
try: |
||||||
|
student = get_user_model().objects.get(email=options['user_email']) |
||||||
|
|
||||||
|
try: |
||||||
|
course = Course.objects.get(slug=options['course_slug']) |
||||||
|
try: |
||||||
|
progress = Progress.objects.get(course_token=course.token, user=student) |
||||||
|
except Progress.DoesNotExist: |
||||||
|
if not options['force_start']: |
||||||
|
raise CommandError( |
||||||
|
"Пользователь %s, не проходит курс %s" % (student.get_full_name(), course.title)) |
||||||
|
|
||||||
|
teacher = get_user_model().objects.get(out_key=course.get_teacher()) |
||||||
|
progress = Progress.objects.create(course_token=course.token, user=student, teacher=teacher) |
||||||
|
|
||||||
|
token_list = [] |
||||||
|
lesson_list = [] |
||||||
|
for topic_idx, topic in enumerate(course.topic_set.all()): |
||||||
|
topic_find = topic_idx == (options['topic_idx'] - 1) |
||||||
|
for lesson_idx, lesson in enumerate(topic.lesson_set.all()): |
||||||
|
token_list.append(lesson.token) |
||||||
|
lesson_list.append(lesson) |
||||||
|
if lesson_idx == (options['lesson_idx'] - 1) and topic_find: |
||||||
|
break |
||||||
|
|
||||||
|
if topic_find: |
||||||
|
break |
||||||
|
|
||||||
|
if progress.progresslesson_set.filter( |
||||||
|
~Q(lesson_token__in=token_list)).exists() and not options['force_start']: |
||||||
|
raise CommandError( |
||||||
|
"Пользователь %s, прошёл дальше по курсу %s" % (student.get_full_name(), course.title)) |
||||||
|
|
||||||
|
progress.progresslesson_set.filter(~Q(lesson_token__in=token_list)).delete() |
||||||
|
|
||||||
|
for lesson_idx, lesson_token in enumerate(token_list[:-1]): |
||||||
|
try: |
||||||
|
pl = ProgressLesson.objects.get(progress__user=student, lesson_token=lesson_token) |
||||||
|
if pl.finish_date is None: |
||||||
|
pl.finish_date = datetime.datetime.now() |
||||||
|
pl.status = 'done' |
||||||
|
pl.save() |
||||||
|
except ProgressLesson.DoesNotExist: |
||||||
|
ProgressLesson.objects.create( |
||||||
|
progress=progress, |
||||||
|
lesson_token=lesson_token, |
||||||
|
checker=progress.teacher if lesson_list[lesson_idx].is_hm else student, |
||||||
|
status="done", |
||||||
|
finish_date=datetime.datetime.now() |
||||||
|
) |
||||||
|
|
||||||
|
try: |
||||||
|
pl = ProgressLesson.objects.get(progress=progress, lesson_token=token_list[-1:][0]) |
||||||
|
if pl.status == "done": |
||||||
|
pl.status = "start" |
||||||
|
|
||||||
|
pl.finish_date = None |
||||||
|
pl.save() |
||||||
|
except ProgressLesson.DoesNotExist: |
||||||
|
pl = ProgressLesson.objects.create( |
||||||
|
progress=progress, |
||||||
|
lesson_token=token_list[-1:][0], |
||||||
|
checker=progress.teacher if lesson_list[-1:][0].is_hm else student |
||||||
|
) |
||||||
|
|
||||||
|
except Course.DoesNotExist: |
||||||
|
print("""Course with slug "%s" doesn't exist""" % options['course_slug']) |
||||||
|
|
||||||
|
except get_user_model().DoesNotExist: |
||||||
|
print("""user with email: "%s" not found""" % options['user_email']) |
||||||
Loading…
Reference in new issue