Skip to content

Commit

Permalink
feat: upgrading simple api to drf compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
awais786 committed Aug 15, 2024
1 parent f9b53b5 commit c3853ec
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 55 deletions.
13 changes: 6 additions & 7 deletions lms/djangoapps/instructor/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ def test_endpoints_accept_get(self, data):
f"Endpoint {data} returned status code 405 where it shouldn't, since it should allow GET."


from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme


@patch('lms.djangoapps.bulk_email.models.html_to_text', Mock(return_value='Mocking CourseEmail.text_message', autospec=True)) # lint-amnesty, pylint: disable=line-too-long
class TestInstructorAPIDenyLevels(SiteMixin,SharedModuleStoreTestCase, LoginEnrollmentTestCase):
"""
Expand Down Expand Up @@ -469,8 +472,7 @@ def _access_endpoint(self, endpoint, args, status_code, msg, content_type=MULTIP
status_code: expected HTTP status code response
msg: message to display if assertion fails.
"""
import pdb;
pdb.set_trace()

url = reverse(endpoint, kwargs={'course_id': str(self.course.id)})
# if endpoint in INSTRUCTOR_GET_ENDPOINTS:
# response = self.client.get(url, args)
Expand All @@ -489,17 +491,15 @@ def test_student_level(self):
# UserPreference.objects.create(user=self.user, key="preview-site-theme", value="test-theme")
# UserPreference.objects.create(user=self.user, key="pref-lang", value="en")


self.client.login(username=self.user.username, password=self.TEST_PASSWORD)

for endpoint, args in self.staff_level_endpoints:
self._access_endpoint(
endpoint,
args,
403,
"Student should not be allowed to access endpoint " + endpoint
)

#
from common.djangoapps.util.db import outer_atomic
with outer_atomic():
for endpoint, args in self.instructor_level_endpoints:
Expand All @@ -524,6 +524,7 @@ def _access_problem_responses_endpoint(self, endpoint, msg):
patched_method.return_value = mock_problem_key
self._access_endpoint(endpoint, {"problem_locations": ["test"]}, 200, msg, content_type="application/json")

@with_comprehensive_theme("test-theme")
def test_staff_level(self):
"""
Ensure that a staff member can't access instructor endpoints.
Expand Down Expand Up @@ -628,8 +629,6 @@ def test_instructor_level(self):
"Instructor should be allowed to access endpoint " + endpoint
)

import pdb;
pdb.set_trace()
for endpoint, args in self.instructor_level_endpoints:
expected_status = 200
self._access_endpoint(
Expand Down
21 changes: 7 additions & 14 deletions lms/djangoapps/instructor/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@ def require_course_permission(permission):
"""
def decorator(func):
def wrapped(*args, **kwargs):
import pdb;
pdb.set_trace()
request = args[0]
course = get_course_by_id(CourseKey.from_string(kwargs['course_id']))

Expand Down Expand Up @@ -1512,12 +1510,7 @@ class GetStudentsWhoMayEnroll(APIView):
"""
Initiate generation of a CSV file containing information about
"""
authentication_classes = (
JwtAuthentication,
BearerAuthenticationAllowInactiveUser,
SessionAuthenticationAllowInactiveUser,
)
permission_classes = (IsAuthenticated, IsAdminUser, permissions.InstructorPermission)
permission_classes = (IsAuthenticated,)
permission_name = permissions.CAN_RESEARCH

