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 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 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 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 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')