Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

MEI-9862 return users main organizations in user and course api #332

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions edx_solutions_api_integration/courses/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,7 @@ def get_serializer_context(self):
"last_login",
"attributes",
"organization_groups",
"main_organization",
]

active_attributes = []
Expand Down
6 changes: 6 additions & 0 deletions edx_solutions_api_integration/imports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from edx_solutions_api_integration.permissions import SecureViewSet
from edx_solutions_api_integration.users.views import _manage_role
from edx_solutions_organizations.models import Organization
from edx_solutions_organizations.receivers import user_organization_updated
from lms.djangoapps.discussion.notification_prefs.views import enable_notifications
from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.course_groups.cohorts import (add_cohort,
Expand Down Expand Up @@ -186,6 +187,11 @@ def _create_user(self, data, errors, response):
except Exception as exc:
self._add_error(errors, str(exc.message), _('Enrolling Participant in Company'), email)

user_organization_updated.send(
sender=__name__, user_id=user.id,
organization_id=company.id
)

def _enroll_user(self, data, errors, response={}):
"""Enroll user in a course and add him to a cohort."""
user = data.get('user_object')
Expand Down
14 changes: 14 additions & 0 deletions edx_solutions_api_integration/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.exceptions import ObjectDoesNotExist
from edx_solutions_api_integration.models import APIUser
from edx_solutions_api_integration.utils import get_profile_image_urls_by_username
from edx_solutions_organizations.models import Organization
from edx_solutions_organizations.serializers import BasicOrganizationSerializer
from rest_framework import serializers

Expand Down Expand Up @@ -48,6 +49,18 @@ class UserSerializer(DynamicFieldsModelSerializer):
attributes = serializers.SerializerMethodField('get_organization_attributes')
course_groups = serializers.SerializerMethodField('get_user_course_groups')
organization_groups = serializers.SerializerMethodField('get_user_organization_groups')
main_organization = serializers.SerializerMethodField('get_user_main_organization')

def get_user_main_organization(self, user):
main_user_organization = None
mapped_user_organization = user.user_organizations.all().filter(is_main_company=True).first()
if mapped_user_organization:
try:
main_user_organization = Organization.objects.get(id=mapped_user_organization.organization_id)
except ObjectDoesNotExist:
main_user_organization = None
main_user_organization = [main_user_organization] if main_user_organization else []
return BasicOrganizationSerializer(main_user_organization, many=True, context=self.context).data

def get_user_organization_groups(self, user):
"""
Expand Down Expand Up @@ -164,6 +177,7 @@ class Meta:
"attributes",
"course_groups",
"organization_groups",
"main_organization",
)
read_only_fields = ("id", "email", "username")

Expand Down
14 changes: 12 additions & 2 deletions edx_solutions_api_integration/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1493,8 +1493,18 @@ def get_queryset(self):
user = get_user_from_request_params(self.request, self.kwargs)
if not user:
return []

return user.organizations.all()
main_user_organization = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 1469-1502 seems duplicate code(used in serializer as well).
can we remove this duplication as both are related with each other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

mapped_user_organization = user.user_organizations.all().filter(is_main_company=True).first()
if mapped_user_organization:
try:
main_user_organization = Organization.objects.get(id=mapped_user_organization.organization_id)
except ObjectDoesNotExist:
main_user_organization = None
user_organizations_set = user.organizations.all()
if main_user_organization:
user_organizations_set = user_organizations_set.exclude(id=main_user_organization.id)
user_organizations_set = [main_user_organization] + list(user_organizations_set)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are excluding main_user_organization from user_organizations_set and then re-adding it is there a reason?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.organizations.all().order_by('-user_organizations__is_main_company')

May be we can use some thing similar to this not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of re-adding was to set the main company at first index. However, updated it with the suggested one.

return user_organizations_set


class UsersWorkgroupsList(SecureListAPIView):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='api-integration',
version='5.0.5',
version='5.0.6',
description='RESTful api integration for edX platform',
long_description=open('README.rst').read(),
author='edX',
Expand Down