Prechádzať zdrojové kódy

项目架构,认证实现,权限实现

yf_fyh 3 rokov pred
rodič
commit
f97fbe1b93

apps/SearchEquip/__init__.py → apps/Equipment/__init__.py


apps/SearchEquip/admin.py → apps/Equipment/admin.py


+ 5 - 0
apps/Equipment/apps.py

@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class EquipmentConfig(AppConfig):
+    name = 'apps.Equipment'

apps/SearchEquip/models.py → apps/Equipment/models.py


apps/SearchEquip/serializers.py → apps/Equipment/serializers.py


apps/SearchEquip/tests.py → apps/Equipment/tests.py


apps/SearchEquip/urls.py → apps/Equipment/urls.py


apps/SearchEquip/views.py → apps/Equipment/views.py


+ 0 - 9
apps/PestAnalysis/urls.py

@@ -1,9 +0,0 @@
-from django.conf.urls import url
-
-from . import views
-
-
-urlpatterns = [
-    url(r'^login/$', views.UserView.as_view(), name='login'),
-    url(r'^login_test/$', views.UserShowView.as_view(), name='login_test'),
-]

+ 0 - 31
apps/PestAnalysis/views.py

@@ -1,31 +0,0 @@
-from rest_framework.views import APIView
-from rest_framework.response import Response
-from django.contrib.auth import authenticate
-from utils.JWTAuthentication_diy import get_token,MyJWTAuthentication
-# Create your views here.
-
-
-class UserView(APIView):
-    #datail=True的时候,查询字符串携带pk
-    def post(self,request):
-        username = request.data.get('username')
-        password = request.data.get('password')
-        user = authenticate(username = username,password = password)
-        if user is not None:
-            token = get_token(user)
-            return Response(token)
-        else:
-            return Response("登录失败")
-
-
-class UserShowView(APIView):
-    #局部配置
-    authentication_classes = [MyJWTAuthentication]
-    # permission_classes = [IsAuthenticated]
-    def post(self,request):
-        token = request.auth
-        user = request.user
-        print(token)
-        print(user)
-        # print(user["user_modules"])
-        return Response("认证成功")

+ 0 - 6
apps/SearchEquip/apps.py

@@ -1,6 +0,0 @@
-from django.apps import AppConfig
-
-
-class SearchequipConfig(AppConfig):
-    name = 'apps.SearchEquip'
-    verbose_name = u'设备搜索界面'

+ 0 - 0
apps/SearchEquip/migrations/__init__.py


+ 2 - 1
apps/UserApp/models.py

@@ -5,7 +5,8 @@ from django.contrib.auth.models import AbstractUser
 
 class MyUser(AbstractUser):
     USERMODULES_CHOICES = {
-        1: 'PestAnalysis',
+        1: 'equipment',
+        2: 'PestAnalysis'
     }
 
     user_remark = models.TextField(u'用户备注', max_length=200, blank=True)

+ 15 - 0
apps/UserApp/serializers.py

@@ -0,0 +1,15 @@
+from rest_framework import serializers
+from .models import MyUser
+
+
+class RegisterViewSerializer(serializers.Serializer):
+    username = serializers.CharField(help_text="注册账户名", required=True)
+    password = serializers.CharField(help_text="注册密码", write_only=True)
+    user_modules = serializers.IntegerField(help_text="用户所属模块", required=True)
+
+    def create(self, validated_data):
+        try:
+            user = MyUser.objects.create_user(**validated_data)
+        except Exception as e:
+            raise ValueError("用户创建失败,已存在用户名")
+        return user

+ 9 - 0
apps/UserApp/urls.py

@@ -0,0 +1,9 @@
+from django.conf.urls import url
+
+from . import views
+
+
+urlpatterns = [
+    url(r'^login/$', views.LoginView.as_view(), name='login'),
+    url(r'^register/$', views.RegisterView.as_view(), name='login'),
+]

+ 41 - 1
apps/UserApp/views.py

@@ -1,3 +1,43 @@
-from django.shortcuts import render
+from rest_framework.views import APIView
+from rest_framework.generics import GenericAPIView
+from rest_framework.response import Response
+from rest_framework import status
+from django.contrib.auth import authenticate
+from utils.JWTAuthentication_diy import get_token, MyJWTAuthentication
+from .serializers import RegisterViewSerializer
 
 # Create your views here.
+
+
+class LoginView(APIView):
+    def post(self,request):
+        username = request.data.get('username')
+        password = request.data.get('password')
+        user = authenticate(username = username,password = password)
+        if user is not None and user.is_active:
+            token = get_token(user)
+            return Response(token)
+        else:
+            return Response(data = {"msg":"登录验证失败","data":""},status=status.HTTP_401_UNAUTHORIZED)
+
+
+class RegisterView(GenericAPIView):
+    authentication_classes = [MyJWTAuthentication]
+    serializer_class = RegisterViewSerializer
+    def post(self,request):
+        serializer = self.get_serializer(data=request.data)
+        serializer.is_valid()
+        serializer.save()
+        return Response(serializer.data)
+
+
+# class UserShowView(APIView):
+#     #局部配置
+#     authentication_classes = [MyJWTAuthentication]
+#     def post(self,request):
+#         token = request.auth
+#         user = request.user
+#         print(token)
+#         print(user)
+#         # print(user["user_modules"])
+#         return Response("认证成功")

+ 5 - 1
bigdataAPI/settings.py

@@ -42,9 +42,9 @@ INSTALLED_APPS = [
     'rest_framework',
     'rest_framework_jwt',
     'apps.UserApp',
-    'apps.SearchEquip',
     'apps.IOTCard',
     'apps.PestAnalysis',
+    'apps.Equipment',
 ]
 
 MIDDLEWARE = [
@@ -142,6 +142,10 @@ REST_FRAMEWORK = {
     'DEFAULT_RENDERER_CLASSES': (
         'utils.rendererresponse.CustomRender',
     ),
+    # 全局配置权限,写法上根据路径进行判断可全局配置
+    'DEFAULT_PERMISSION_CLASSES': (
+        'utils.permissions.CustomPermission',
+    ),
 }
 
 

+ 2 - 2
bigdataAPI/urls.py

@@ -18,7 +18,7 @@ from django.urls import path, include
 
 urlpatterns = [
     # path('admin/', admin.site.urls),
-    path('equipmanager/', include('apps.SearchEquip.urls')),
     path('iotcard/', include('apps.IOTCard.urls')),
-    path('pestanalysis/', include('apps.PestAnalysis.urls')),
+    path('user/', include('apps.UserApp.urls')),
+    path('equipment/', include('apps.Equipment.urls')),
 ]

+ 17 - 0
utils/permissions.py

@@ -0,0 +1,17 @@
+# coding:utf-8
+
+from rest_framework.permissions import BasePermission
+
+
+class CustomPermission(BasePermission):
+
+    def has_permission(self, request, view):
+        path = request.path
+        user = request.user
+        if path == '/user/register/':
+            if user['username'] == "管理员":
+                return True
+            else:
+                return False
+        else:
+            return True