Преглед изворни кода

安装角色权限插件``django-role-permissions``
新增两个角色
新增两张表:前端路由备份表、角色和前端路由的关联表

DYaiu пре 4 година
родитељ
комит
7fcb0b522c
5 измењених фајлова са 95 додато и 1 уклоњено
  1. 1 0
      Pipfile
  2. 46 0
      basic/migrations/0002_grouproutermenu_routermenu.py
  3. 28 1
      basic/models.py
  4. 17 0
      my_project/roles.py
  5. 3 0
      my_project/settings.py

+ 1 - 0
Pipfile

@@ -13,6 +13,7 @@ mysql = "*"
 mysqlclient = "*"
 django-filter = "*"
 django-mptt = "*"
+django-role-permissions = "*"
 
 [requires]
 python_version = "3.7"

+ 46 - 0
basic/migrations/0002_grouproutermenu_routermenu.py

@@ -0,0 +1,46 @@
+# Generated by Django 3.0.8 on 2020-08-06 11:20
+
+from django.db import migrations, models
+import django.db.models.deletion
+import mptt.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('auth', '0011_update_proxy_permissions'),
+        ('basic', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='RouterMenu',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('name', models.CharField(max_length=50)),
+                ('path', models.CharField(max_length=50)),
+                ('url', 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, to='basic.RouterMenu')),
+            ],
+            options={
+                'verbose_name': '前端菜单',
+                'verbose_name_plural': '前端菜单列表',
+            },
+        ),
+        migrations.CreateModel(
+            name='GroupRouterMenu',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='auth.Group')),
+                ('menu', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='basic.RouterMenu')),
+            ],
+            options={
+                'verbose_name': '前端菜单与角色的中间表',
+                'verbose_name_plural': '前端菜单和角色的中间表列表',
+            },
+        ),
+    ]

+ 28 - 1
basic/models.py

@@ -1,4 +1,5 @@
-from django.contrib.auth.models import User
+from django.contrib.auth.models import User, Group
+from django.db.models import Model
 from mptt.fields import TreeForeignKey
 from mptt.models import MPTTModel
 from django.db import models
@@ -17,3 +18,29 @@ class Menu(MPTTModel):
         db_table = 'basic_menu'
         verbose_name = '报表菜单'
         verbose_name_plural = '报表菜单列表'
+
+
+class RouterMenu(MPTTModel):
+    """
+    前端路由备份,用作中间表做权限
+    """
+    name = models.CharField(max_length=50)
+    path = models.CharField(max_length=50)
+    url = models.CharField(max_length=50)
+    parent = TreeForeignKey('self', blank=True, null=True, on_delete=models.CASCADE)
+
+    class Meta:
+        verbose_name = '前端菜单'
+        verbose_name_plural = '前端菜单列表'
+
+
+class GroupRouterMenu(Model):
+    """
+    前端菜单的权限与角色绑定
+    """
+    group = models.ForeignKey(Group, blank=True, null=True, on_delete=models.CASCADE)
+    menu = models.ForeignKey(RouterMenu, blank=True, null=True, on_delete=models.CASCADE)
+
+    class Meta:
+        verbose_name = '前端菜单与角色的中间表'
+        verbose_name_plural = '前端菜单和角色的中间表列表'

+ 17 - 0
my_project/roles.py

@@ -0,0 +1,17 @@
+from rolepermissions.roles import AbstractUserRole
+
+
+class ReportManager(AbstractUserRole):
+    """
+    日报角色
+    """
+    available_permissions = {
+    }
+
+
+class PaymentManager(AbstractUserRole):
+    """
+    支出的角色
+    """
+    available_permissions = {
+    }

+ 3 - 0
my_project/settings.py

@@ -43,6 +43,7 @@ INSTALLED_APPS = [
     'treeAndTable',
     'payment',
     'basic',
+    'rolepermissions',
 ]
 
 MIDDLEWARE = [
@@ -142,3 +143,5 @@ REST_FRAMEWORK = {
 AUTHENTICATION_BACKENDS = (
     'django.contrib.auth.backends.ModelBackend'
 )
+
+ROLEPERMISSIONS_MODULE = 'my_project.roles'