Jelajahi Sumber

优化route app,新增用户与路由关联表,新增用户权限路由,根据用户ID返回不同路由

liceal 4 tahun lalu
induk
melakukan
5dabc975c4

TEMPAT SAMPAH
Django_test/__pycache__/settings.cpython-37.pyc


TEMPAT SAMPAH
Django_test/__pycache__/urls.cpython-37.pyc


+ 2 - 1
Django_test/settings.py

@@ -41,7 +41,8 @@ INSTALLED_APPS = [
     'mptt_test',
     'even_table',
     'payment',
-    'basic'
+    'basic',
+    'routes',
 ]
 
 MIDDLEWARE = [

+ 2 - 1
Django_test/urls.py

@@ -26,5 +26,6 @@ urlpatterns = [
     url(r'work/', include('mptt_test.urls')),  # 使用mptt
     url(r'work/', include('even_table.urls')),
     url(r'work/', include('payment.urls')),
-    url(r'work/', include('basic.urls'))
+    url(r'work/', include('basic.urls')),
+    url(r'work/', include('routes.urls')),
 ]

TEMPAT SAMPAH
payment/__pycache__/urls.cpython-37.pyc


TEMPAT SAMPAH
payment/__pycache__/views.cpython-37.pyc


+ 4 - 2
payment/urls.py

@@ -19,12 +19,14 @@ from django.conf.urls import url
 # 主入口请求出口
 from rest_framework_bulk.routes import BulkRouter
 
-from payment.views import PaymentView, StatisticsView
+from payment.views import PaymentView, StatisticsView, groupView, testView
 
 router = BulkRouter()
 router.register(r'payment', PaymentView)
 
 urlpatterns = router.urls
 urlpatterns += [
-    url('statistics',StatisticsView.as_view())
+    url('statistics', StatisticsView.as_view()),
+    url('group', groupView.as_view()),
+    url('test', testView.as_view()),
 ]

+ 21 - 2
payment/views.py

@@ -17,14 +17,33 @@ class PaymentView(ModelViewSet):
     pagination_class = None
 
 
-# 统计 {order_by:0(默认0升序,1降序)}
+# 统计
 class StatisticsView(APIView):
+    # 统计 {order_by:0(默认0升序,1降序)}
     def get(self, request):
         data = request.GET
         order_by_value = "price_sum"
         if 'order_by' in data:
             if data['order_by'] == 1:
                 order_by_value = "-price_sum"
-        res = Payment.objects.values('payment_type', 'year', 'month').annotate(price_sum=Sum('index_price')).order_by(
+        res = Payment.objects.values('payment_type', price_sum=Sum('index_price')).annotate(
+            payment_type_count=Count('payment_type')).order_by(
             order_by_value)
         return Response(data=res, status=status.HTTP_200_OK)
+
+
+# 去重
+class groupView(APIView):
+    # 得到指定字段去重
+    def get(self, request):
+        data = request.GET
+        res = Payment.objects.values(data['field']).annotate(count=Count(data['field']))
+        return Response(data=res, status=status.HTTP_200_OK)
+
+
+# queryset测试
+class testView(APIView):
+    def get(self, request):
+        data = request.GET
+        res = Payment.objects.exclude(payment_type="一、一般公共服务").values()
+        return Response(data=res, status=status.HTTP_200_OK)

+ 0 - 0
routes/__init__.py


TEMPAT SAMPAH
routes/__pycache__/__init__.cpython-37.pyc


TEMPAT SAMPAH
routes/__pycache__/admin.cpython-37.pyc


TEMPAT SAMPAH
routes/__pycache__/models.cpython-37.pyc


TEMPAT SAMPAH
routes/__pycache__/serializers.cpython-37.pyc


TEMPAT SAMPAH
routes/__pycache__/urls.cpython-37.pyc


TEMPAT SAMPAH
routes/__pycache__/views.cpython-37.pyc


+ 3 - 0
routes/admin.py

@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.

+ 5 - 0
routes/apps.py

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

+ 36 - 0
routes/migrations/0001_initial.py

@@ -0,0 +1,36 @@
+# Generated by Django 3.0.8 on 2020-07-31 08:42
+
+from django.db import migrations, models
+import django.db.models.deletion
+import mptt.fields
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Route',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('path', models.CharField(max_length=50)),
+                ('name', models.CharField(max_length=50)),
+                ('alias', models.CharField(max_length=50)),
+                ('component', models.CharField(max_length=50)),
+                ('lft', models.PositiveIntegerField(editable=False)),
+                ('rght', models.PositiveIntegerField(editable=False)),
+                ('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
+                ('level', models.PositiveIntegerField(editable=False)),
+                ('parent', mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='routes.Route')),
+            ],
+            options={
+                'verbose_name': '路由表',
+                'verbose_name_plural': '路由表',
+                'db_table': 'routes',
+            },
+        ),
+    ]

