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

add basic auth #18

Merged
merged 2 commits into from
Nov 10, 2023
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
12 changes: 12 additions & 0 deletions smartreport/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"smartreport_app",
"corsheaders",
'django_filters',
'guardian',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -139,10 +140,21 @@
},
]

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'guardian.backends.ObjectPermissionBackend',
]

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework.authentication.BasicAuthentication",
"rest_framework.authentication.SessionAuthentication",
),
# UNCOMMENT THIS TO ENABLE PERMISSIONS CHECKS
#
# 'DEFAULT_PERMISSION_CLASSES': [
# 'smartreport_app.permissions.FullObjectPermission',
# ],
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}

Expand Down
16 changes: 16 additions & 0 deletions smartreport_app/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from rest_framework import permissions


class FullObjectPermission(permissions.DjangoObjectPermissions):
"""
Similar to `DjangoObjectPermissions`, but adding 'view' permissions.
"""
perms_map = {
'GET': ['%(app_label)s.view_%(model_name)s'],
'OPTIONS': ['%(app_label)s.view_%(model_name)s'],
'HEAD': ['%(app_label)s.view_%(model_name)s'],
'POST': ['%(app_label)s.add_%(model_name)s'],
'PUT': ['%(app_label)s.change_%(model_name)s'],
'PATCH': ['%(app_label)s.change_%(model_name)s'],
'DELETE': ['%(app_label)s.delete_%(model_name)s'],
}
42 changes: 13 additions & 29 deletions smartreport_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,50 @@
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.decorators import api_view

from .kb_interface import kb_interface



class ReportTemplateViewSet(viewsets.ModelViewSet):

queryset = ReportTemplate.objects.all()
serializer_class = ReportTemplateSerializer

class ReportTemplatePageViewSet(viewsets.ModelViewSet):

queryset = ReportTemplatePage.objects.all()
serializer_class = ReportTemplatePageSerializer

def create(self, request, *args, **kwargs):
return Response({"message": "POST method is not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

class KpiReportElementViewSet(viewsets.ModelViewSet):

queryset = KpiReportElement.objects.all()
serializer_class = KpiReportElementSerializer

def create(self, request, *args, **kwargs):
return Response({"message": "POST method is not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

def update(self, request, *args, **kwargs):
return Response({"message": "PUT method is not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

class KpiViewSet(viewsets.ModelViewSet):
class KpiViewSet(viewsets.ReadOnlyModelViewSet):

queryset = Kpi.objects.all()
serializer_class = KpiSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = [ 'user_type' , 'name' ]

def create(self, request, *args, **kwargs):
return Response({"message": "POST method is not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

def update(self, request, *args, **kwargs):
return Response({"message": "PUT method is not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
filterset_fields = [ 'user_type' , 'name' ]

class AlarmViewSet(viewsets.ModelViewSet):

queryset = Alarm.objects.all()
serializer_class = AlarmSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = [ 'user_type' ]

class ChartTypeViewSet(viewsets.ModelViewSet):
class ChartTypeViewSet(viewsets.ReadOnlyModelViewSet):

queryset = ChartType.objects.all()
serializer_class = ChartTypeSerializer

def create(self, request, *args, **kwargs):
return Response({"message": "POST method is not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

def update(self, request, *args, **kwargs):
return Response({"message": "PUT method is not allowed"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

class DashboardLayoutViewSet(viewsets.ModelViewSet):

queryset = DashboardLayout.objects.all()
serializer_class = DashboardLayoutSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = [ 'user_type' ]


@api_view(['GET'])
def kpi_data(request, format=None):

Expand Down
Loading