Commit f37ffc2f by Diana Huang

Add the course as part of the context for teams events.

TNL-3248
parent 17e64535
......@@ -803,7 +803,6 @@ class BrowseTeamsWithinTopicTest(TeamsTabBase):
events = [{
'event_type': 'edx.team.searched',
'event': {
'course_id': self.course_id,
'search_text': search_text,
'topic_id': self.topic['id'],
'number_of_results': 0
......@@ -1043,15 +1042,11 @@ class CreateTeamTest(TeamFormActions):
expected_events = [
{
'event_type': 'edx.team.created',
'event': {
'course_id': self.course_id,
}
'event_type': 'edx.team.created'
},
{
'event_type': 'edx.team.learner_added',
'event': {
'course_id': self.course_id,
'add_method': 'added_on_create',
}
}
......@@ -1209,14 +1204,12 @@ class DeleteTeamTest(TeamFormActions):
{
'event_type': 'edx.team.deleted',
'event': {
'course_id': self.course_id,
'team_id': self.team['id']
}
},
{
'event_type': 'edx.team.learner_removed',
'event': {
'course_id': self.course_id,
'team_id': self.team['id'],
'remove_method': 'team_deleted',
'user_id': self.user_info['user_id']
......@@ -1302,7 +1295,6 @@ class EditTeamTest(TeamFormActions):
{
'event_type': 'edx.team.changed',
'event': {
'course_id': self.course_id,
'team_id': self.team['id'],
'field': 'country',
'old': 'AF',
......@@ -1313,7 +1305,6 @@ class EditTeamTest(TeamFormActions):
{
'event_type': 'edx.team.changed',
'event': {
'course_id': self.course_id,
'team_id': self.team['id'],
'field': 'name',
'old': self.team['name'],
......@@ -1324,7 +1315,6 @@ class EditTeamTest(TeamFormActions):
{
'event_type': 'edx.team.changed',
'event': {
'course_id': self.course_id,
'team_id': self.team['id'],
'field': 'language',
'old': 'aa',
......@@ -1335,7 +1325,6 @@ class EditTeamTest(TeamFormActions):
{
'event_type': 'edx.team.changed',
'event': {
'course_id': self.course_id,
'team_id': self.team['id'],
'field': 'description',
'old': self.team['description'],
......@@ -1515,7 +1504,6 @@ class EditMembershipTest(TeamFormActions):
{
'event_type': 'edx.team.learner_removed',
'event': {
'course_id': self.course_id,
'team_id': self.team['id'],
'remove_method': 'removed_by_admin',
'user_id': self.user_info['user_id']
......@@ -1801,7 +1789,6 @@ class TeamPageTest(TeamsTabBase):
{
'event_type': 'edx.team.learner_added',
'event': {
'course_id': self.course_id,
'add_method': 'joined_from_team_view'
}
}
......@@ -1880,7 +1867,6 @@ class TeamPageTest(TeamsTabBase):
{
'event_type': 'edx.team.learner_removed',
'event': {
'course_id': self.course_id,
'remove_method': 'self_removal'
}
}
......
......@@ -548,7 +548,6 @@ class TestListTeamsAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.searched',
course_id=unicode(self.test_course_2.id),
search_text=text_search,
topic_id=None,
number_of_results=len(expected_team_names)
......@@ -707,13 +706,11 @@ class TestCreateTeamAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.created',
team_id=self._expected_team_id(team, 'fully-specified-team'),
course_id=unicode(self.test_course_1.id),
)
self.assert_event_emitted(
'edx.team.learner_added',
team_id=self._expected_team_id(team, 'fully-specified-team'),
course_id=unicode(self.test_course_1.id),
user_id=self.users[creator].id,
add_method='added_on_create'
)
......@@ -821,12 +818,10 @@ class TestDeleteTeamAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.deleted',
team_id=self.solar_team.team_id,
course_id=unicode(self.test_course_1.id)
)
self.assert_event_emitted(
'edx.team.learner_removed',
team_id=self.solar_team.team_id,
course_id=unicode(self.test_course_1.id),
remove_method='team_deleted',
user_id=self.users['student_enrolled'].id
)
......@@ -840,12 +835,10 @@ class TestDeleteTeamAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.deleted',
team_id=self.solar_team.team_id,
course_id=unicode(self.test_course_1.id)
)
self.assert_event_emitted(
'edx.team.learner_removed',
team_id=self.solar_team.team_id,
course_id=unicode(self.test_course_1.id),
remove_method='team_deleted',
user_id=self.users['student_enrolled'].id
)
......@@ -877,7 +870,6 @@ class TestUpdateTeamAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.changed',
team_id=self.solar_team.team_id,
course_id=unicode(self.solar_team.course_id),
truncated=[],
field='name',
old=prev_name,
......@@ -919,7 +911,6 @@ class TestUpdateTeamAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.changed',
team_id=self.solar_team.team_id,
course_id=unicode(self.solar_team.course_id),
truncated=[],
field=key,
old=prev_value,
......@@ -1220,7 +1211,6 @@ class TestCreateMembershipAPI(EventTestMixin, TeamAPITestCase):
'edx.team.learner_added',
team_id=self.solar_team.team_id,
user_id=self.users['student_enrolled_not_on_team'].id,
course_id=unicode(self.solar_team.course_id),
add_method=add_method
)
else:
......@@ -1378,7 +1368,6 @@ class TestDeleteMembershipAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.learner_removed',
team_id=self.solar_team.team_id,
course_id=unicode(self.solar_team.course_id),
user_id=self.users['student_enrolled'].id,
remove_method='removed_by_admin'
)
......@@ -1396,7 +1385,6 @@ class TestDeleteMembershipAPI(EventTestMixin, TeamAPITestCase):
self.assert_event_emitted(
'edx.team.learner_removed',
team_id=self.solar_team.team_id,
course_id=unicode(self.solar_team.course_id),
user_id=self.users['student_enrolled'].id,
remove_method='self_removal'
)
......
......@@ -40,6 +40,7 @@ from opaque_keys.edx.keys import CourseKey
from courseware.courses import get_course_with_access, has_access
from eventtracking import tracker
from track import contexts
from student.models import CourseEnrollment, CourseAccessRole
from student.roles import CourseStaffRole
from django_comment_client.utils import has_discussion_privileges
......@@ -66,6 +67,16 @@ MAXIMUM_SEARCH_SIZE = 100000
log = logging.getLogger(__name__)
def emit_team_event(event_name, course_key, event_data):
"""
Emit team events with the correct course id context.
"""
context = contexts.course_context_from_course_id(course_key)
with tracker.get_tracker().context(event_name, context):
tracker.emit(event_name, event_data)
@receiver(post_save, sender=CourseTeam)
def team_post_save_callback(sender, instance, **kwargs): # pylint: disable=unused-argument
""" Emits signal after the team is saved. """
......@@ -76,11 +87,11 @@ def team_post_save_callback(sender, instance, **kwargs): # pylint: disable=unus
if field not in instance.FIELD_BLACKLIST:
truncated_fields = truncate_fields(unicode(changed_fields[field]), unicode(getattr(instance, field)))
truncated_fields['team_id'] = instance.team_id
truncated_fields['course_id'] = unicode(instance.course_id)
truncated_fields['field'] = field
tracker.emit(
emit_team_event(
'edx.team.changed',
instance.course_id,
truncated_fields
)
......@@ -377,11 +388,10 @@ class TeamsListView(ExpandableFieldViewMixin, GenericAPIView):
self.get_page()
)
serializer = self.get_pagination_serializer(paginated_results)
tracker.emit('edx.team.searched', {
emit_team_event('edx.team.searched', course_key, {
"number_of_results": search_results['total'],
"search_text": text_search,
"topic_id": topic_id,
"course_id": course_id_string,
})
else:
queryset = CourseTeam.objects.filter(**result_filter)
......@@ -455,19 +465,18 @@ class TeamsListView(ExpandableFieldViewMixin, GenericAPIView):
}, status=status.HTTP_400_BAD_REQUEST)
else:
team = serializer.save()
tracker.emit('edx.team.created', {
'team_id': team.team_id,
'course_id': unicode(course_id)
emit_team_event('edx.team.created', course_key, {
'team_id': team.team_id
})
if not team_administrator:
# Add the creating user to the team.
team.add_user(request.user)
tracker.emit(
emit_team_event(
'edx.team.learner_added',
course_key,
{
'team_id': team.team_id,
'user_id': request.user.id,
'course_id': unicode(team.course_id),
'add_method': 'added_on_create'
}
)
......@@ -618,14 +627,12 @@ class TeamsDetailView(ExpandableFieldViewMixin, RetrievePatchAPIView):
# Note: also deletes all team memberships associated with this team
team.delete()
log.info('user %d deleted team %s', request.user.id, team_id)
tracker.emit('edx.team.deleted', {
emit_team_event('edx.team.deleted', team.course_id, {
'team_id': team_id,
'course_id': unicode(team.course_id),
})
for member in memberships:
tracker.emit('edx.team.learner_removed', {
emit_team_event('edx.team.learner_removed', team.course_id, {
'team_id': team_id,
'course_id': unicode(team.course_id),
'remove_method': 'team_deleted',
'user_id': member.user_id
})
......@@ -1064,12 +1071,12 @@ class MembershipListView(ExpandableFieldViewMixin, GenericAPIView):
try:
membership = team.add_user(user)
tracker.emit(
emit_team_event(
'edx.team.learner_added',
team.course_id,
{
'team_id': team.team_id,
'user_id': user.id,
'course_id': unicode(team.course_id),
'add_method': 'joined_from_team_view' if user == request.user else 'added_by_another_user'
}
)
......@@ -1202,11 +1209,11 @@ class MembershipDetailView(ExpandableFieldViewMixin, GenericAPIView):
if 'admin' in request.QUERY_PARAMS:
removal_method = 'removed_by_admin'
membership.delete()
tracker.emit(
emit_team_event(
'edx.team.learner_removed',
team.course_id,
{
'team_id': team.team_id,
'course_id': unicode(team.course_id),
'user_id': membership.user.id,
'remove_method': removal_method
}
......
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