def get(self, request, format=None): """ Get all the courses. """ param = { 'offset': int(request.GET.get('offset', 0)), 'limit': int(request.GET.get('limit', 10)), 'timestamp': int(request.GET.get('timestamp', 0)), 'with_category': request.GET.get('with_category', 1), 'categories': request.GET.get('categories', '') } screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='list') categories = [each for each in param['categories'].split(',') if each] verified_courses_id = CourseMode.get_verified_option_courses() query = Course.exclude_vpc().filter(status__gte=0, course_type=0).exclude(course_id__in=verified_courses_id) if param['timestamp']: query = query.filter( modified__gt=datetime.utcfromtimestamp(param['timestamp'])) if categories: query = query.filter(category__id__in=categories).distinct() total = query.count() courses = query[param['offset']:param['offset'] + param['limit']] result = { "courses": CourseWithCategorySerializer(thumbnail_size, courses, many=True).data if param['with_category'] else CourseSerializer(thumbnail_size, courses, many=True).data, "total": total, } return Response(result)
def get(self, request, cid, format=None): """ Get the courses of current category. """ param = { 'offset': int(request.GET.get('offset', 0)), 'limit': int(request.GET.get('limit', 10)), 'show_staffs': int(request.GET.get('show_staffs', 0)), } screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='list') category = CourseCategory.objects.get(id=cid) user_agent = request.META.get('HTTP_USER_AGENT', '').lower() if 'androidtv' in user_agent: is_tv = True else: is_tv = False if is_tv: query = category.course_set.filter((~Q(status=-1)) & (Q(course_type=1))) else: query = category.course_set.filter(status__gte=0, course_type=0) total = query.count() courses = query[param['offset']:param['offset'] + param['limit']] if param['show_staffs']: courses_json = CourseSerializer(thumbnail_size, courses, many=True).data else: courses_json = CourseWithoutStaffSerializer(thumbnail_size, courses, many=True).data result = { "courses": courses_json, "total": total, } return Response(result)
def get(self, request, kid, format=None): """ Get the courses of current knowledge map. """ screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='list') knowledgemaptags = CourseInKnowledgeMap.objects.filter(map_id_id=kid) courses = [kt.course_id for kt in knowledgemaptags] result = { "courses": CourseSerializer(thumbnail_size, courses, many=True).data, } return Response(result)
def get(self, request, format=None): """ Get all the hot courses. """ screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='list') homepage_courses = HomepageCourses.objects.select_related('course').filter(course__course_type=0).order_by('order')[0:8] courses = [hc.course for hc in homepage_courses] result = { "courses": CourseSerializer(thumbnail_size, courses, many=True).data } return Response(result)
def get(self, request, format=None): """ Get all the hot courses. """ screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='list') homepage_fragments = HomepageFragmentKnowledge.objects.filter(enabled=True, is_draft=False).order_by('order')[0:8] fragments = [hf.fragment for hf in homepage_fragments] params = { 'thumbnail_size': thumbnail_size, } result = { "fragments": FragmentKnowledgeSerializer(fragments, many=True, context=params).data } return Response(result)
def get(self, request): param = { 'offset': int(request.GET.get('offset', 0)), 'limit': int(request.GET.get('limit', 10)), } screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='list') user = request.user _followed_course = Follow.targets(user, 'course_meta.models.Course') followed_course = _followed_course.filter(course_type=0) total = followed_course.count() courses = followed_course[param['offset']:param['offset'] + param['limit']] result = { "courses": CourseSerializer(thumbnail_size, courses, many=True).data, "total": total, } return Response(result)
def get(self, request, course_id, format=None): """ Get course details. """ params = { 'without_chapters': int(request.GET.get('without_chapters', 0)), 'without_qas': int(request.GET.get('without_qas', 0)), 'display_recommend_courses': int(request.GET.get('display_recommend_courses', 0)), } screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='detail') try: course = Course.objects.get(course_id=course_id) except: return Response(status=status.HTTP_404_NOT_FOUND) result = CourseDetailSerializer(thumbnail_size, course, context=params).data if params['display_recommend_courses'] > 0: qset = Course.objects.filter((~Q(status=-1)) & (Q(course_type=1))).exclude(id=course.id) count = qset.count() if count > params['display_recommend_courses']: slice = random.random() * (count - params['display_recommend_courses']) r_cs = qset[slice: slice + params['display_recommend_courses']] else: r_cs = qset r_params = { 'without_chapters': 1, 'without_qas': 1, 'display_recommend_courses': 0, } result['recommend_courses'] = [] for course in r_cs: result['recommend_courses'].append(CourseDetailSerializer(thumbnail_size, course, context=r_params).data) else: result['recommend_courses'] = [] return Response(result)
def get(self, request, format=None): """ Get the courses that user has enrolled, sort by enroll time. """ param = { 'offset': int(request.GET.get('offset', 0)), 'limit': int(request.GET.get('limit', 10)), } screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='detail') enrollments = exclude_vpc_and_selfpaced_enrollments(request.user) total = len(enrollments) courses = [] for e in enrollments[param['offset']:param['offset'] + param['limit']]: try: courses.append(Course.objects.get(course_id=e.course_id)) except: pass result = { "courses": CourseSerializer(thumbnail_size, courses, many=True).data, "total": total, } return Response(result)
def get(self, request, format=None): """ Get all the recent courses. """ param = { 'offset': int(request.GET.get('offset', 0)), 'limit': int(request.GET.get('limit', 10)), } screen = get_screen_size(request) thumbnail_size = get_thumbnail_size(screen, position='detail') local_tz = pytz_timezone(settings.TIME_ZONE) now = datetime.now(local_tz) recent_time = datetime(now.year, now.month, now.day, tzinfo=local_tz) recent_utc_time = recent_time.astimezone(UTC) verified_courses_id = CourseMode.get_verified_option_courses() query = Course.exclude_vpc().filter( status__gte=0, start__gt=recent_utc_time, course_type=0).exclude(course_id__in=verified_courses_id).order_by('start') total = query.count() courses = query[param['offset']:param['offset'] + param['limit']] result = { "courses": CourseSerializer(thumbnail_size, courses, many=True).data, "total": total, } return Response(result)