Skip to content

Commit

Permalink
fix(key-terms): Resolving quality checks and pylint warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
ztraboo authored and becdavid committed Jan 16, 2024
1 parent e8278b7 commit 7f17614
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
37 changes: 21 additions & 16 deletions cms/djangoapps/contentstore/courseware_index.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
""" Code to allow module store to interface with courseware index """

import logging
import re

from abc import ABCMeta, abstractmethod
from datetime import timedelta

import json
import logging
import re
import requests

from django.conf import settings
from django.urls import resolve
from django.utils.translation import gettext as _
Expand All @@ -15,10 +19,13 @@

from cms.djangoapps.contentstore.course_group_config import GroupConfiguration
from common.djangoapps.course_modes.models import CourseMode
from lms.lib.utils import get_parent_unit
from opaque_keys.edx.keys import UsageKey
from openedx.core.lib.courses import course_image_url
from xmodule.annotator_mixin import html_to_text
from xmodule.library_tools import normalize_key_for_search
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore as module_store
from xmodule.modulestore.exceptions import ItemNotFoundError

# REINDEX_AGE is the default amount of time that we look back for changes
Expand All @@ -36,19 +43,16 @@


def keyterms_reindex(course_id):
""" Uses key terms API endpoint """
import requests
import json
from xmodule.modulestore.django import modulestore
from lms.lib.utils import get_parent_unit
from opaque_keys.edx.keys import UsageKey
# url for api endpoint
URL = settings.KEY_TERMS_API_REINDEX_URL + "?course_id=" + str(course_id)
"""
Uses key terms API endpoint
"""
# url for keyterms api endpoint for reindexing a course
url_key_terms_api_reindex = settings.KEY_TERMS_API_REINDEX_URL + "?course_id=" + str(course_id)

try:
# when request is sent to endpoint, starts process of retrieving and searching through
# textbooks for key terms
requests.post(URL)
requests.post(url_key_terms_api_reindex)

# retrieve lesson data to be updated
payload = json.dumps({})
Expand All @@ -57,7 +61,7 @@ def keyterms_reindex(course_id):
'Content-Type': 'application/json',
# 'Cookie': 'csrftoken=<token>'
}
response = requests.request("GET", URL, headers=headers, data=payload)
response = requests.request("GET", url_key_terms_api_reindex, headers=headers, data=payload)

if response.status_code == 200:
# holds our updated lesson links
Expand All @@ -68,20 +72,21 @@ def keyterms_reindex(course_id):
if "vertical+block" not in lesson['lesson_link']:
usage_key = UsageKey.from_string(lesson['lesson_link'])
try:
item = modulestore().get_item(usage_key)
item = module_store().get_item(usage_key)
newlink = str(get_parent_unit(item).location)
print(item)
updatedlesson[lesson['lesson_link']] = newlink
except (ItemNotFoundError) as excep:
log.info(
'[keyterms_reindex] Cannot find block %s in modulestore for course %s.\nException: %s',
log.info(
'[keyterms_reindex] Cannot find block %s in modulestore for course'
' %s.\nException: %s',
lesson['lesson_link'], str(course_id), str(excep)
)
else:
updatedlesson[lesson['lesson_link']] = lesson['lesson_link']

# send updated data
return requests.post(URL, json=json.dumps(updatedlesson))
return requests.post(url_key_terms_api_reindex, json=json.dumps(updatedlesson))
elif response.status_code == 204:
log.info(
'[key-terms-api endpoint] No keyterms found when reindexing %s',
Expand Down
8 changes: 2 additions & 6 deletions cms/djangoapps/contentstore/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ def textbooks_list_api_handler(request, course_key_string):
store = modulestore()
with store.bulk_operations(course_key):
course = store.get_course(course_key, depth=0)
# import pdb;pdb.set_trace()

return JsonResponse(course.pdf_textbooks)


Expand All @@ -1560,18 +1560,14 @@ def textbooks_api_handler(request, course_key_string, textbook_name):
GET
pdf: return pdf of ebook chapter
'''
from django.http.response import HttpResponse
from xmodule.contentstore.content import StaticContent
course_key = CourseKey.from_string(course_key_string)
content = contentstore()
# gets location of textbook pdf
# import pdb;pdb.set_trace()
content_key = StaticContent.compute_location(course_key, textbook_name)
# gets textbook pdf from contentstore
textbook_data = content.find(content_key)
textbook = textbook_data._data
textbook = textbook_data._data # lint-amnesty, pylint: disable=protected-access
if request.method == 'GET':
# import pdb;pdb.set_trace()
response = HttpResponse(textbook, content_type='application/pdf')
return response

Expand Down

0 comments on commit 7f17614

Please sign in to comment.