-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Update forum role membership api to DRF ( 11th ) #35343
Draft
awais786
wants to merge
6
commits into
master
Choose a base branch
from
update_forum_role_membership
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−52
Draft
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7bf97aa
feat: upgrading simple api to drf compatible.
awais786 b6b211a
feat: upgrading simple api to drf compatible.
awais786 31cd269
feat: upgrading simple api to drf compatible.
awais786 8cb1bb7
feat: upgrading simple api to drf compatible.
awais786 b8e6185
Merge branch 'master' into update_forum_role_membership
awais786 9805519
Merge branch 'master' into update_forum_role_membership
awais786 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,9 @@ | |
from lms.djangoapps.instructor_task.api_helper import AlreadyRunningError, QueueConnectionError | ||
from lms.djangoapps.instructor_task.data import InstructorTaskTypes | ||
from lms.djangoapps.instructor_task.models import ReportStore | ||
from lms.djangoapps.instructor.views.serializer import RoleNameSerializer, UserSerializer, AccessSerializer | ||
from lms.djangoapps.instructor.views.serializer import ( | ||
RoleNameSerializer, UserSerializer, AccessSerializer, UpdateForumRoleMembershipSerializer | ||
) | ||
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview | ||
from openedx.core.djangoapps.course_groups.cohorts import add_user_to_cohort, is_course_cohorted | ||
from openedx.core.djangoapps.course_groups.models import CourseUserGroup | ||
|
@@ -2814,17 +2816,8 @@ def send_email(request, course_id): | |
return JsonResponse(response_payload) | ||
|
||
|
||
@require_POST | ||
@ensure_csrf_cookie | ||
@cache_control(no_cache=True, no_store=True, must_revalidate=True) | ||
@require_course_permission(permissions.EDIT_FORUM_ROLES) | ||
@require_post_params( | ||
unique_student_identifier="email or username of user to change access", | ||
rolename="the forum role", | ||
action="'allow' or 'revoke'", | ||
) | ||
@common_exceptions_400 | ||
def update_forum_role_membership(request, course_id): | ||
@method_decorator(cache_control(no_cache=True, no_store=True, must_revalidate=True), name='dispatch') | ||
class UpdateForumRoleMembership(APIView): | ||
""" | ||
Modify user's forum role. | ||
|
||
|
@@ -2833,52 +2826,62 @@ def update_forum_role_membership(request, course_id): | |
which is limited to instructors. | ||
No one can revoke an instructors FORUM_ROLE_ADMINISTRATOR status. | ||
|
||
Query parameters: | ||
- `email` is the target users email | ||
- `rolename` is one of [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_GROUP_MODERATOR, | ||
FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA] | ||
- `action` is one of ['allow', 'revoke'] | ||
""" | ||
course_id = CourseKey.from_string(course_id) | ||
course = get_course_by_id(course_id) | ||
has_instructor_access = has_access(request.user, 'instructor', course) | ||
has_forum_admin = has_forum_access( | ||
request.user, course_id, FORUM_ROLE_ADMINISTRATOR | ||
) | ||
permission_classes = (IsAuthenticated, permissions.InstructorPermission) | ||
permission_name = permissions.EDIT_FORUM_ROLES | ||
serializer_class = UpdateForumRoleMembershipSerializer | ||
|
||
unique_student_identifier = request.POST.get('unique_student_identifier') | ||
rolename = request.POST.get('rolename') | ||
action = request.POST.get('action') | ||
@method_decorator(ensure_csrf_cookie) | ||
def post(self, request, course_id): | ||
""" | ||
Handles role modification requests for a forum user. | ||
|
||
# default roles require either (staff & forum admin) or (instructor) | ||
if not (has_forum_admin or has_instructor_access): | ||
return HttpResponseBadRequest( | ||
"Operation requires staff & forum admin or instructor access" | ||
Query parameters: | ||
- `email` is the target users email | ||
- `rolename` is one of [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_GROUP_MODERATOR, | ||
FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA] | ||
- `action` is one of ['allow', 'revoke'] | ||
""" | ||
course_id = CourseKey.from_string(course_id) | ||
course = get_course_by_id(course_id) | ||
has_instructor_access = has_access(request.user, 'instructor', course) | ||
has_forum_admin = has_forum_access( | ||
request.user, course_id, FORUM_ROLE_ADMINISTRATOR | ||
) | ||
|
||
# EXCEPT FORUM_ROLE_ADMINISTRATOR requires (instructor) | ||
if rolename == FORUM_ROLE_ADMINISTRATOR and not has_instructor_access: | ||
return HttpResponseBadRequest("Operation requires instructor access.") | ||
serializer_data = UpdateForumRoleMembershipSerializer(data=request.data) | ||
if not serializer_data.is_valid(): | ||
return HttpResponseBadRequest(reason=serializer_data.errors) | ||
|
||
if rolename not in [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_GROUP_MODERATOR, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this line, now serializer will do all the validation. |
||
FORUM_ROLE_COMMUNITY_TA]: | ||
return HttpResponseBadRequest(strip_tags( | ||
f"Unrecognized rolename '{rolename}'." | ||
)) | ||
user = serializer_data.validated_data.get('unique_student_identifier') | ||
if not user: | ||
return JsonResponse({'error': 'User does not exist.'}, status=400) | ||
|
||
user = get_student_from_identifier(unique_student_identifier) | ||
if action == 'allow' and not is_user_enrolled_in_course(user, course_id): | ||
CourseEnrollment.enroll(user, course_id) | ||
try: | ||
update_forum_role(course_id, user, rolename, action) | ||
except Role.DoesNotExist: | ||
return HttpResponseBadRequest("Role does not exist.") | ||
rolename = serializer_data.data['rolename'] | ||
action = serializer_data.data['action'] | ||
|
||
response_payload = { | ||
'course_id': str(course_id), | ||
'action': action, | ||
} | ||
return JsonResponse(response_payload) | ||
# default roles require either (staff & forum admin) or (instructor) | ||
if not (has_forum_admin or has_instructor_access): | ||
return HttpResponseBadRequest( | ||
"Operation requires staff & forum admin or instructor access" | ||
) | ||
|
||
# EXCEPT FORUM_ROLE_ADMINISTRATOR requires (instructor) | ||
if rolename == FORUM_ROLE_ADMINISTRATOR and not has_instructor_access: | ||
return HttpResponseBadRequest("Operation requires instructor access.") | ||
|
||
if action == 'allow' and not is_user_enrolled_in_course(user, course_id): | ||
CourseEnrollment.enroll(user, course_id) | ||
try: | ||
update_forum_role(course_id, user, rolename, action) | ||
except Role.DoesNotExist: | ||
return HttpResponseBadRequest("Role does not exist.") | ||
|
||
response_payload = { | ||
'course_id': str(course_id), | ||
'action': action, | ||
} | ||
return JsonResponse(response_payload) | ||
|
||
|
||
@require_POST | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a shortcoming of the permissions.InstructorPermission class? that we can't check these two and the above
EDIT_FORUM_ROLES
permissions at the same time for a user? Could we adapt the permission class to check for multiple roles or use a different one so that all the permissions checks happen as DRF permission checks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@feanil in separate API which is related with forums I have re-write the functionality.
Related [PR]
(#35366). I will merge
35366
first and then I will rebase this later.has_instructor_access
means user must be instructor.has_forum_admin
means user be added asAdministrator
inDjango_Comment_Common > Roles
.