# coding=utf-8 from access.models import User from lms.decors import api_decor import simplejson as json # Процесс добавления журналов через API # Добавление проходит по from lms.settings import SECRET_KEY from courses.models import Course, Lesson, Homework, Exam from journals.models import TeacherJ, LessonJ, ExamJ, HomeworkJ def new_index(map, model, key, obj): if str(key) in map[model]: return obj.objects.get(id=map[model][str(key)]) return None @api_decor(without_auth=False, need_keys=['id', 'type'], method='POST', check_request=True) def check_materials_access(request, context): return context @api_decor(without_auth=True, need_keys=['student', 'lesson', 'status', 'start_date', 'f_date', 'lesson_old_id', 'student_old_id']) def open_lj(request, context): _fixture = json.load(open('../migrate/fixtures/pk_maps.json')) if request.POST['token'] == SECRET_KEY: l = new_index(_fixture, 'courses.lesson', request.POST['lesson'], Lesson) if request.POST[ 'lesson_old_id'] else Lesson.objects.get(id=request.POST['lesson']) s = new_index(_fixture, 'access.user', request.POST['student'], User) if request.POST[ 'student_old_id'] else User.objects.get(id=request.POST['student']) lj = LessonJ.objects.get_or_create(material=l, student=s) if request.POST['status'] == 'F': lj.saw_this() lj.f_date = request.POST['f_date'] lj.date = request.POST['start_date'] elif request.POST['status'] == 'A': lj.open_material() lj.date = request.POST['start_date'] elif request.POST['status'] == 'N': lj.date = None lj.f_date = None lj.success = False lj.save() return context @api_decor(without_auth=True, need_keys=['student', 'homework', 'status', 'start_date', 'f_date', 'token', 'comments', 'student_old_id', 'homework_old_id']) def set_ht(request, context): # Добавить попытку и все ее комментарии _fixture = json.load(open('../migrate/fixtures/pk_maps.json')) if request.POST['token'] == SECRET_KEY: t = new_index(_fixture, 'access.user', request.POST['teacher'], User) if request.POST[ 'teacher_old_id'] else User.objects.get(id=request.POST['teaceher']) s = new_index(_fixture, 'access.user', request.POST['student'], User) if request.POST[ 'student_old_id'] else User.objects.get(id=request.POST['student']) c = new_index(_fixture, 'courses.course', request.POST['course'], Course) if request.POST[ 'course_old_id'] else Course.objects.get(id=request.POST['course']) tj = TeacherJ.objects.get_or_create(student=s, course=c) tj.teacher = t tj.save() return context @api_decor(without_auth=True, need_keys=['token', 'teacher', 'teacher_old_id', 'student', 'student_old_id', 'course', 'course_old_id']) def set_teacher(request, context): _fixture = json.load(open('../migrate/fixtures/pk_maps.json')) if request.POST['token'] == SECRET_KEY: t = new_index(_fixture, 'access.user', request.POST['teacher'], User) if request.POST[ 'teacher_old_id'] else User.objects.get(id=request.POST['teaceher']) s = new_index(_fixture, 'access.user', request.POST['student'], User) if request.POST[ 'student_old_id'] else User.objects.get(id=request.POST['student']) c = new_index(_fixture, 'courses.course', request.POST['course'], Course) if request.POST[ 'course_old_id'] else Course.objects.get(id=request.POST['course']) tj = TeacherJ.objects.get_or_create(student=s, course=c) tj.teacher = t tj.save() return context @api_decor(without_auth=True, need_keys=['id', 'type'], method='GET', check_request=True) def check_jaccess(request, context): if request.user.is_authenticated(): if request.user.is_admin: context['code'] = '1' return context context['code'] = '0' _type = request.GET['type'] journal = None if _type == 'lesson': journal = LessonJ.objects.filter(material=Lesson.objects.get(id=request.GET['id']), student=request.user) if _type == 'homework': journal = HomeworkJ.objects.filter(material=Homework.objects.get(id=request.GET['id']), student=request.user) if _type == 'exam': journal = ExamJ.objects.filter(material=Exam.objects.get(id=request.GET['id']), student=request.user) if journal and journal.exists(): if request.user in journal[0].material.course.teachers.all(): context['code'] = '1' return context _before = journal.first().get_before() if journal.first().get_status_flag() != 'N': context['code'] = '1' elif journal.first().get_status_flag() == 'N': if _before['journal'].get_status_flag() == 'F': journal.first().open_material() return context @api_decor(without_auth=False, need_keys=['id'], method='GET', check_request=True) def check_exists_jhomework(request, context): try: HomeworkJ.objects.get(id=request.GET['id'], f_date=None) except HomeworkJ.DoesNotExist: context['code'] = '0' else: context['code'] = '1' return context @api_decor(without_auth=False, need_keys=['id'], method='GET', check_request=True) def check_exists_jexam(request, context): try: ExamJ.objects.get(id=request.GET['id'], f_date=None) except ExamJ.DoesNotExist: context['code'] = '0' else: context['code'] = '1' return context @api_decor(without_auth=True, need_keys=['id', 'type'], method='GET', check_request=True) def check_block(request, context): if request.user.is_authenticated(): context['code'] = '0' _type = request.GET['type'] journal = None if _type == 'lesson': journal = LessonJ.objects.filter(material=Lesson.objects.get(id=request.GET['id']), student=request.user) if _type == 'homework': journal = HomeworkJ.objects.filter(material=Homework.objects.get(id=request.GET['id']), student=request.user) if _type == 'exam': journal = ExamJ.objects.filter(material=Exam.objects.get(id=request.GET['id']), student=request.user) if journal and journal.exists(): context['code'] = '0' if journal.first().material.theme.empty else '1' return context