def edxnotes(request, course_id): """ Displays the EdxNotes page. """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, "load", course_key) if not is_feature_enabled(course): raise Http404 try: notes = get_notes(request.user, course) except EdxNotesServiceUnavailable: raise Http404 context = { "course": course, "search_endpoint": reverse("search_notes", kwargs={"course_id": course_id}), "notes": notes, "debug": json.dumps(settings.DEBUG), 'position': None, } if not notes: field_data_cache = FieldDataCache.cache_for_descriptor_descendents( course.id, request.user, course, depth=2 ) course_module = get_module_for_descriptor(request.user, request, course, field_data_cache, course_key) position = get_course_position(course_module) if position: context.update({ 'position': position, }) return render_to_response("edxnotes/edxnotes.html", context)
def test_search_empty_collection(self, mock_get): """ Tests no results. """ mock_get.return_value.content = json.dumps(NOTES_API_EMPTY_RESPONSE) self.assertItemsEqual( NOTES_VIEW_EMPTY_RESPONSE, helpers.get_notes(self.request, self.course) )
def edxnotes(request, course_id): """ Displays the EdxNotes page. Arguments: request: HTTP request object course_id: course id Returns: Rendered HTTP response. """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, "load", course_key) if not is_feature_enabled(course, request.user): raise Http404 notes_info = get_notes(request, course) has_notes = (len(notes_info.get('results')) > 0) context = { "course": course, "notes_endpoint": reverse("notes", kwargs={"course_id": course_id}), "notes": notes_info, "page_size": DEFAULT_PAGE_SIZE, "debug": settings.DEBUG, 'position': None, 'disabled_tabs': settings.NOTES_DISABLED_TABS, 'has_notes': has_notes, } if not has_notes: field_data_cache = FieldDataCache.cache_for_descriptor_descendents( course.id, request.user, course, depth=2) course_module = get_module_for_descriptor(request.user, request, course, field_data_cache, course_key, course=course) position = get_course_position(course_module) if position: context.update({ 'position': position, }) return render_to_response("edxnotes/edxnotes.html", context)
def edxnotes(request, course_id): """ Displays the EdxNotes page. Arguments: request: HTTP request object course_id: course id Returns: Rendered HTTP response. """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, "load", course_key) if not is_feature_enabled(course, request.user): raise Http404 notes_info = get_notes(request, course) has_notes = (len(notes_info.get('results')) > 0) context = { "course": course, "notes_endpoint": reverse("notes", kwargs={"course_id": course_id}), "notes": notes_info, "page_size": DEFAULT_PAGE_SIZE, "debug": settings.DEBUG, 'position': None, 'disabled_tabs': settings.NOTES_DISABLED_TABS, 'has_notes': has_notes, } if not has_notes: field_data_cache = FieldDataCache.cache_for_descriptor_descendents( course.id, request.user, course, depth=2 ) course_module = get_module_for_descriptor( request.user, request, course, field_data_cache, course_key, course=course ) position = get_course_position(course_module) if position: context.update({ 'position': position, }) return render_to_response("edxnotes/edxnotes.html", context)
def test_get_notes_empty_collection(self, mock_get): """ Tests the result if an empty collection is received. """ mock_get.return_value.content = json.dumps([]) self.assertIsNone(helpers.get_notes(self.user, self.course))
def test_get_notes_json_error(self, mock_get): """ Tests the result if incorrect json is received. """ mock_get.return_value.content = "Error" self.assertIsNone(helpers.get_notes(self.user, self.course))
def test_get_notes_correct_data(self, mock_get): """ Tests the result if correct data is received. """ mock_get.return_value.content = json.dumps([{ u"quote": u"quote text", u"text": u"text", u"usage_id": unicode(self.html_module_1.location), u"updated": datetime(2014, 11, 19, 8, 5, 16, 00000).isoformat(), }, { u"quote": u"quote text", u"text": u"text", u"usage_id": unicode(self.html_module_2.location), u"updated": datetime(2014, 11, 19, 8, 6, 16, 00000).isoformat(), }]) self.assertItemsEqual([ { u"quote": u"quote text", u"text": u"text", u"chapter": { u"display_name": self.chapter.display_name_with_default, u"index": 0, u"location": unicode(self.chapter.location), u"children": [unicode(self.sequential.location)] }, u"section": { u"display_name": self.sequential.display_name_with_default, u"location": unicode(self.sequential.location), u"children": [ unicode(self.vertical.location), unicode(self.vertical_with_container.location) ] }, u"unit": { u"url": self._get_unit_url(self.course, self.chapter, self.sequential), u"display_name": self.vertical.display_name_with_default, u"location": unicode(self.vertical.location), }, u"usage_id": unicode(self.html_module_2.location), u"updated": "Nov 19, 2014 at 08:06 UTC", }, { u"quote": u"quote text", u"text": u"text", u"chapter": { u"display_name": self.chapter.display_name_with_default, u"index": 0, u"location": unicode(self.chapter.location), u"children": [unicode(self.sequential.location)] }, u"section": { u"display_name": self.sequential.display_name_with_default, u"location": unicode(self.sequential.location), u"children": [ unicode(self.vertical.location), unicode(self.vertical_with_container.location) ] }, u"unit": { u"url": self._get_unit_url(self.course, self.chapter, self.sequential), u"display_name": self.vertical.display_name_with_default, u"location": unicode(self.vertical.location), }, u"usage_id": unicode(self.html_module_1.location), u"updated": "Nov 19, 2014 at 08:05 UTC", }, ], json.loads(helpers.get_notes(self.user, self.course)))
def notes(request, course_id): """ Notes view to handle list and search requests. Query parameters: page: page number to get page_size: number of items in the page text: text string to search. If `text` param is missing then get all the notes for the current user for this course else get only those notes which contain the `text` value. Arguments: request: HTTP request object course_id: course id Returns: Paginated response as JSON. A sample response is below. { "count": 101, "num_pages": 11, "current_page": 1, "results": [ { "chapter": { "index": 4, "display_name": "About Exams and Certificates", "location": "i4x://org/course/category/name@revision", "children": [ "i4x://org/course/category/name@revision" ] }, "updated": "Dec 09, 2015 at 09:31 UTC", "tags": ["shadow","oil"], "quote": "foo bar baz", "section": { "display_name": "edX Exams", "location": "i4x://org/course/category/name@revision", "children": [ "i4x://org/course/category/name@revision", "i4x://org/course/category/name@revision", ] }, "created": "2015-12-09T09:31:17.338305Z", "ranges": [ { "start": "/div[1]/p[1]", "end": "/div[1]/p[1]", "startOffset": 0, "endOffset": 6 } ], "user": "******", "text": "first angry height hungry structure", "course_id": "edx/DemoX/Demo", "id": "1231", "unit": { "url": "/courses/edx%2FDemoX%2FDemo/courseware/1414ffd5143b4b508f739b563ab468b7/workflow/1", "display_name": "EdX Exams", "location": "i4x://org/course/category/name@revision" }, "usage_id": "i4x://org/course/category/name@revision" } ], "next": "http://0.0.0.0:8000/courses/edx%2FDemoX%2FDemo/edxnotes/notes/?page=2&page_size=10", "start": 0, "previous": null } """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, "load", course_key) if not is_feature_enabled(course): raise Http404 page = request.GET.get("page") or DEFAULT_PAGE page_size = request.GET.get("page_size") or DEFAULT_PAGE_SIZE text = request.GET.get("text") try: notes_info = get_notes(request, course, page=page, page_size=page_size, text=text) except (EdxNotesParseError, EdxNotesServiceUnavailable) as err: return JsonResponseBadRequest({"error": err.message}, status=500) return HttpResponse(json.dumps(notes_info, cls=NoteJSONEncoder), content_type="application/json")
def notes(request, course_id): """ Notes view to handle list and search requests. Query parameters: page: page number to get page_size: number of items in the page text: text string to search. If `text` param is missing then get all the notes for the current user for this course else get only those notes which contain the `text` value. Arguments: request: HTTP request object course_id: course id Returns: Paginated response as JSON. A sample response is below. { "count": 101, "num_pages": 11, "current_page": 1, "results": [ { "chapter": { "index": 4, "display_name": "About Exams and Certificates", "location": "i4x://org/course/category/name@revision", "children": [ "i4x://org/course/category/name@revision" ] }, "updated": "Dec 09, 2015 at 09:31 UTC", "tags": ["shadow","oil"], "quote": "foo bar baz", "section": { "display_name": "edX Exams", "location": "i4x://org/course/category/name@revision", "children": [ "i4x://org/course/category/name@revision", "i4x://org/course/category/name@revision", ] }, "created": "2015-12-09T09:31:17.338305Z", "ranges": [ { "start": "/div[1]/p[1]", "end": "/div[1]/p[1]", "startOffset": 0, "endOffset": 6 } ], "user": "******", "text": "first angry height hungry structure", "course_id": "edx/DemoX/Demo", "id": "1231", "unit": { "url": "/courses/edx%2FDemoX%2FDemo/courseware/1414ffd5143b4b508f739b563ab468b7/workflow/1", "display_name": "EdX Exams", "location": "i4x://org/course/category/name@revision" }, "usage_id": "i4x://org/course/category/name@revision" } ], "next": "http://0.0.0.0:8000/courses/edx%2FDemoX%2FDemo/edxnotes/notes/?page=2&page_size=10", "start": 0, "previous": null } """ course_key = CourseKey.from_string(course_id) course = get_course_with_access(request.user, 'load', course_key) if not is_feature_enabled(course, request.user): raise Http404 page = request.GET.get('page') or DEFAULT_PAGE page_size = request.GET.get('page_size') or DEFAULT_PAGE_SIZE text = request.GET.get('text') try: notes_info = get_notes(request, course, page=page, page_size=page_size, text=text) except (EdxNotesParseError, EdxNotesServiceUnavailable) as err: return JsonResponseBadRequest({"error": text_type(err)}, status=500) return HttpResponse(json.dumps(notes_info, cls=NoteJSONEncoder), content_type="application/json")
def test_search_correct_data(self, mock_get): """ Tests the result if correct data is received. """ mock_get.return_value.content = json.dumps({ "total": 2, "current_page": 1, "start": 0, "next": None, "previous": None, "num_pages": 1, "rows": [ { u"quote": u"quote text", u"text": u"text", u"usage_id": unicode(self.html_module_1.location), u"updated": datetime(2014, 11, 19, 8, 5, 16, 00000).isoformat(), }, { u"quote": u"quote text", u"text": u"text", u"usage_id": unicode(self.html_module_2.location), u"updated": datetime(2014, 11, 19, 8, 6, 16, 00000).isoformat(), } ] }) self.assertItemsEqual( { "count": 2, "current_page": 1, "start": 0, "next": None, "previous": None, "num_pages": 1, "results": [ { u"quote": u"quote text", u"text": u"text", u"chapter": { u"display_name": self.chapter.display_name_with_default_escaped, u"index": 0, u"location": unicode(self.chapter.location), u"children": [unicode(self.sequential.location)] }, u"section": { u"display_name": self.sequential.display_name_with_default_escaped, u"location": unicode(self.sequential.location), u"children": [ unicode(self.vertical.location), unicode(self.vertical_with_container.location)] }, u"unit": { u"url": self._get_unit_url(self.course, self.chapter, self.sequential), u"display_name": self.vertical.display_name_with_default_escaped, u"location": unicode(self.vertical.location), }, u"usage_id": unicode(self.html_module_2.location), u"updated": "Nov 19, 2014 at 08:06 UTC", }, { u"quote": u"quote text", u"text": u"text", u"chapter": { u"display_name": self.chapter.display_name_with_default_escaped, u"index": 0, u"location": unicode(self.chapter.location), u"children": [unicode(self.sequential.location)] }, u"section": { u"display_name": self.sequential.display_name_with_default_escaped, u"location": unicode(self.sequential.location), u"children": [ unicode(self.vertical.location), unicode(self.vertical_with_container.location)] }, u"unit": { u"url": self._get_unit_url(self.course, self.chapter, self.sequential), u"display_name": self.vertical.display_name_with_default_escaped, u"location": unicode(self.vertical.location), }, u"usage_id": unicode(self.html_module_1.location), u"updated": "Nov 19, 2014 at 08:05 UTC", }, ] }, helpers.get_notes(self.request, self.course) )
def test_get_notes_correct_data(self, mock_get): """ Tests the result if correct data is received. """ mock_get.return_value.content = json.dumps([ { u"quote": u"quote text", u"text": u"text", u"usage_id": unicode(self.html_module_1.location), u"updated": datetime(2014, 11, 19, 8, 5, 16, 00000).isoformat(), }, { u"quote": u"quote text", u"text": u"text", u"usage_id": unicode(self.html_module_2.location), u"updated": datetime(2014, 11, 19, 8, 6, 16, 00000).isoformat(), } ]) self.assertItemsEqual( [ { u"quote": u"quote text", u"text": u"text", u"chapter": { u"display_name": self.chapter.display_name_with_default, u"index": 0, u"location": unicode(self.chapter.location), u"children": [unicode(self.sequential.location)] }, u"section": { u"display_name": self.sequential.display_name_with_default, u"location": unicode(self.sequential.location), u"children": [unicode(self.vertical.location), unicode(self.vertical_with_container.location)] }, u"unit": { u"url": self._get_unit_url(self.course, self.chapter, self.sequential), u"display_name": self.vertical.display_name_with_default, u"location": unicode(self.vertical.location), }, u"usage_id": unicode(self.html_module_2.location), u"updated": "Nov 19, 2014 at 08:06 UTC", }, { u"quote": u"quote text", u"text": u"text", u"chapter": { u"display_name": self.chapter.display_name_with_default, u"index": 0, u"location": unicode(self.chapter.location), u"children": [unicode(self.sequential.location)] }, u"section": { u"display_name": self.sequential.display_name_with_default, u"location": unicode(self.sequential.location), u"children": [ unicode(self.vertical.location), unicode(self.vertical_with_container.location)] }, u"unit": { u"url": self._get_unit_url(self.course, self.chapter, self.sequential), u"display_name": self.vertical.display_name_with_default, u"location": unicode(self.vertical.location), }, u"usage_id": unicode(self.html_module_1.location), u"updated": "Nov 19, 2014 at 08:05 UTC", }, ], json.loads(helpers.get_notes(self.user, self.course)) )