def lesson(request, course_slug, lesson_slug=None): course = get_object_or_404(Course, slug=course_slug) if not request.user in course.active_teachers(): request.user.message_set.create(message="That action may only be performed by \ teachers of the \"%s\" course. If you are a teacher please log in." % course) return HttpResponseRedirect(reverse("acct_login")) if lesson_slug: lesson = get_object_or_404(Lesson, course=course, slug=lesson_slug) template = "courses/lessons/edit.html" else: lesson = Lesson(course=course) template = "courses/lessons/create.html" if request.method == 'POST': form = LessonForm(request.POST, instance=lesson) if form.is_valid(): if lesson_slug: form.save() request.user.message_set.create(message="Lesson changes have \ been saved") else: lesson = form.save() request.user.message_set.create(message="Lesson has been added") return HttpResponseRedirect(lesson.get_absolute_url()) form = LessonForm(instance=lesson) return render_to_response(template, { 'form': form, 'course': course, 'lesson': lesson }, context_instance=RequestContext(request))
def test_is_created_preview_image(self): storage = Lesson().video.storage lesson = Lesson.objects.first() image_preview = lesson.video.preview_image_name self.assertTrue(storage.exists(image_preview), "Отсутсвует изображение-превью для видеоплеера")
def test_is_created_chunks(self): storage = Lesson().video.storage expected_streams_count = 5 expected_count_of_chunks_for_stream = 2 lesson = Lesson.objects.first() dash_seg_name = DashFilesNames.dash_segments_mask(lesson.video.name) dash_seg_name_mask = dash_seg_name. \ replace(r"\$RepresentationID\$", "{0}"). \ replace(r"\$Number%05d\$", "{1}") for stream_id in range(expected_streams_count): chunks_count = 0 for chunk_id in range(1, expected_count_of_chunks_for_stream + 1): chunk_name = dash_seg_name_mask.format(stream_id, str(chunk_id).zfill(5)) if storage.exists(chunk_name): chunks_count += 1 self.assertEqual( chunks_count, expected_count_of_chunks_for_stream, f"Неверное количество чанков для DASH потока №{stream_id}, " f"ожидалось {expected_count_of_chunks_for_stream}, существует {chunks_count}" )
def lesson(request, course_slug, lesson_slug=None): course = get_object_or_404(Course, slug=course_slug) if not request.user in course.active_teachers(): request.user.message_set.create( message="That action may only be performed by \ teachers of the \"%s\" course. If you are a teacher please log in." % course) return HttpResponseRedirect(reverse("acct_login")) if lesson_slug: lesson = get_object_or_404(Lesson, course=course, slug=lesson_slug) template = "courses/lessons/edit.html" else: lesson = Lesson(course=course) template = "courses/lessons/create.html" if request.method == 'POST': form = LessonForm(request.POST, instance=lesson) if form.is_valid(): if lesson_slug: form.save() request.user.message_set.create(message="Lesson changes have \ been saved") else: lesson = form.save() request.user.message_set.create( message="Lesson has been added") return HttpResponseRedirect(lesson.get_absolute_url()) form = LessonForm(instance=lesson) return render_to_response(template, { 'form': form, 'course': course, 'lesson': lesson }, context_instance=RequestContext(request))
def test_model_relation_course_missing(self): course = Course.objects.create(name='test1') lesson = Lesson() # Lesson without `course` set with self.assertRaises(ValidationError): lesson.full_clean() lesson.save() self.assertEqual( Lesson.objects.filter(course__name='test2').count(), 0)
def setUp(self): """Sets up the test case by creating a sample text and user""" self.u = User.objects.create_user("Mittens", "*****@*****.**", "meow") self.u.save() c = Course() c.save() l = Lesson(course=c) l.save() self.s = Section(lesson=l) self.s.save()
def test_model_relation(self): course = Course.objects.create(name='test1') lesson = Lesson(course=Course.objects.create(name='test2'), subject='test1', description='test1', order=int('1') ) lesson.full_clean() # `course` correctly set. This should pass lesson.save() self.assertEqual( Lesson.objects.filter(course__name='test2').count(), 1)
def test_is_destroy(self): lesson = Lesson.objects.last() storage = Lesson().video.storage video_name = lesson.video.name lesson.video.delete() self._check_is_init_files_deleted(video_name, storage) self._check_is_seg_files_deleted(video_name, storage) self._check_is_preview_files_deleted(video_name, storage) self._check_is_mpd_manifest_deleted(video_name, storage)
def setUp(self): """Set up test cases, this creates users, a task and user on task instances""" self.m = User.objects.create_user("Mittens", "*****@*****.**", "meow") self.m.save() self.f = User.objects.create_user("Fido", "*****@*****.**", "woof") self.f.save() c = Course() c.save() l = Lesson(course=c) l.save() s = Section(lesson=l) s.save() self.t = Task(section=s, model_answer="replaced", language="dummy") self.t.save() self.muot = self.t.get_uot(self.m) self.fuot = self.t.get_uot(self.f)
def test_is_created_dash_init_files(self): storage = Lesson().video.storage expected_count_of_stream_init = 5 lesson = Lesson.objects.first() stream_init_name = DashFilesNames.dash_init_files_mask( lesson.video.name) stream_init_name_mask = stream_init_name.replace( r"\$RepresentationID\$", "{0}") stream_init_files_count = 0 for stream_id in range(expected_count_of_stream_init): init_name = stream_init_name_mask.format(stream_id) if storage.exists(init_name): stream_init_files_count += 1 self.assertEqual( stream_init_files_count, expected_count_of_stream_init, f"Неверное количество файлов описания для DASH потоков" f"ожидалось {expected_count_of_stream_init}, существует {stream_init_files_count}" )
def post(self, request): ID = request.POST.get("id", "") type = request.POST.get("type", "") if int(type) > 0: #添加课程 #此处类似于 读者-写者 模型 #添加行级锁,防止改动课程时用户进入读取错误信息 with transaction.atomic(): if ID: lesson = Lesson.objects.select_for_update().get(id=ID) else: lesson = Lesson() lesson.course_id = request.POST.get("cId", "") lesson.name = request.POST.get("name", "") lesson.save() video = Video() video.lesson_id = lesson.id video.name = lesson.name video.url = request.POST.get("url", "") video.save() else: #进行事务处理,保证课程发布可以通知到每个关注者 with transaction.atomic(): if ID: course = Course.objects.select_for_update().get(id=ID) else: course = Course() teacher = Teacher.objects.get(id=request.user.teacher_id) course.name = request.POST.get("name", "") course.tag = request.POST.get("tag", "") course.desc = request.POST.get("desc", "") course.detail = request.POST.get("detail", "") course.degree = request.POST.get("degree", "") course.learn_times = request.POST.get("learn_times", "") course.teacher = teacher course.save() teacher_id = teacher.id users = UserFavorite.objects.filter(fav_id=teacher_id, fav_type=1) if ID: for user in users: sendMsg(user.user_id, teacher.name + "修改了课程:" + course.name, teacher.id) else: for user in users: sendMsg(user.user_id, teacher.name + "发布了新课程:" + course.name, teacher.id) return render(request, 'usercenter-publish-course.html', {})
def post(cls, request): lesson_form = CreateLessonForm(request.POST) if lesson_form.is_valid(): name = lesson_form.cleaned_data['name'] course = lesson_form.cleaned_data['course'] lesson = Lesson() lesson.name = name lesson.course_id = int(course) lesson.save() teacher = Teacher.objects.get(userprofile=request.user) courses = Course.objects.filter(teacher=teacher) lessons = [] for course in courses: lessons.extend(Lesson.objects.filter(course=course)) return render(request, 'user_lecturer_course.html', { 'courses': courses, 'lessons': lessons, }) else: return HttpResponse('{"status":"fail"}', content_type='application/json')
def test_is_created_mpd_manifest(self): storage = Lesson().video.storage lesson = Lesson.objects.first() self.assertTrue(storage.exists(lesson.video.mpd_file_name), "Не сгенерирован mpd файл манифеста для DASH")