Реализовал систему разрешений
This commit is contained in:
38
dbapp/mainapp/management/commands/init_permissions.py
Normal file
38
dbapp/mainapp/management/commands/init_permissions.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""
|
||||
Management command для инициализации разрешений в базе данных.
|
||||
|
||||
Usage:
|
||||
python manage.py init_permissions
|
||||
"""
|
||||
from django.core.management.base import BaseCommand
|
||||
from mainapp.models import UserPermission
|
||||
from mainapp.permissions import PERMISSIONS
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Инициализирует все разрешения в базе данных'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
created_count = 0
|
||||
existing_count = 0
|
||||
|
||||
for code, name, description in PERMISSIONS:
|
||||
permission, created = UserPermission.objects.get_or_create(code=code)
|
||||
if created:
|
||||
created_count += 1
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f'Создано разрешение: {code} - {name}')
|
||||
)
|
||||
else:
|
||||
existing_count += 1
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f'\nГотово! Создано: {created_count}, уже существовало: {existing_count}'
|
||||
)
|
||||
)
|
||||
|
||||
# Показываем все разрешения
|
||||
self.stdout.write('\nВсе разрешения в системе:')
|
||||
for code, name, description in PERMISSIONS:
|
||||
self.stdout.write(f' - {code}: {name}')
|
||||
Reference in New Issue
Block a user