mirror of https://github.com/snachodog/mybuddy.git
feat: Add data migration to create groups and assign permissions
This commit is contained in:
parent
b50748598b
commit
84d14c3410
|
@ -0,0 +1,69 @@
|
||||||
|
# Generated by Django 4.1.2 on 2022-10-23 07:59
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
from django.contrib.auth.models import Group, Permission
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# MODELS = [model.__name__ for model in apps.get_models()]
|
||||||
|
MODELS = [
|
||||||
|
"Tag",
|
||||||
|
"tagged",
|
||||||
|
"BMI",
|
||||||
|
"Child",
|
||||||
|
"Diaper Change",
|
||||||
|
"Feeding",
|
||||||
|
"Head Circumference",
|
||||||
|
"Height",
|
||||||
|
"Note",
|
||||||
|
"Pumping",
|
||||||
|
"Sleep",
|
||||||
|
"Temperature",
|
||||||
|
"Timer",
|
||||||
|
"Tummy Time",
|
||||||
|
"Weight",
|
||||||
|
"user",
|
||||||
|
]
|
||||||
|
PERMISSIONS = ["add", "change", "delete", "view"]
|
||||||
|
|
||||||
|
|
||||||
|
def add_group_permissions(apps, schema_editor):
|
||||||
|
group, created = Group.objects.get_or_create(name="read_only")
|
||||||
|
perm = []
|
||||||
|
for model in MODELS:
|
||||||
|
name = f"Can view {model}"
|
||||||
|
logging.info(f"Creating {name}...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
perm.append(Permission.objects.get(name=name))
|
||||||
|
except Permission.DoesNotExist:
|
||||||
|
logging.warning(f"Permission not found with name {name}.")
|
||||||
|
continue
|
||||||
|
|
||||||
|
group.permissions.add(*perm)
|
||||||
|
|
||||||
|
group, created = Group.objects.get_or_create(name="standard")
|
||||||
|
perm = []
|
||||||
|
for model in MODELS:
|
||||||
|
for permission in PERMISSIONS:
|
||||||
|
name = f"Can {permission} {model}"
|
||||||
|
logging.info(f"Creating {name}...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
perm.append(Permission.objects.get(name=name))
|
||||||
|
except Permission.DoesNotExist:
|
||||||
|
logging.warning(f"Permission not found with name {name}.")
|
||||||
|
continue
|
||||||
|
|
||||||
|
group.permissions.add(*perm)
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("babybuddy", "0023_alter_settings_timezone"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [migrations.RunPython(add_group_permissions)]
|
|
@ -24,9 +24,6 @@ class CommandsTestCase(TransactionTestCase):
|
||||||
self.assertEqual(Child.objects.count(), 1)
|
self.assertEqual(Child.objects.count(), 1)
|
||||||
|
|
||||||
def test_createuser(self):
|
def test_createuser(self):
|
||||||
Group.objects.create(name="read_only")
|
|
||||||
Group.objects.create(name="standard")
|
|
||||||
|
|
||||||
call_command(
|
call_command(
|
||||||
"createuser",
|
"createuser",
|
||||||
username="test",
|
username="test",
|
||||||
|
|
Loading…
Reference in New Issue