Files
dbstorage/dbapp/mainapp/management/commands/init_permissions.py

39 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
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}')