def post(self, request): """ Update the ID of the module that the specified user last visited in the specified course. """ courses = request.DATA.get("courses", "[]") courses = json.loads(courses) result = [] for course in courses: course_id = course.get('course_id') chapter_id = course.get('chapter_id') sequential_id = course.get('sequential_id') modification_date_string = course.get("timestamp") if not course_id: continue if not chapter_id: try: course = get_course(course_id) if course: result.append(self._get_course_info(request, course)) except Exception as ex: log.info(ex) continue else: result.append(self._modify_course_status(request, course_id, chapter_id, sequential_id, modification_date_string)) return Response({'courses': result})
def get(self, request, course_name, format=None): """ Get course navigational elements """ course = get_course(course_name) now = datetime.now(UTC()) # try: # courses = CourseEnrollment.objects.get( # user=request.user, course_id=course_name, is_active=True) # except: # return Response(status=status.HTTP_404_NOT_FOUND) is_enrolled = CourseEnrollment.is_enrolled(request.user, course_name) if not is_enrolled: return Response(status=status.HTTP_404_NOT_FOUND) result = { "success": True, "course": { "display_name": course.display_name, "location": str(course.location), "start": course.start, "children": _get_children(course_name, course.children, 'chapter', now), } } chapters = result['course']['children'] for chapter in chapters: sequentials = chapter['children'] = _get_children( course_name, chapter['children'], 'sequential', now) for sequential in sequentials: verticals = sequential['children'] = _get_children( course_name, sequential['children'], 'vertical', now) for vertical in verticals: children = [get_item(course_name, 'video', item_id) for item_id in _get_item_id_list( vertical['children'], 'video')] children = filter(lambda x: x and x.start < now, children) vertical['children'] = [] course_key = SlashSeparatedCourseKey.from_string(course_name) for video in children: data = { 'display_name': video.display_name, 'location': str(video.location), 'start': video.start, 'track_zh': get_track_zh(video, course_key), 'track_en': get_track_en(video, course_key), 'source': get_video_source(video), } try: ccsource = video.ccsource.strip() data['length'] = VideoInfo.objects.get( vid=ccsource).duration except: data['length'] = 0 vertical['children'].append(data) return Response(result)
def get(self, request, course_id): """ Get the ID of the module that the specified user last visited in the specified course. """ try: course = get_course(course_id) if not course: return Response(status=status.HTTP_404_NOT_FOUND) except Exception as ex: log.error(ex) return Response(status=status.HTTP_404_NOT_FOUND) return Response(self._get_course_info(request, course))
def get(self, request): courses = request.GET.get("courses", "[]") courses = json.loads(courses) result = [] for course in courses: course_id = course.get('course_id') if not course_id: continue try: course = get_course(course_id) except Course.DoesNotExist: continue result.append(self._get_course_info(request, course)) return Response({'courses': result})
def _modify_course_status(self, request, course_id, chapter_id, sequential_id, modification_date_string): modification_date = None try: course = get_course(course_id) except Exception: return Response(status=status.HTTP_404_NOT_FOUND) if not course: return Response(status=status.HTTP_404_NOT_FOUND) if modification_date_string: modification_date = dateparse.parse_datetime(modification_date_string) if not modification_date or not modification_date.tzinfo: try: modification_date = datetime.utcfromtimestamp(int(modification_date_string) / 1000.0) # 应该通过0时区对比 modification_date = timezone.make_aware(modification_date, timezone.utc) except Exception: modification_date = None if sequential_id: return self._update_last_visited_sequential(request, course, chapter_id, sequential_id, modification_date) else: # The arguments are optional, so if there's no argument just succeed return self._get_course_info(request, course)
def get(self, request, course_id, format=None): """ Get course chapter list. 注意:如果移动端需要的vertical_types需要video之外的东西,整个方法需要重构 """ if not Course.objects.filter(course_id=course_id).exists(): return Response(status=status.HTTP_404_NOT_FOUND) if not CourseEnrollment.is_enrolled(request.user, course_id): return Response(status=status.HTTP_403_FORBIDDEN) user_agent = request.META.get('HTTP_USER_AGENT', '').lower() if 'androidtv' in user_agent: is_tv = True else: is_tv = False show_sequentials = request.GET.get('show_sequentials') if show_sequentials: if show_sequentials == '1' or show_sequentials.lower() == 'true': show_sequentials = True else: show_sequentials = False # 首先取一下缓存 if show_sequentials: # 手动清除缓存的后面 invalite_cache = request.GET.get('cache', None) if invalite_cache: cache_key_tv = 'api.course.{}.chapters_with_seq.{}'.format(course_id, True) cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, False) cache.delete(cache_key_tv) cache.delete(cache_key) if settings.DEBUG: cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, is_tv) + str(time.time()) else: cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, is_tv) cache_result = cache.get(cache_key) if cache_result: return Response(cache_result) course = get_course(course_id) chapters = [get_item(course_id, "chapter", chapter_id) for chapter_id in _get_item_id_list(course.children, "chapter")] now = datetime.now(UTC) chapters = filter(lambda x: x and x.start < now, chapters) if show_sequentials: # key sequential, value {video:0} seq_type_dict = defaultdict(lambda : defaultdict(int)) # 首先查出vertical需要的block列表 vertical_types = ['video'] vertical_dict = {vt: set() for vt in vertical_types} for vtype in vertical_types: blocks = get_items(course_id, vtype) blocks = filter(lambda x: x and x.start < now, blocks) vtype_set = _get_vertical_set(blocks) vertical_dict[vtype] = vtype_set for chapter in chapters: sequentials = [get_item(course_id, "sequential", sequential_id) for sequential_id in _get_item_id_list(chapter.children, "sequential")] sequentials = filter(lambda x: x and x.start < now, sequentials) chapter.sequentials = sequentials for sequential in sequentials: verticals = [get_item(course_id, "vertical", vertical_id) for vertical_id in _get_item_id_list(sequential.children, "vertical")] verticals = filter(lambda x: x and x.start < now, verticals) sequential.verticals = verticals # 通过之前查出的block集合 for vertical in verticals: blocks = vertical.children for block in blocks: category = _get_location_category(block) block_location_id = _get_location_id(block) if category in vertical_dict and block_location_id in vertical_dict[category]: seq_type_dict[sequential][category] += 1 for sequential, types in seq_type_dict.iteritems(): sequential.type = dict(types) chapters_array = ChapterWithSequentialSerializer(chapters, many=True).data if is_tv: cp_array = [] for chapters_temp in chapters_array: sq_array = [] for sq_temp in chapters_temp['sequentials']: if sq_temp['type'].get('video', None): # tv端过滤非video sq_array.append(sq_temp) chapters_temp['sequentials'] = sq_array if chapters_temp['sequentials'] !=[]: cp_array.append(chapters_temp) chapters_array = cp_array result = { "chapters": chapters_array, } if is_tv: cache.set(cache_key, result, 60 * 60 * 24 * 7) else: cache.set(cache_key, result, 60 * 60) else: result = { "chapters": ChapterSerializer(chapters, many=True).data } return Response(result)