Skip to content

Commit

Permalink
temp: testing bulk create
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Aug 26, 2024
1 parent 241fe94 commit 04e4c2b
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions edx_exams/apps/core/management/commands/bulk_add_course_staff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time

from django.core.management.base import BaseCommand
from django.db import transaction

from edx_exams.apps.core.models import CourseStaffRole, User

Expand Down Expand Up @@ -56,43 +57,37 @@ def handle(self, *args, **options):

logger.info('Bulk add course staff complete!')

@transaction.atomic
def add_course_staff_from_csv(self, csv_file, batch_size, batch_delay):
"""
Add the given set of course staff provided in csv
"""
reader = list(csv.DictReader(csv_file))
users = {}

# bulk create users
for i in range(0, len(reader), batch_size):
User.objects.bulk_create(
bulk_users = User.objects.bulk_create(
(User(
username=row.get('username'),
email=row.get('email'),
) for row in reader[i:i + batch_size]),
ignore_conflicts=True,
)
users_to_add = {k: v for k, v in [(u.username, u) for u in bulk_users]}
logger.info('*** existing users ***', users_to_add)
users.update(users_to_add)

time.sleep(batch_delay)

# bulk create course staff
for i in range(0, len(reader), batch_size):
CourseStaffRole.objects.bulk_create(
(CourseStaffRole(
user=User.objects.get(username=row.get('username')),
user=users.get(row.get('username')),
course_id=row.get('course_id'),
role=row.get('role'),
) for row in reader[i:i + batch_size]),
ignore_conflicts=True,
)
time.sleep(batch_delay)

# bulk create course staff
# for i in range(0, len(reader), batch_size):
# CourseStaffRole.objects.bulk_create(
# (CourseStaffRole(
# user=User.objects.get(username=row.get('username')),
# course_id=row.get('course_id'),
# role=row.get('role'),
# ) for row in reader[i:i + batch_size]),
# ignore_conflicts=True,
# )
# time.sleep(batch_delay)

0 comments on commit 04e4c2b

Please sign in to comment.