Skip to content

Commit

Permalink
Add grant-default-model-level-perms route
Browse files Browse the repository at this point in the history
  • Loading branch information
Davis Muro committed Dec 21, 2022
1 parent 37107aa commit 3ab8f4f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
25 changes: 11 additions & 14 deletions kpi/urls/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
# coding: utf-8
import private_storage.urls
from django.conf import settings
from django.urls import include, re_path, path
from django.urls import include, path, re_path
from django.views.i18n import JavaScriptCatalog

from hub.models import ConfigurationFile
from hub.views import ExtraDetailRegistrationView
from kobo.apps.superuser_stats.views import (
user_report,
country_report,
retrieve_reports,
)
from kobo.apps.mfa.views import MfaLoginView, MfaTokenView
from kobo.apps.superuser_stats.views import (country_report, retrieve_reports,
user_report)
from kpi.forms.registration import RegistrationForm
from kpi.views import authorized_application_authenticate_user
from kpi.views import home, one_time_login, browser_tests, design_system, modern_browsers
from kpi.views.environment import EnvironmentView
from kpi.views import (authorized_application_authenticate_user, browser_tests,
design_system, home, modern_browsers, one_time_login)
from kpi.views.current_user import CurrentUserViewSet
from kobo.apps.mfa.views import (
MfaLoginView,
MfaTokenView,
)
from kpi.views.environment import EnvironmentView
from kpi.views.token import TokenView

from .router_api_v1 import router_api_v1
from .router_api_v2 import router_api_v2, URL_NAMESPACE
from .router_api_v2 import URL_NAMESPACE, router_api_v2

# TODO: Give other apps their own `urls.py` files instead of importing their
# views directly! See
Expand All @@ -37,6 +31,9 @@
'get': 'retrieve',
'patch': 'partial_update',
}), name='currentuser-detail'),
path('grant-default-model-level-perms', CurrentUserViewSet.as_view({
'post': 'grant_default_model_level_perms',
}), name='currentuser-default-perms')
re_path(r'^', include(router_api_v1.urls)),
re_path(r'^api/v2/', include((router_api_v2.urls, URL_NAMESPACE))),
re_path(r'^api/v2/auth/', include('kobo.apps.mfa.urls')),
Expand Down
20 changes: 19 additions & 1 deletion kpi/views/current_user.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# coding: utf-8
from django.contrib.auth.models import User
from rest_framework import viewsets
from rest_framework import renderers, status, viewsets
from rest_framework.decorators import detail_route
from rest_framework.response import Response

from kpi.serializers import CurrentUserSerializer
from kpi.utils.permissions import grant_default_model_level_perms


class CurrentUserViewSet(viewsets.ModelViewSet):
Expand All @@ -11,3 +14,18 @@ class CurrentUserViewSet(viewsets.ModelViewSet):

def get_object(self):
return self.request.user

@detail_route(methods=["POST"], renderer_classes=[renderers.JSONRenderer])
def grant_default_model_level_perms(self, request, *args, **kwargs):
user = self.get_object()
grant_default_model_level_perms(user)

return Response(
data={
"detail": (
"Successfully granted default model level "
f"perms to user {user.username}"
)
},
status=status.HTTP_200_OK
)

0 comments on commit 3ab8f4f

Please sign in to comment.