@method_decorator(ensure_csrf_cookie)
Expand All @@ -1532,13 +1525,13 @@ def post(self, request, course_id):
"""
import pdb;
pdb.set_trace()
course_key = CourseKey.from_string(course_id)
query_features = ['email']
report_type = _('enrollment')
task_api.submit_calculate_may_enroll_csv(request, course_key, query_features)
success_status = SUCCESS_MESSAGE_TEMPLATE.format(report_type=report_type)
# course_key = CourseKey.from_string(course_id)
# query_features = ['email']
# report_type = _('enrollment')
# # task_api.submit_calculate_may_enroll_csv(request, course_key, query_features)
# success_status = SUCCESS_MESSAGE_TEMPLATE.format(report_type=report_type)

return Response({"status": success_status})
return Response({"status": 200})

def get(self, request, *args, **kwargs):
"""
Expand Down
30 changes: 14 additions & 16 deletions lms/templates/header/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@
<%
unsupported_browser_alert_versions = configuration_helpers.get_value('UNSUPPORTED_BROWSER_ALERT_VERSIONS', settings.FEATURES.get('UNSUPPORTED_BROWSER_ALERT_VERSIONS'))
%>
% if waffle.switch_is_active('enable_unsupported_browser_alert'):
<script>
var $buoop = {
notify:${unsupported_browser_alert_versions | n, decode.utf8},
api:5,
reminder:0
};
function $buo_f() {
var e = document.createElement("script");
e.src = "//browser-update.org/update.min.js";
document.body.appendChild(e);
};
try {document.addEventListener("DOMContentLoaded", $buo_f,false)}
catch(e){window.attachEvent("onload", $buo_f)}
</script>
% endif
<script>
var $buoop = {
notify:${unsupported_browser_alert_versions | n, decode.utf8},
api:5,
reminder:0
};
function $buo_f() {
var e = document.createElement("script");
e.src = "//browser-update.org/update.min.js";
document.body.appendChild(e);
};
try {document.addEventListener("DOMContentLoaded", $buo_f,false)}
catch(e){window.attachEvent("onload", $buo_f)}
</script>

<header class="global-header ${'slim' if course else ''}">
% if settings.FEATURES.get('ENABLE_COOKIE_POLICY_BANNER', use_cookie_banner):
Expand Down
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/lang_pref/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def process_response(self, request, response): # lint-amnesty, pylint: disable=
else:
# Get the user's language preference
try:
import pdb;
pdb.set_trace()
user_pref = get_user_preference(current_user, LANGUAGE_KEY)
except (UserAPIRequestError, UserAPIInternalError):
# If we can't find the user preferences, then don't modify the cookie
Expand Down
1 change: 1 addition & 0 deletions openedx/core/djangoapps/theming/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def get_theme_base_dir(theme_dir_name, suppress_error=False):
Returns:
(str): Base directory that contains the given theme
"""
return
for themes_dir in get_theme_base_dirs():
if theme_dir_name in get_theme_dirs(themes_dir):
return themes_dir
Expand Down
16 changes: 1 addition & 15 deletions openedx/core/djangoapps/theming/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,4 @@ def process_request(self, request):
Set the request's 'site_theme' attribute based upon the current user.
"""
# Specifying a "site_theme" querystring param takes precedence
qs_theme = request.GET.get('site_theme')

# Determine if the user has specified a preview site
preview_site_theme = get_user_preview_site_theme(request)

if qs_theme:
site_theme = SiteTheme(site=request.site, theme_dir_name=qs_theme)
elif preview_site_theme:
site_theme = preview_site_theme
else:
default_theme = None
if settings.DEFAULT_SITE_THEME:
default_theme = SiteTheme(site=request.site, theme_dir_name=settings.DEFAULT_SITE_THEME)
site_theme = SiteTheme.get_theme(request.site, default=default_theme)
request.site_theme = site_theme
pass
7 changes: 5 additions & 2 deletions openedx/core/djangoapps/user_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ def get_value(cls, user, preference_key, default=None):
try:
import pdb;
pdb.set_trace()
user_preference = cls.objects.get(user=user, key=preference_key)
return user_preference.value
user_preference, __ = cls.objects.get_or_create(user=user, key='pref-lang', value='en')
if user_preference:
return user_preference.value
user_preference, __ = cls.objects.get_or_create(user=user, key='pref-lang', value='en')
return user_preference
except cls.DoesNotExist:
return default

Expand Down
4 changes: 3 additions & 1 deletion openedx/core/djangoapps/user_api/preferences/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def has_user_preference(requesting_user, preference_key, username=None):
return UserPreference.has_value(existing_user, preference_key)


@intercept_errors(UserAPIInternalError, ignore_errors=[UserAPIRequestError])
# @intercept_errors(UserAPIInternalError, ignore_errors=[UserAPIRequestError])
def get_user_preference(requesting_user, preference_key, username=None):
"""Returns the value of the user preference with the specified key.
Expand All @@ -79,6 +79,8 @@ def get_user_preference(requesting_user, preference_key, username=None):
UserNotAuthorized: the requesting_user does not have access to the user preference.
UserAPIInternalError: the operation failed due to an unexpected error.
"""
import pdb;
pdb.set_trace()
existing_user = _get_authorized_user(requesting_user, username, allow_staff=True)
return UserPreference.get_value(existing_user, preference_key)

Expand Down

0 comments on commit c3853ec

Please sign in to comment.