From fb41b3b3d9b09a187fd8c493f58901dbc2ea6735 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Fri, 21 Feb 2020 00:41:19 +0000 Subject: [PATCH] Add a script to link old instructors with their new user --- .../management/commands/link_old_sessions.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 apiserver/apiserver/api/management/commands/link_old_sessions.py diff --git a/apiserver/apiserver/api/management/commands/link_old_sessions.py b/apiserver/apiserver/api/management/commands/link_old_sessions.py new file mode 100644 index 0000000..b7bc8c6 --- /dev/null +++ b/apiserver/apiserver/api/management/commands/link_old_sessions.py @@ -0,0 +1,56 @@ +from django.core.management.base import BaseCommand, CommandError +from django.contrib.auth.models import User +from apiserver.api import models, utils, utils_stats + +import time + +class Command(BaseCommand): + help = 'Link old sessions to instructors. Usage example: link_old_sessions "Tanner C" tanner.collin' + + def add_arguments(self, parser): + parser.add_argument('old_instructor', type=str) + parser.add_argument('username', type=str) + + def link_old_sessions(self, old_instructor, username): + sessions = models.Session.objects + old_sessions = sessions.filter(old_instructor=old_instructor) + + if not old_sessions.exists(): + self.stdout.write('Old instructor not found. Aborting.') + return 0 + + user = User.objects.filter(username=username) + + if not user.exists(): + self.stdout.write('Username not found. Aborting.') + return 0 + + user = user.first() + + for s in old_sessions: + s.instructor = user + s.save() + self.stdout.write('Linked ' + s.course.name) + + return old_sessions.count() + + def handle(self, *args, **options): + old_instructor = options['old_instructor'] + username = options['username'] + + self.stdout.write('Exact old instructor name: ' + old_instructor) + self.stdout.write('Exact Spaceport username: ' + username) + confirm = input('Is this correct? [y/N]: ') + + if confirm != 'y': + self.stdout.write('Aborting.') + return + + start = time.time() + + count = self.link_old_sessions(old_instructor, username) + self.stdout.write('Linked {} old sessions'.format(str(count))) + + self.stdout.write('Completed in {} s'.format( + str(time.time() - start)[:4] + ))