diff --git a/lms/djangoapps/teams/api.py b/lms/djangoapps/teams/api.py index 362eea60d8..c30b17b559 100644 --- a/lms/djangoapps/teams/api.py +++ b/lms/djangoapps/teams/api.py @@ -218,7 +218,7 @@ def teamset_is_public_or_user_is_on_team_in_teamset(user, course_module, teamset teamset = course_module.teams_configuration.teamsets_by_id[teamset_id] if teamset.teamset_type != TeamsetType.private_managed: return True - return CourseTeamMembership.user_in_team_for_course(user, course_module.id, topic_id=teamset_id) + return CourseTeamMembership.user_in_team_for_teamset(user, course_module.id, topic_id=teamset_id) def user_on_team_or_team_is_public(user, team): diff --git a/lms/djangoapps/teams/errors.py b/lms/djangoapps/teams/errors.py index dc9e735760..acf7df915f 100644 --- a/lms/djangoapps/teams/errors.py +++ b/lms/djangoapps/teams/errors.py @@ -13,8 +13,8 @@ class NotEnrolledInCourseForTeam(TeamAPIRequestError): pass -class AlreadyOnTeamInCourse(TeamAPIRequestError): - """User is already a member of another team in the same course.""" +class AlreadyOnTeamInTeamset(TeamAPIRequestError): + """User is already a member of another team in the same teamset.""" pass diff --git a/lms/djangoapps/teams/models.py b/lms/djangoapps/teams/models.py index f8e6b7642b..cd50878222 100644 --- a/lms/djangoapps/teams/models.py +++ b/lms/djangoapps/teams/models.py @@ -36,7 +36,7 @@ from openedx.core.djangoapps.django_comment_common.signals import ( from student.models import CourseEnrollment, LanguageField from .errors import ( - AlreadyOnTeamInCourse, + AlreadyOnTeamInTeamset, ImmutableMembershipFieldException, NotEnrolledInCourseForTeam, AddToIncompatibleTeamError @@ -209,8 +209,8 @@ class CourseTeam(models.Model): if not CourseEnrollment.is_enrolled(user, self.course_id): raise NotEnrolledInCourseForTeam - if CourseTeamMembership.user_in_team_for_course(user, self.course_id, self.topic_id): - raise AlreadyOnTeamInCourse + if CourseTeamMembership.user_in_team_for_teamset(user, self.course_id, self.topic_id): + raise AlreadyOnTeamInTeamset if not user_protection_status_matches_team(user, self): raise AddToIncompatibleTeamError return CourseTeamMembership.objects.create( @@ -319,10 +319,11 @@ class CourseTeamMembership(models.Model): return queryset @classmethod - def user_in_team_for_course(cls, user, course_id, topic_id=None): + def user_in_team_for_teamset(cls, user, course_id, topic_id=None): """ Checks user membership in two ways: - if topic_id is None, checks to see if a user is assigned to any team in the course + if topic_id is None, checks to see if a user is assigned to any team in the teamsets associated with this + course. if topic_id (teamset) is provided, checks to see if a user is assigned to a specific team in the course. Args: @@ -334,10 +335,7 @@ class CourseTeamMembership(models.Model): True if the user is on a team in the course already False if not """ - if topic_id is None: - return cls.objects.filter(user=user, team__course_id=course_id).exists() - else: - return cls.objects.filter(user=user, team__course_id=course_id, team__topic_id=topic_id).exists() + return cls.objects.filter(user=user, team__course_id=course_id, team__topic_id=topic_id).exists() @classmethod def update_last_activity(cls, user, discussion_topic_id): diff --git a/lms/djangoapps/teams/tests/test_models.py b/lms/djangoapps/teams/tests/test_models.py index f2774ce08d..9c2ff840a6 100644 --- a/lms/djangoapps/teams/tests/test_models.py +++ b/lms/djangoapps/teams/tests/test_models.py @@ -238,7 +238,7 @@ class TeamMembershipTest(SharedModuleStoreTestCase): def test_user_in_team_for_course(self, username, course_id, expected_value): user = getattr(self, username) self.assertEqual( - CourseTeamMembership.user_in_team_for_course(user, course_id), + CourseTeamMembership.user_in_team_for_teamset(user, course_id), expected_value ) @@ -254,7 +254,7 @@ class TeamMembershipTest(SharedModuleStoreTestCase): def test_user_in_team_for_course_teamset(self, username, course_id, teamset_id, expected_value): user = getattr(self, username) self.assertEqual( - CourseTeamMembership.user_in_team_for_course(user, course_id, teamset_id), + CourseTeamMembership.user_in_team_for_teamset(user, course_id, teamset_id), expected_value ) diff --git a/lms/djangoapps/teams/views.py b/lms/djangoapps/teams/views.py index bd1dc26478..adf5bbba88 100644 --- a/lms/djangoapps/teams/views.py +++ b/lms/djangoapps/teams/views.py @@ -59,7 +59,7 @@ from .api import ( user_organization_protection_status ) from .csv import load_team_membership_csv, TeamMembershipImportManager -from .errors import AlreadyOnTeamInCourse, ElasticSearchConnectionError, NotEnrolledInCourseForTeam +from .errors import AlreadyOnTeamInTeamset, ElasticSearchConnectionError, NotEnrolledInCourseForTeam from .search_indexes import CourseTeamIndexer from .serializers import ( BulkTeamCountTopicSerializer, @@ -586,7 +586,7 @@ class TeamsListView(ExpandableFieldViewMixin, GenericAPIView): is_team_administrator = (has_access(request.user, 'staff', course_key) or has_discussion_privileges(request.user, course_key)) if not is_team_administrator and ( - CourseTeamMembership.user_in_team_for_course(request.user, course_key, topic_id=topic_id) + CourseTeamMembership.user_in_team_for_teamset(request.user, course_key, topic_id=topic_id) ): error_message = build_api_error( ugettext_noop('You are already in a team in this teamset.'), @@ -1164,12 +1164,11 @@ class TopicDetailView(APIView): ) return Response(serializer.data) - class MembershipListView(ExpandableFieldViewMixin, GenericAPIView): """ **Use Cases** - List course team memberships or add a user to a course team. + List teamset team memberships or add a user to a teamset. **Example Requests**: @@ -1183,12 +1182,12 @@ class MembershipListView(ExpandableFieldViewMixin, GenericAPIView): * username: Returns membership records only for the specified user. If the requesting user is not staff then only memberships for - teams associated with courses in which the requesting user is + teams associated with teamsets in which the requesting user is enrolled are returned. * team_id: Returns only membership records associated with the - specified team. The requesting user must be staff or enrolled in - the course associated with the team. + specified team. The requesting user must be staff or a member of + the teamset associated with the team. * teamset_id: Returns membership records only for the specified teamset. if teamset_id is specified, course_id must also be specified. @@ -1461,10 +1460,10 @@ class MembershipListView(ExpandableFieldViewMixin, GenericAPIView): 'add_method': 'joined_from_team_view' if user == request.user else 'added_by_another_user' } ) - except AlreadyOnTeamInCourse: + except AlreadyOnTeamInTeamset: return Response( build_api_error( - ugettext_noop(u"The user {username} is already a member of a team in this course."), + ugettext_noop(u"The user {username} is already a member of a team in this teamset."), username=username ), status=status.HTTP_400_BAD_REQUEST