Skip to content

Commit

Permalink
feat: new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Sep 6, 2021
1 parent f5940f4 commit b6f9d11
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
4 changes: 3 additions & 1 deletion example/unicms/settingslocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,12 @@

ALLOWED_UNICMS_SITES = [2]
ALLOWED_CDS_COURSETYPES = ['L','LM','LM5','LM6','M1-270','M2-270']
ALLOWED_ADDRESSBOOK_STRUCTURE_TYPES = ['AREAUOC', 'DIP', 'MCRA','SET']
ALLOWED_ADDRESSBOOK_ROLES = ['PO', 'PA', 'RU', 'RD', 'ND', 'AR',
'BS', 'CB', 'CC', 'DR', 'NM']
ALLOWED_TEACHER_ROLES = ['PO', 'PA', 'RU', 'RD']
INITIAL_STRUCTURE_FATHER = "170005"

ALLOWED_STRUCTURE_TYPES = ['AREAUOC', 'AMCEN', 'DIP', 'MCRA','SET']
# END UNICAL STORAGE HANDLER


Expand Down
19 changes: 19 additions & 0 deletions src/cms/api/tests/test_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,22 @@ def test_media(self):
url = reverse('unicms_api:media-form')
res = req.get(url)
assert isinstance(res.json(), list)


def test_media_filetypes(self):
"""
Media allowed file types
"""
req = Client()
user = ContextUnitTest.create_user()

url = reverse('unicms_api:media-filetype-list')

# accessible to staff users only
res = req.get(url)
assert res.status_code == 403
user.is_staff = True
user.save()
req.force_login(user)
res = req.get(url)
assert isinstance(res.json(), list)
21 changes: 20 additions & 1 deletion src/cms/api/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setUp(self):

def test_user(self):
"""
Carousel API
Current User API
"""
req = Client()
user = ContextUnitTest.create_user()
Expand All @@ -35,3 +35,22 @@ def test_user(self):
req.force_login(user)
res = req.get(url)
assert isinstance(res.json(), list)

def test_user_detail(self):
"""
User Detail API
"""
req = Client()
user = ContextUnitTest.create_user()

url = reverse('unicms_api:user-detail',
kwargs={'user_id': user.pk})

# accessible to staff users only
res = req.get(url)
assert res.status_code == 403
user.is_staff = True
user.save()
req.force_login(user)
res = req.get(url)
assert isinstance(res.json(), object)
2 changes: 2 additions & 0 deletions src/cms/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
urlpatterns += path(f'{m_prefix}/form/', media.MediaFormView.as_view(), name='media-form'),
urlpatterns += path(f'{m_prefix}/options/', media.MediaOptionList.as_view(), name='media-options'),
urlpatterns += path(f'{m_prefix}/options/<int:pk>/', media.MediaOptionView.as_view(), name='media-option'),
urlpatterns += path(f'{m_prefix}/allowed-filetypes/', media.MediaFileTypeAllowedList.as_view(), name='media-filetype-list'),

# media collections
mc_prefix = f'{eb_prefix}/media-collections'
Expand Down Expand Up @@ -376,4 +377,5 @@
# users
u_prefix = f'{eb_prefix}/users'
# urlpatterns += path(f'{u_prefix}/form/', webpath_pub_context.EditorialBoardLockUserFormView.as_view(), name='users-form'),
urlpatterns += path(f'{u_prefix}/<int:user_id>/', users.UserDetail.as_view(), name='user-detail'),
urlpatterns += path(f'{u_prefix}/current/', users.CurrentUserIDView.as_view(), name='users-current'),
19 changes: 19 additions & 0 deletions src/cms/api/views/media.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import logging
import os

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.http import Http404
from django.shortcuts import get_object_or_404

from cms.medias import settings as media_settings
from cms.medias.forms import MediaForm
from cms.medias.models import Media
from cms.medias.serializers import MediaSerializer, MediaSelectOptionsSerializer
Expand All @@ -25,6 +27,9 @@

logger = logging.getLogger(__name__)

FILETYPE_ALLOWED = getattr(settings, 'FILETYPE_ALLOWED',
media_settings.FILETYPE_ALLOWED)


class MediaList(UniCMSListCreateAPIView):
"""
Expand Down Expand Up @@ -143,3 +148,17 @@ def get_queryset(self, **kwargs):
item = get_object_or_404(Media, pk=object_id)
content_type_id = ContentType.objects.get_for_model(item).pk
return super().get_queryset(object_id, content_type_id)


class MediaFileTypeAllowedList(generics.ListAPIView):
"""
"""
description = ""
permission_classes = [IsAdminUser]

def get(self, request):
"""
"""
return Response(tuple(sorted(FILETYPE_ALLOWED)))


24 changes: 21 additions & 3 deletions src/cms/api/views/users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.contrib.auth import get_user_model

from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
Expand All @@ -9,11 +11,27 @@ class CurrentUserIDView(APIView):
description = ""
permission_classes = [IsAdminUser]

def get(self, *args, **kwargs):
users = [self.request.user.pk]
return Response(users)


class UserDetail(APIView):
"""
"""
description = ""
permission_classes = [IsAdminUser]

def get_queryset(self):
"""
"""
users = [self.request.user.pk]
return users
user_id = self.kwargs.get('user_id')
if user_id:
return get_user_model().objects.filter(pk=user_id).\
values('first_name',
'last_name',
'email')
return get_user_model().objects.none() # pragma: no cover

def get(self, *args, **kwargs):
return Response(self.get_queryset())
return Response(self.get_queryset().first())

0 comments on commit b6f9d11

Please sign in to comment.