Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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_attributes.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:
pass
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
18 changes: 15 additions & 3 deletions edx_solutions_api_integration/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.core.files.base import ContentFile
from django.core.validators import validate_email, validate_slug
from django.db import IntegrityError
from django.db.models import BooleanField, Case, Count, Q, Value, When
from django.db.models import BooleanField, Case, Count, OuterRef, Subquery, Q, Value, When
from django.http import Http404
from django.utils.translation import get_language
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -48,6 +48,7 @@
get_user_from_request_params, str2bool)
from edx_solutions_organizations.models import (Organization,
OrganizationUsersAttributes)
from edx_solutions_organizations.utils import generate_key_for_main_user
from edx_solutions_organizations.serializers import BasicOrganizationSerializer
from edx_solutions_projects.serializers import BasicWorkgroupSerializer
from gradebook.models import StudentGradebook
Expand Down Expand Up @@ -1493,8 +1494,19 @@ def get_queryset(self):
user = get_user_from_request_params(self.request, self.kwargs)
if not user:
return []

return user.organizations.all()
user_organizations_set = user.organizations.all()
if len(user_organizations_set) > 1:
q_object = Q()
q_object.add(Q(user_id=user.id), Q.AND)
q_object.add(Q(organization_id=OuterRef('id')), Q.AND)
q_object.add(Q(is_main_company=True), Q.AND)
user_organizations_set = user_organizations_set.annotate(
is_main_company_flag=Case(
When(id__in=Subquery(OrganizationUsersAttributes.objects.filter(q_object).values('organization_id'))
, then=True), default=False, output_field=BooleanField()
)
).distinct().order_by('-is_main_company_flag')
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