Commit 5139b570 by Clinton Blackburn Committed by Clinton Blackburn

Removed duplicated override of update_team method

LEARNER-2469
parent 0b1d22f0
...@@ -3,12 +3,12 @@ from django.contrib.auth import get_user_model ...@@ -3,12 +3,12 @@ from django.contrib.auth import get_user_model
from django.db import transaction from django.db import transaction
from rest_framework import serializers from rest_framework import serializers
from rest_framework.fields import empty from rest_framework.fields import empty
from xmodule.modulestore.django import modulestore
from cms.djangoapps.contentstore.views.course import create_new_course, get_course_and_check_access, rerun_course from cms.djangoapps.contentstore.views.course import create_new_course, get_course_and_check_access, rerun_course
from contentstore.views.assets import update_course_run_asset from contentstore.views.assets import update_course_run_asset
from openedx.core.lib.courses import course_image_url from openedx.core.lib.courses import course_image_url
from student.models import CourseAccessRole from student.models import CourseAccessRole
from xmodule.modulestore.django import modulestore
IMAGE_TYPES = { IMAGE_TYPES = {
'image/jpeg': 'jpg', 'image/jpeg': 'jpg',
...@@ -50,13 +50,19 @@ class CourseRunTeamSerializerMixin(serializers.Serializer): ...@@ -50,13 +50,19 @@ class CourseRunTeamSerializerMixin(serializers.Serializer):
team = CourseRunTeamSerializer(required=False) team = CourseRunTeamSerializer(required=False)
def update_team(self, instance, team): def update_team(self, instance, team):
CourseAccessRole.objects.filter(course_id=instance.id).delete() # Existing data should remain intact when performing a partial update.
if not self.partial:
CourseAccessRole.objects.filter(course_id=instance.id).delete()
# TODO In the future we can optimize by getting users in a single query. # We iterate here, instead of using a bulk operation, to avoid uniqueness errors that arise
CourseAccessRole.objects.bulk_create([ # when using `bulk_create` with existing data. Given the relatively small number of team members
CourseAccessRole(course_id=instance.id, role=member['role'], user=User.objects.get(username=member['user'])) # in a course, this is not worth optimizing at this time.
for member in team for member in team:
]) CourseAccessRole.objects.update_or_create(
course_id=instance.id,
user=User.objects.get(username=member['user']),
defaults={'role': member['role']}
)
def image_is_jpeg_or_png(value): def image_is_jpeg_or_png(value):
...@@ -111,21 +117,6 @@ class CourseRunSerializer(CourseRunTeamSerializerMixin, serializers.Serializer): ...@@ -111,21 +117,6 @@ class CourseRunSerializer(CourseRunTeamSerializerMixin, serializers.Serializer):
modulestore().update_item(instance, self.context['request'].user.id) modulestore().update_item(instance, self.context['request'].user.id)
return instance return instance
def update_team(self, instance, team):
# Existing data should remain intact when performing a partial update.
if not self.partial:
CourseAccessRole.objects.filter(course_id=instance.id).delete()
# We iterate here, instead of using a bulk operation, to avoid uniqueness errors that arise
# when using `bulk_create` with existing data. Given the relatively small number of team members
# in a course, this is not worth optimizing at this time.
for member in team:
CourseAccessRole.objects.update_or_create(
course_id=instance.id,
user=User.objects.get(username=member['user']),
defaults={'role': member['role']}
)
class CourseRunCreateSerializer(CourseRunSerializer): class CourseRunCreateSerializer(CourseRunSerializer):
org = serializers.CharField(source='id.org') org = serializers.CharField(source='id.org')
......
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