From 0c7e944b07b1d228b764b9c3c27c16c2c6bff398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Fri, 20 Apr 2018 22:09:33 +0000 Subject: [PATCH] Add permission for API view --- api/initial_perm.py | 17 +++++++++++++++++ api/settings.py | 6 ++++++ api/urls.py | 1 + re2o/urls.py | 7 +++++-- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 api/initial_perm.py diff --git a/api/initial_perm.py b/api/initial_perm.py new file mode 100644 index 00000000..f4482fd9 --- /dev/null +++ b/api/initial_perm.py @@ -0,0 +1,17 @@ +from django.contrib.contenttypes.models import ContentType +from django.contrib.auth.models import Permission +from django.conf import settings + +api_content_type, created = ContentType.objects.get_or_create( + app_label=settings.API_CONTENT_TYPE_APP_LABEL, + model=settings.API_CONTENT_TYPE_MODEL +) +if created: + api_content_type.save() +api_permission, created = Permission.objects.get_or_create( + name=settings.API_PERMISSION_NAME, + content_type=api_content_type, + codename=settings.API_PERMISSION_CODENAME +) +if created: + api_permission.save() diff --git a/api/settings.py b/api/settings.py index 767082ce..8cf152f2 100644 --- a/api/settings.py +++ b/api/settings.py @@ -35,3 +35,9 @@ REST_FRAMEWORK = { 'api.permissions.DefaultACLPermission', ) } + +# API permission settings +API_CONTENT_TYPE_APP_LABEL = 'api' +API_CONTENT_TYPE_MODEL = 'api' +API_PERMISSION_NAME = 'Can use the API' +API_PERMISSION_CODENAME = 'use_api' diff --git a/api/urls.py b/api/urls.py index 662dd8c4..90672b47 100644 --- a/api/urls.py +++ b/api/urls.py @@ -28,6 +28,7 @@ from django.conf.urls import url, include from rest_framework.routers import DefaultRouter from . import views +from . import initial_perm router = DefaultRouter() router.register(r'users', views.UserViewSet) diff --git a/re2o/urls.py b/re2o/urls.py index 47172521..34cb0b15 100644 --- a/re2o/urls.py +++ b/re2o/urls.py @@ -42,6 +42,7 @@ Including another URLconf """ from __future__ import unicode_literals +from django.conf import settings from django.conf.urls import include, url from django.contrib import admin from django.contrib.auth import views as auth_views @@ -70,6 +71,8 @@ urlpatterns = [ r'^preferences/', include('preferences.urls', namespace='preferences') ), - url(r'^api/', include('api.urls', namespace='api')), - ] +if 'api' in settings.INSTALLED_APPS: + urlpatterns += [ + url(r'^api/', include('api.urls', namespace='api')), + ]