diff --git a/backend/models.py b/backend/models.py index 855c5ef7425e006a629a840794c0322f58ade814..e15d115f9a3a1dc170e7b0c2f38a8cf6e52d33a9 100644 --- a/backend/models.py +++ b/backend/models.py @@ -6,11 +6,18 @@ class hosts(models.Model): password = models.CharField(max_length=50) port = models.IntegerField(null=True, blank=True) cluster = models.CharField(max_length=50) + def __str__(self): + return self.ipaddress + class users(models.Model): user = models.CharField(max_length=50) password = models.CharField(max_length=50) + def __str__(self): + return self.user class cluster(models.Model): name = models.CharField(max_length=50) + def __str__(self): + return self.name diff --git a/backend/serializers.py b/backend/serializers.py new file mode 100644 index 0000000000000000000000000000000000000000..f65b460570a54b604f112cac7f2ba87de4cae290 --- /dev/null +++ b/backend/serializers.py @@ -0,0 +1,14 @@ +from django.contrib.auth.models import Group, User +from rest_framework import serializers + + +class UserSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = User + fields = ['url', 'username', 'email', 'groups'] + + +class GroupSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Group + fields = ['url', 'name'] \ No newline at end of file diff --git a/backend/views.py b/backend/views.py index 91ea44a218fbd2f408430959283f0419c921093e..264ff20f38c57604a1d8a445fef80d495c12a08d 100644 --- a/backend/views.py +++ b/backend/views.py @@ -1,3 +1,23 @@ -from django.shortcuts import render +from django.contrib.auth.models import Group, User +from rest_framework import permissions, viewsets +from backend.serializers import GroupSerializer, UserSerializer +from django.shortcuts import render, HttpResponse + +class UserViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows users to be viewed or edited. + """ + queryset = User.objects.all().order_by('-date_joined') + serializer_class = UserSerializer + # permission_classes = [permissions.IsAuthenticated] + + +class GroupViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows groups to be viewed or edited. + """ + queryset = Group.objects.all() + serializer_class = GroupSerializer + # permission_classes = [permissions.IsAuthenticated] # Create your views here. diff --git a/safeguard_web/__pycache__/__init__.cpython-39.pyc b/safeguard_web/__pycache__/__init__.cpython-39.pyc index e93afb5d9e8b3fac386606106a6f2ffafb73ba01..bc0a3466596b1f4e31c7f6e9d4af346b45abb9e1 100644 Binary files a/safeguard_web/__pycache__/__init__.cpython-39.pyc and b/safeguard_web/__pycache__/__init__.cpython-39.pyc differ diff --git a/safeguard_web/__pycache__/settings.cpython-39.pyc b/safeguard_web/__pycache__/settings.cpython-39.pyc index 51ead38da154322885bb8eaca7c3e3bda9f9280c..e3294b9662e774a2b8b7fc6dc1d74cb52b63fbe6 100644 Binary files a/safeguard_web/__pycache__/settings.cpython-39.pyc and b/safeguard_web/__pycache__/settings.cpython-39.pyc differ diff --git a/safeguard_web/__pycache__/urls.cpython-39.pyc b/safeguard_web/__pycache__/urls.cpython-39.pyc index e437a816c6353ed6891d110bf40ff00900254903..84f42b62cf93b7f5644ef629fdb5727f0d74ab63 100644 Binary files a/safeguard_web/__pycache__/urls.cpython-39.pyc and b/safeguard_web/__pycache__/urls.cpython-39.pyc differ diff --git a/safeguard_web/settings.py b/safeguard_web/settings.py index e985665f9688e6952adf00ef960c3c860dd34325..8fc43416130609598245282dfbbe5db718f215c8 100644 --- a/safeguard_web/settings.py +++ b/safeguard_web/settings.py @@ -32,6 +32,7 @@ ALLOWED_HOSTS = ["*"] INSTALLED_APPS = [ 'backend.apps.BackendConfig', + 'rest_framework', 'rest_framework_swagger', 'django.contrib.admin', 'django.contrib.auth', @@ -45,6 +46,28 @@ REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' } +SWAGGER_SETTINGS = { + # 基础样式 + 'SECURITY_DEFINITIONS': { + "basic":{ + 'type': 'basic' + } + }, + # 如果需要登录才能够查看接口文档, 登录的链接使用restframework自带的. + # 'LOGIN_URL': 'rest_framework:login', + # 'LOGOUT_URL': 'rest_framework:logout', + # 'DOC_EXPANSION': None, + # 'SHOW_REQUEST_HEADERS':True, + # 'USE_SESSION_AUTH': True, + # 'DOC_EXPANSION': 'list', + # 接口文档中方法列表以首字母升序排列 + 'APIS_SORTER': 'alpha', + # 如果支持json提交, 则接口文档中包含json输入框 + 'JSON_EDITOR': True, + # 方法列表字母排序 + 'OPERATIONS_SORTER': 'alpha', + 'VALIDATOR_URL': None, +} MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', diff --git a/safeguard_web/urls.py b/safeguard_web/urls.py index fee54943e0161fd687afe9c2618b827f28f7714b..40da0000764a62f207bdfffbdb29654785255d27 100644 --- a/safeguard_web/urls.py +++ b/safeguard_web/urls.py @@ -1,11 +1,21 @@ from django.contrib import admin -from django.urls import path -from rest_framework_swagger.views import get_swagger_view +from django.urls import path,include +from rest_framework import routers +from backend import views +from rest_framework.schemas import get_schema_view +from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer -schema_view = get_swagger_view(title='API') +schema_view = get_schema_view(title='API', renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer]) + +router = routers.DefaultRouter() +router.register(r'users',views.UserViewSet) +router.register(r'groups',views.GroupViewSet) urlpatterns = [ path('admin/', admin.site.urls), - path('doc/', schema_view) + path('',include(router.urls)), + path('api-auth/',include('rest_framework.urls',namespace='rest_framework')), + path('docs/',schema_view,name='docs'), ] +urlpatterns += router.urls \ No newline at end of file