-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to bulk update course verticals
- Loading branch information
Showing
9 changed files
with
206 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from django.core.mail import EmailMessage | ||
from django.template.loader import get_template | ||
|
||
def send_email_for_course_verticals_update(report, to_users): | ||
""" | ||
Send an overall report of an update_course_verticals mgmt command run | ||
""" | ||
success_count = len(report['successes']) | ||
failure_count = len(report['failures']) | ||
context = { | ||
'total_count': success_count + failure_count, | ||
'failure_count': failure_count, | ||
'success_count': success_count, | ||
'failures': report['failures'] | ||
} | ||
html_template = 'course_metadata/email/update_course_verticals.html' | ||
template = get_template(html_template) | ||
html_content = template.render(context) | ||
|
||
email = EmailMessage( | ||
"Update Course Verticals Command Summary", | ||
html_content, | ||
settings.PUBLISHER_FROM_EMAIL, | ||
to_users, | ||
) | ||
email.content_subtype = "html" | ||
email.send() | ||
Empty file.
Empty file.
81 changes: 81 additions & 0 deletions
81
course_discovery/apps/tagging/management/commands/update_course_verticals.py
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
Management command for updating course verticals and subverticals | ||
Example usage: | ||
$ ./manage.py update_course_verticals | ||
""" | ||
import logging | ||
import unicodecsv | ||
from django.conf import settings | ||
from django.core.management import BaseCommand, CommandError | ||
|
||
from course_discovery.apps.tagging.emails import send_email_for_course_verticals_update | ||
from course_discovery.apps.course_metadata.models import Course | ||
from course_discovery.apps.tagging.models import CourseVertical, SubVertical, UpdateCourseVerticalsConfig, Vertical | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Update course verticals and subverticals" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.report = { | ||
'failures': [], | ||
'successes': [], | ||
} | ||
|
||
def handle(self, *args, **options): | ||
reader = self.get_csv_reader() | ||
|
||
for row in reader: | ||
try: | ||
course_key = row.get('course') | ||
self.process_vertical_information(row) | ||
except Exception as exc: | ||
self.report['failures'].append( | ||
{ | ||
'id': course_key, | ||
'reason': repr(exc) | ||
} | ||
) | ||
logger.exception(f"Failed to set vertical/subvertical information for course with key {course_key}") | ||
else: | ||
self.report['successes'].append( | ||
{ | ||
'id': course_key, | ||
} | ||
) | ||
logger.info(f"Successfully set vertical and subvertical info for course with key {course_key}") | ||
|
||
send_email_for_course_verticals_update(self.report, settings.COURSE_VERTICALS_UPDATE_RECIPIENTS) | ||
|
||
def process_vertical_information(self, row): | ||
course_key, vertical_name, subvertical_name = row['course'], row['vertical'], row['subvertical'] | ||
course = Course.objects.get(key=course_key) | ||
vertical = Vertical.objects.filter(name=vertical_name).first() | ||
subvertical = SubVertical.objects.filter(name=subvertical_name).first() | ||
if (not vertical and vertical_name) or (not subvertical and subvertical_name): | ||
raise ValueError("Incorrect vertical or subvertical provided") | ||
|
||
course_vertical = CourseVertical.objects.filter(course=course).first() | ||
if course_vertical: | ||
logger.info(f"Changing existing vertical association for course with key {course.key}") | ||
course_vertical.vertical = vertical | ||
course_vertical.subvertical = subvertical | ||
course_vertical.save() | ||
else: | ||
CourseVertical.objects.create(course=course, vertical=vertical, subvertical=subvertical) | ||
|
||
def get_csv_reader(self): | ||
config = UpdateCourseVerticalsConfig.current() | ||
if not config.enabled: | ||
raise CommandError('Configuration object is not enabled') | ||
|
||
if not config.csv_file: | ||
raise CommandError('Configuration object does not have any input csv') | ||
|
||
reader = unicodecsv.DictReader(config.csv_file) | ||
return reader | ||
31 changes: 31 additions & 0 deletions
31
course_discovery/apps/tagging/migrations/0002_updatecourseverticalsconfig.py
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 4.2.17 on 2025-01-21 12:05 | ||
|
||
from django.conf import settings | ||
import django.core.validators | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('tagging', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='UpdateCourseVerticalsConfig', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('change_date', models.DateTimeField(auto_now_add=True, verbose_name='Change date')), | ||
('enabled', models.BooleanField(default=False, verbose_name='Enabled')), | ||
('csv_file', models.FileField(help_text='A csv file containing the course keys, verticals and subverticals', upload_to='', validators=[django.core.validators.FileExtensionValidator(allowed_extensions=['csv'])])), | ||
('changed_by', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL, verbose_name='Changed by')), | ||
], | ||
options={ | ||
'ordering': ('-change_date',), | ||
'abstract': False, | ||
}, | ||
), | ||
] |
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
42 changes: 42 additions & 0 deletions
42
course_discovery/apps/tagging/templates/email/update_course_verticals.html
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{% extends "course_metadata/email/email_base.html" %} | ||
{% load django_markup %} | ||
{% block body %} | ||
|
||
<p> | ||
The course verticals update process has completed. A summary is presented below. | ||
</p> | ||
<div> | ||
<table border="2" width="50%" style="padding: 5px;"> | ||
<tr> | ||
<th colspan="2"> | ||
Course Verticals Update Summary | ||
</th> | ||
</tr> | ||
<tr> | ||
<th>Total Data Rows</th> | ||
<td>{{ total_count }}</td> | ||
</tr> | ||
<tr> | ||
<th>Successfully Archived</th> | ||
<td>{{ success_count }}</td> | ||
</tr> | ||
<tr> | ||
<th>Failures</th> | ||
<td>{{ failure_count }}</td> | ||
</tr> | ||
</table> | ||
</div> | ||
|
||
|
||
{% if failure_count > 0 %} | ||
<div> | ||
<h3>Verticals Update Failures</h3> | ||
<ul> | ||
{% for failure in failures %} | ||
<li>[{{failure.id}}]: {{failure.reason}}</li> | ||
{% endfor %} | ||
</ul> | ||
</div> | ||
{% endif %} | ||
|
||
{% endblock body %} |
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 |
---|---|---|
|
@@ -804,3 +804,5 @@ | |
|
||
# The list of user groups that have access to assign verticals and sub-verticals to courses | ||
VERTICALS_MANAGEMENT_GROUPS = [] | ||
|
||
COURSE_VERTICALS_UPDATE_RECIPIENTS = ['[email protected]'] |