-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ENT-7553 Added Academies api for MFEs
- Loading branch information
IrfanUddinAhmad
committed
Nov 18, 2023
1 parent
def14ed
commit 7065254
Showing
5 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from uuid import uuid4 | ||
|
||
import factory | ||
from factory.fuzzy import FuzzyText | ||
|
||
from enterprise_catalog.apps.academy.models import Academy, Tag | ||
from enterprise_catalog.apps.catalog.tests.factories import ( | ||
EnterpriseCatalogFactory, | ||
) | ||
|
||
|
||
class TagFactory(factory.django.DjangoModelFactory): | ||
""" | ||
Test factory for the `Tag` model | ||
""" | ||
class Meta: | ||
model = Tag | ||
|
||
|
||
class AcademyFactory(factory.django.DjangoModelFactory): | ||
""" | ||
Test factory for the `Academy` model | ||
""" | ||
class Meta: | ||
model = Academy | ||
|
||
uuid = factory.LazyFunction(uuid4) | ||
title = FuzzyText(length=32) | ||
short_description = FuzzyText(length=32) | ||
long_description = FuzzyText(length=255) | ||
|
||
@factory.post_generation | ||
def enterprise_catalogs(self, create, extracted, **kwargs): | ||
if not create: | ||
# Simple build, do nothing. | ||
return | ||
|
||
if extracted: | ||
for enterprise_catalog in extracted: | ||
self.enterprise_catalogs.add(enterprise_catalog) # pylint: disable=no-member | ||
else: | ||
enterprise_catalog1 = EnterpriseCatalogFactory() | ||
enterprise_catalog2 = EnterpriseCatalogFactory() | ||
self.enterprise_catalogs.set([enterprise_catalog1, enterprise_catalog2]) # pylint: disable=no-member | ||
|
||
@factory.post_generation | ||
def tags(self, create, extracted, **kwargs): | ||
if not create: | ||
# Simple build, do nothing. | ||
return | ||
|
||
if extracted: | ||
for tag in extracted: | ||
self.tags.add(tag) # pylint: disable=no-member | ||
else: | ||
tag1 = TagFactory() | ||
tag2 = TagFactory() | ||
self.tags.set([tag1, tag2]) # pylint: disable=no-member |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.utils.functional import cached_property | ||
from edx_rest_framework_extensions.auth.jwt.authentication import ( | ||
JwtAuthentication, | ||
) | ||
from rest_framework import permissions, viewsets | ||
from rest_framework.authentication import SessionAuthentication | ||
from rest_framework.renderers import JSONRenderer | ||
from rest_framework_xml.renderers import XMLRenderer | ||
|
||
from enterprise_catalog.apps.academy.models import Academy | ||
from enterprise_catalog.apps.api.v1.serializers import AcademySerializer | ||
|
||
|
||
class AcademiesReadOnlyViewSet(viewsets.ReadOnlyModelViewSet): | ||
""" Viewset for Read Only operations on Academies """ | ||
authentication_classes = [JwtAuthentication, SessionAuthentication] | ||
permission_classes = [permissions.IsAuthenticated] | ||
renderer_classes = [JSONRenderer, XMLRenderer] | ||
serializer_class = AcademySerializer | ||
lookup_field = 'uuid' | ||
|
||
@cached_property | ||
def request_action(self): | ||
return getattr(self, 'action', None) | ||
|
||
def get_queryset(self): | ||
""" | ||
Returns the queryset corresponding to all academies the requesting user has access to. | ||
""" | ||
enterprise_customer = self.request.GET.get('enterprise_customer', False) | ||
all_academies = Academy.objects.all() | ||
if self.request_action == 'list': | ||
if enterprise_customer: | ||
user_accessible_academy_uuids = [] | ||
for academy in all_academies: | ||
academy_associated_catalogs = academy.enterprise_catalogs.all() | ||
enterprise_associated_catalogs = academy_associated_catalogs.filter( | ||
enterprise_uuid=enterprise_customer | ||
) | ||
if enterprise_associated_catalogs: | ||
user_accessible_academy_uuids.append(academy.uuid) | ||
return all_academies.filter(uuid__in=user_accessible_academy_uuids) | ||
else: | ||
return Academy.objects.none() | ||
|
||
return Academy.objects.all() |