-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: upgrade bulk_beta_modify_access to drf ( 30 ) (#35604)
* feat!: upgrading api to DRF.
- Loading branch information
Showing
4 changed files
with
158 additions
and
75 deletions.
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 |
---|---|---|
|
@@ -1990,6 +1990,15 @@ def test_add_notenrolled_username_autoenroll(self): | |
self.add_notenrolled(response, self.notenrolled_student.username) | ||
assert CourseEnrollment.is_enrolled(self.notenrolled_student, self.course.id) | ||
|
||
def test_add_notenrolled_username_autoenroll_with_multiple_users(self): | ||
url = reverse('bulk_beta_modify_access', kwargs={'course_id': str(self.course.id)}) | ||
identifiers = (f"[email protected], " | ||
f"[email protected]\n[email protected]\r [email protected]\r, [email protected], " | ||
f"{self.notenrolled_student.username}" | ||
) | ||
response = self.client.post(url, {'identifiers': identifiers, 'action': 'add', 'email_students': False, 'auto_enroll': True}) # lint-amnesty, pylint: disable=line-too-long | ||
assert 6, len(json.loads(response.content.decode())['results']) | ||
|
||
@ddt.data('http', 'https') | ||
def test_add_notenrolled_with_email(self, protocol): | ||
url = reverse('bulk_beta_modify_access', kwargs={'course_id': str(self.course.id)}) | ||
|
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
""" Instructor apis serializers. """ | ||
import re | ||
|
||
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user | ||
from django.core.exceptions import ValidationError | ||
|
@@ -232,6 +233,76 @@ def __init__(self, *args, **kwargs): | |
self.fields['due_datetime'].required = False | ||
|
||
|
||
class ModifyAccessSerializer(serializers.Serializer): | ||
""" | ||
serializers for enroll or un-enroll users in beta testing program. | ||
""" | ||
identifiers = serializers.CharField( | ||
help_text="A comma separated list of emails or usernames.", | ||
required=True | ||
) | ||
action = serializers.ChoiceField( | ||
choices=["add", "remove"], | ||
help_text="Action to perform: add or remove.", | ||
required=True | ||
) | ||
|
||
email_students = serializers.BooleanField( | ||
default=False, | ||
help_text="Boolean flag to indicate if students should be emailed." | ||
) | ||
|
||
auto_enroll = serializers.BooleanField( | ||
default=False, | ||
help_text="Boolean flag to indicate if the user should be auto-enrolled." | ||
) | ||
|
||
def validate_identifiers(self, value): | ||
""" | ||
Validate the 'identifiers' field which is now a list of strings. | ||
""" | ||
# Iterate over the list of identifiers and validate each one | ||
validated_list = _split_input_list(value) | ||
if not validated_list: | ||
raise serializers.ValidationError("The identifiers list cannot be empty.") | ||
|
||
return validated_list | ||
|
||
def validate_email_students(self, value): | ||
""" | ||
handle string values like 'true' or 'false'. | ||
""" | ||
if isinstance(value, str): | ||
return value.lower() == 'true' | ||
return bool(value) | ||
|
||
def validate_auto_enroll(self, value): | ||
""" | ||
handle string values like 'true' or 'false'. | ||
""" | ||
if isinstance(value, str): | ||
return value.lower() == 'true' | ||
return bool(value) | ||
|
||
|
||
def _split_input_list(str_list): | ||
""" | ||
Separate out individual student email from the comma, or space separated string. | ||
e.g. | ||
in: "[email protected], [email protected]\n[email protected]\r [email protected]\r, [email protected]" | ||
out: ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'] | ||
`str_list` is a string coming from an input text area | ||
returns a list of separated values | ||
""" | ||
new_list = re.split(r'[,\s\n\r]+', str_list) | ||
new_list = [s.strip() for s in new_list] | ||
new_list = [s for s in new_list if s != ''] | ||
|
||
return new_list | ||
|
||
|
||
class CertificateStatusesSerializer(serializers.Serializer): | ||
""" | ||
Serializer for validating and serializing certificate status inputs. | ||
|