+ 18 - 0
routes/migrations/0002_auto_20200803_1041.py

@@ -0,0 +1,18 @@
+# Generated by Django 3.0.8 on 2020-08-03 02:41
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('routes', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.RenameField(
+            model_name='route',
+            old_name='component',
+            new_name='url',
+        ),
+    ]

+ 29 - 0
routes/migrations/0003_route2user.py

@@ -0,0 +1,29 @@
+# Generated by Django 3.0.8 on 2020-08-03 06:06
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('routes', '0002_auto_20200803_1041'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Route2User',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('route', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='routes.Route')),
+                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+            ],
+            options={
+                'verbose_name': '用户对应路由关联表',
+                'verbose_name_plural': '用户对应路由关联表',
+                'db_table': 'Route2User',
+            },
+        ),
+    ]

+ 0 - 0
routes/migrations/__init__.py


TEMPAT SAMPAH
routes/migrations/__pycache__/0001_initial.cpython-37.pyc


TEMPAT SAMPAH
routes/migrations/__pycache__/0002_auto_20200803_1041.cpython-37.pyc


TEMPAT SAMPAH
routes/migrations/__pycache__/__init__.cpython-37.pyc


+ 31 - 0
routes/models.py

@@ -0,0 +1,31 @@
+from django.contrib.auth.models import User
+from django.db import models
+
+# Create your models here.
+from mptt.fields import TreeForeignKey
+from mptt.models import MPTTModel
+
+
+class Route(MPTTModel):
+    path = models.CharField(max_length=50)
+    name = models.CharField(max_length=50)
+    alias = models.CharField(max_length=50)
+    url = models.CharField(max_length=50)
+    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
+
+    class Meta:
+        app_label = "routes"  # 指向自己的文件夹,不是项目,是这个模型的
+        db_table = "routes"  # 定义生成表的名称
+        verbose_name = "路由表"  # 注解
+        verbose_name_plural = "路由表"  # 注解
+
+
+class Route2User(models.Model):
+    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
+    route = models.ForeignKey(Route, on_delete=models.CASCADE, blank=True, null=True)
+
+    class Meta:
+        app_label = "routes"  # 指向自己的文件夹,不是项目,是这个模型的
+        db_table = "Route2User"  # 定义生成表的名称
+        verbose_name = "用户对应路由关联表"  # 注解
+        verbose_name_plural = "用户对应路由关联表"  # 注解

+ 15 - 0
routes/serializers.py

@@ -0,0 +1,15 @@
+# 序列号定义
+from django.contrib.auth.models import User
+from rest_framework import serializers
+from rest_framework.serializers import ListSerializer
+
+from routes.models import Route
+
+
+class RoutesSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = Route
+        list_serializer = ListSerializer
+        fields = '__all__'
+
+

+ 3 - 0
routes/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 11 - 0
routes/urls.py

@@ -0,0 +1,11 @@
+from rest_framework_bulk.routes import BulkRouter
+
+from routes.views import RoutesView
+
+router = BulkRouter()
+router.register(r'routes', RoutesView)
+
+urlpatterns = router.urls
+urlpatterns += [
+
+]

+ 24 - 0
routes/views.py

@@ -0,0 +1,24 @@
+# Create your views here.
+import json
+
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.viewsets import ModelViewSet
+from django.forms.models import model_to_dict
+
+from routes.models import Route, Route2User
+from routes.serializers import RoutesSerializer
+
+
+class RoutesView(ModelViewSet):
+    queryset = Route.objects.all()
+    serializer_class = RoutesSerializer
+    pagination_class = None
+
+    # 根据用户id
+    def list(self, request, *args, **kwargs):
+        now_user_id = request.user.id
+        route_ids = Route2User.objects.filter(user_id=now_user_id).values('route_id')
+        routes = Route.objects.filter(id__in=route_ids).values()
+
+        return Response(data=routes, status=status.HTTP_200_OK)