Commit 21832bb9 by arbabnazar

modify the whitelist cert script to take list of users

parent cd60dd56
...@@ -12,6 +12,18 @@ from certificates.models import CertificateWhitelist ...@@ -12,6 +12,18 @@ from certificates.models import CertificateWhitelist
from django.contrib.auth.models import User from django.contrib.auth.models import User
def get_user_from_identifier(identifier):
"""
This function takes the string identifier and fetch relevant user object from database
"""
identifier = identifier.strip()
if '@' in identifier:
user = User.objects.get(email=identifier)
else:
user = User.objects.get(username=identifier)
return user
class Command(BaseCommand): class Command(BaseCommand):
help = """ help = """
...@@ -57,6 +69,17 @@ class Command(BaseCommand): ...@@ -57,6 +69,17 @@ class Command(BaseCommand):
if not course_id: if not course_id:
raise CommandError("You must specify a course-id") raise CommandError("You must specify a course-id")
def update_user_whitelist(username, add=True):
"""
Update the status of whitelist user(s)
"""
user = get_user_from_identifier(username)
cert_whitelist, _created = CertificateWhitelist.objects.get_or_create(
user=user, course_id=course
)
cert_whitelist.whitelist = add
cert_whitelist.save()
# try to parse the serialized course key into a CourseKey # try to parse the serialized course key into a CourseKey
try: try:
course = CourseKey.from_string(course_id) course = CourseKey.from_string(course_id)
...@@ -69,19 +92,11 @@ class Command(BaseCommand): ...@@ -69,19 +92,11 @@ class Command(BaseCommand):
if options['add'] or options['del']: if options['add'] or options['del']:
user_str = options['add'] or options['del'] user_str = options['add'] or options['del']
if '@' in user_str: add_to_whitelist = True if options['add'] else False
user = User.objects.get(email=user_str) users_list = user_str.split(",")
else: for username in users_list:
user = User.objects.get(username=user_str) if username.strip():
update_user_whitelist(username, add=add_to_whitelist)
cert_whitelist, _created = \
CertificateWhitelist.objects.get_or_create(
user=user, course_id=course)
if options['add']:
cert_whitelist.whitelist = True
elif options['del']:
cert_whitelist.whitelist = False
cert_whitelist.save()
whitelist = CertificateWhitelist.objects.filter(course_id=course) whitelist = CertificateWhitelist.objects.filter(course_id=course)
wl_users = '\n'.join( wl_users = '\n'.join(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment