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