def get(self, request): user = request.user course_id = request.GET.get('id') try: course = Course.objects.get(id=course_id, users__in=[user]) days = course.expire userincourse = UsersInCourse.objects.get(course=course, user=user) if userincourse.check_time(days) is None: userincourse.delete() raise Course.DoesNotExist except Course.DoesNotExist: raise PermissionDenied resp = [] schedules = Schedule.objects.filter( course_id=course.id).order_by('week') ws = [sc.week for sc in schedules] weeks = set(ws) for week in weeks: weeklist = [] schedules = Schedule.objects.filter(course_id=course_id, week=week).order_by('day') for sc in schedules: exercises = Exercise.objects.filter( schedule=sc).order_by('order') weeklist.append({ 'day': sc.day, 'exercises': ExerciseSerializer(exercises, many=True).data }) resp.append(weeklist) course = CourseSerializer(course).data course['expired'] = userincourse.get_expire(days) return Response(data={'course': course, 'schedule': resp})
def update_name(self, request, pk=None): course = self.get_object() new_name = request.data.get('new_name') course.name = new_name course.save() return Response(CourseSerializer(course).data)
def user_info(self,request): this_user = request.user if request.user.is_anonymous: return Response(status=status.HTTP_403_FORBIDDEN) if request.user.u_role == 2: # 如果当前用户是学生 #result_set = request.user.registration_set.all() # registration_set 记得要小写Orz result_set = request.user.registration_set.filter(user=this_user,is_quit=False) for e in result_set: print(e.course) return Response({ 'user_info':UserSerializer(request.user).data, 'course_info':RegistrationSerializerByUser(result_set,many=True).data }) elif request.user.u_role == 1: # 如果当前用户是老师 result_set = request.user.tea_course_st.all() #result_set = request.user.tea_course_st.filter(c_teacher=this_user,is_deleted=False) return Response({ 'user_info':UserSerializer(request.user).data, 'course_info':CourseSerializer(result_set,many=True).data })
def get(self, request, format=None): user = request.user courseslist = [] courses = Course.objects.all().order_by('order') faqs = Faq.objects.all() results = Result.objects.all() for course in courses: course_json = CourseSerializer(course).data days = course.expire if user.pk: try: userincourse = UsersInCourse.objects.get(course=course, user=user) course_json['expired'] = userincourse.get_expire(days) except UsersInCourse.DoesNotExist: course_json['expired'] = None course_json['has_permissions'] = True if user.pk and user in course.users.all() else False courseslist.append(course_json) return Response(data={ 'courses': courseslist, 'faqs': FaqSerializer(faqs, many=True).data, 'results': ResultSerializer(results, many=True).data })
def get(self, request): user = request.user courses = Course.objects.filter(users__in=[user]).order_by('order') response = [] if courses: for course in courses: days = course.expire userincourse = UsersInCourse.objects.get(course=course, user=user) if userincourse.check_time(days) is None: userincourse.delete() else: course = CourseSerializer(course).data c = { 'title': course.get("title"), 'is_active': course.get("active"), 'description': course.get("description"), 'photo': course.get("photo"), 'expired': userincourse.get_expire(days) } response.append(c) return Response(data={'courses': response})
def courses(self, request, pk=None): teacher = self.get_object() courses_teacher = [] for course in Course.objects.filter(teacher=teacher): courses_teacher.append(CourseSerializer(course).data) return Response(courses_teacher)
def courses(self, request, pk=None): student = self.get_object() course_user = [] for course in Course.objects.filter(student=student): course_user.append(CourseSerializer(course).data) return Response(course_user)