feat: allow notify_credentials to take a list of usernames

This commit is contained in:
oliviaruizknott
2021-02-09 17:04:21 -05:00
parent ac7a9f405b
commit 0533ecc814
3 changed files with 18 additions and 17 deletions

View File

@@ -157,7 +157,7 @@ def get_certificates_for_user_by_course_keys(user, course_keys):
}
def get_recently_modified_certificates(course_keys=None, start_date=None, end_date=None, username=None):
def get_recently_modified_certificates(course_keys=None, start_date=None, end_date=None, usernames=None):
"""
Returns a QuerySet of GeneratedCertificate objects filtered by the input
parameters and ordered by modified_date.
@@ -173,8 +173,8 @@ def get_recently_modified_certificates(course_keys=None, start_date=None, end_da
if end_date:
cert_filter_args['modified_date__lte'] = end_date
if username:
cert_filter_args['user__username'] = username
if usernames:
cert_filter_args['user__username__in'] = usernames
return GeneratedCertificate.objects.filter(**cert_filter_args).order_by('modified_date')

View File

@@ -35,7 +35,7 @@ def clear_prefetched_course_and_subsection_grades(course_key):
_PersistentCourseGrade.clear_prefetched_data(course_key)
def get_recently_modified_grades(course_keys, start_date, end_date, user=None):
def get_recently_modified_grades(course_keys, start_date, end_date, users=None):
"""
Returns a QuerySet of PersistentCourseGrade objects filtered by the input
parameters and ordered by modified date.
@@ -47,8 +47,8 @@ def get_recently_modified_grades(course_keys, start_date, end_date, user=None):
grade_filter_args['modified__gte'] = start_date
if end_date:
grade_filter_args['modified__lte'] = end_date
if user:
grade_filter_args['user_id'] = user.id
if users:
grade_filter_args['user_id__in'] = [u.id for u in users]
return _PersistentCourseGrade.objects.filter(**grade_filter_args).order_by('modified')

View File

@@ -162,9 +162,10 @@ class Command(BaseCommand):
help='Send program award notifications with course notification tasks',
)
parser.add_argument(
'--username',
'--usernames',
default=None,
help='Run the command for a single user',
nargs='+',
help='Run the command for the given user or list of users',
)
def get_args_from_database(self):
@@ -189,7 +190,7 @@ class Command(BaseCommand):
log.info(
u"notify_credentials starting, dry-run=%s, site=%s, delay=%d seconds, page_size=%d, "
u"from=%s, to=%s, notify_programs=%s, username=%s, execution=%s",
u"from=%s, to=%s, notify_programs=%s, usernames=%s, execution=%s",
options['dry_run'],
options['site'],
options['delay'],
@@ -197,7 +198,7 @@ class Command(BaseCommand):
options['start_date'] if options['start_date'] else 'NA',
options['end_date'] if options['end_date'] else 'NA',
options['notify_programs'],
options['username'],
options['usernames'],
'auto' if options['auto'] else 'manual',
)
@@ -207,18 +208,18 @@ class Command(BaseCommand):
log.error(u'No site configuration found for site %s', options['site'])
course_keys = self.get_course_keys(options['courses'])
if not (course_keys or options['start_date'] or options['end_date'] or options['username']):
raise CommandError('You must specify a filter (e.g. --courses= or --start-date or --username)')
if not (course_keys or options['start_date'] or options['end_date'] or options['usernames']):
raise CommandError('You must specify a filter (e.g. --courses= or --start-date or --usernames)')
certs = get_recently_modified_certificates(
course_keys, options['start_date'], options['end_date'], options['username']
course_keys, options['start_date'], options['end_date'], options['usernames']
)
user = None
if options['username']:
user = User.objects.get(username=options['username'])
users = None
if options['usernames']:
users = User.objects.filter(username__in=options['usernames'])
grades = get_recently_modified_grades(
course_keys, options['start_date'], options['end_date'], user
course_keys, options['start_date'], options['end_date'], users
)
log.info('notify_credentials Sending notifications for {certs} certificates and {grades} grades'.format(