Skip to content

Commit

Permalink
Fix migration issues from rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Jan 16, 2024
1 parent 405792f commit 752e0c6
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Migration(migrations.Migration):

dependencies = [
('ansible_base', '0009_authenticator_users'),
('ansible_base', '0010_alter_authenticator_type'),
('contenttypes', '0002_remove_content_type_name'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
migrations.swappable_dependency(settings.ANSIBLE_BASE_TEAM_MODEL),
Expand Down
6 changes: 3 additions & 3 deletions ansible_base/models/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from crum import get_current_user
from django.contrib.auth import get_user_model
from django.conf import settings
from django.db import models
from django.urls.exceptions import NoReverseMatch
from django.utils import timezone
Expand Down Expand Up @@ -31,7 +31,7 @@ class Meta:
help_text="The date/time this resource was created",
)
created_by = models.ForeignKey(
get_user_model(),
settings.AUTH_USER_MODEL,
related_name='%(app_label)s_%(class)s_created+',
default=None,
null=True,
Expand All @@ -45,7 +45,7 @@ class Meta:
help_text="The date/time this resource was created",
)
modified_by = models.ForeignKey(
get_user_model(),
settings.AUTH_USER_MODEL,
related_name='%(app_label)s_%(class)s_modified+',
default=None,
null=True,
Expand Down
62 changes: 0 additions & 62 deletions ansible_base/tests/functional/models.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,11 @@
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models

from ansible_base.models import AbstractOrganization

from ansible_base.rbac import permission_registry


class Organization(AbstractOrganization):
pass


class User(AbstractUser):
singleton_roles = models.ManyToManyField('ansible_base.RoleDefinition', related_name='singleton_users')


class Team(models.Model):
name = models.CharField(max_length=512)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)

tracked_users = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='tracked_teams')
team_parents = models.ManyToManyField('Team', related_name='team_children')

singleton_roles = models.ManyToManyField('ansible_base.RoleDefinition')

class Meta:
app_label = 'functional'
permissions = [('member_team', 'Has all roles assigned to this team')]


class Inventory(models.Model):
"Simple example of a child object, it has a link to its parent organization"
name = models.CharField(max_length=512)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)

class Meta:
app_label = 'functional'
permissions = [('update_inventory', 'Do inventory updates')]


class InstanceGroup(models.Model):
"Example of an object with no parent object, a root resource, a lone wolf"
name = models.CharField(max_length=512)

class Meta:
app_label = 'functional'
default_permissions = ('change', 'delete', 'view')


class Namespace(models.Model):
"Example of a child object with its own child objects"
name = models.CharField(max_length=64, unique=True, blank=False)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)


class CollectionImport(models.Model):
"Example of a child of a child object, organization is implied by its namespace"
name = models.CharField(max_length=64, unique=True, blank=False)
namespace = models.ForeignKey(Namespace, on_delete=models.CASCADE)


class ExampleEvent(models.Model):
"Example of a model which is not registered in permission registry in the first place"
name = models.CharField(max_length=64, unique=True, blank=False)


permission_registry.register(Organization, Inventory, Namespace, Team)
permission_registry.register(CollectionImport, parent_field_name='namespace')
permission_registry.register(InstanceGroup, parent_field_name=None)

permission_registry.track_relationship(Team, 'tracked_users', 'team-member')
permission_registry.track_relationship(Team, 'team_parents', 'team-member')
2 changes: 1 addition & 1 deletion ansible_base/tests/functional/rbac/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ansible_base.models.rbac import RoleDefinition
from ansible_base.rbac import permission_registry
from ansible_base.tests.functional.models import Inventory, Organization
from test_app.models import Inventory, Organization


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ansible_base.management.commands.RBAC_checks import Command
from ansible_base.models.rbac import ObjectRole, RoleDefinition
from ansible_base.tests.functional.models import Inventory
from test_app.models import Inventory


def run_and_get_output():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ansible_base.models.rbac import RoleDefinition
from ansible_base.rbac.permission_registry import permission_registry
from ansible_base.tests.functional.models import CollectionImport, Namespace, Organization
from test_app.models import CollectionImport, Namespace, Organization


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ansible_base.models.rbac import RoleDefinition, RoleEvaluation
from ansible_base.rbac.permission_registry import permission_registry
from ansible_base.tests.functional.models import Inventory, Organization
from test_app.models import Inventory, Organization


@pytest.mark.django_db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ansible_base.models.rbac import ObjectRole, RoleDefinition, RoleEvaluation
from ansible_base.rbac.permission_registry import permission_registry
from ansible_base.tests.functional.models import Inventory, Organization
from test_app.models import Inventory, Organization


@pytest.mark.django_db
Expand Down
2 changes: 1 addition & 1 deletion ansible_base/tests/functional/rbac/test_triggers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from ansible_base.tests.functional.models import Inventory, Organization
from test_app.models import Inventory, Organization


@pytest.mark.django_db
Expand Down
2 changes: 1 addition & 1 deletion ansible_base/tests/functional/rbac/test_uniqueness.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ansible_base.models.rbac import ObjectRole, RoleDefinition, RoleEvaluation
from ansible_base.rbac import permission_registry
from ansible_base.tests.functional.models import Organization
from test_app.models import Organization


@pytest.mark.django_db
Expand Down
4 changes: 2 additions & 2 deletions ansible_base/tests/settings_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@

STATIC_URL = '/static/'

AUTH_USER_MODEL = 'functional.User'
AUTH_USER_MODEL = 'test_app.User'
ANSIBLE_BASE_TEAM_MODEL = 'test_app.Team'
ANSIBLE_BASE_ORGANIZATION_MODEL = 'functional.Organization'
ANSIBLE_BASE_ORGANIZATION_MODEL = 'test_app.Organization'
ROLE_CREATOR_DEFAULTS = ['change', 'delete', 'view']
GATEWAY_ROLE_PRECREATE = {} # tested in individual tests
ROLE_TEAM_TEAM_ALLOWED = True
Expand Down
51 changes: 45 additions & 6 deletions test_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,69 @@
# Generated by Django 4.2.6 on 2024-01-12 12:27
# Generated by Django 4.2.8 on 2024-01-16 07:44

from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='EncryptionModel',
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='Organization',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(default=None, editable=False, help_text='The date/time this resource was created')),
('modified_on', models.DateTimeField(default=None, editable=False, help_text='The date/time this resource was created')),
('name', models.CharField(help_text='The name of this resource', max_length=512)),
('testing1', models.CharField(default='a', max_length=1, null=True)),
('testing2', models.CharField(default='b', max_length=1, null=True)),
('name', models.CharField(help_text='The name of this resource', max_length=512, unique=True)),
('description', models.TextField(default='', help_text='The organization description.')),
('created_by', models.ForeignKey(default=None, editable=False, help_text='The user who created this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_created+', to=settings.AUTH_USER_MODEL)),
('modified_by', models.ForeignKey(default=None, editable=False, help_text='The user who last modified this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_modified+', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='Team',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=512)),
('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.organization')),
('team_parents', models.ManyToManyField(related_name='team_children', to='test_app.team')),
('tracked_users', models.ManyToManyField(related_name='tracked_teams', to=settings.AUTH_USER_MODEL)),
],
options={
'permissions': [('member_team', 'Has all roles assigned to this team')],
},
),
]
93 changes: 93 additions & 0 deletions test_app/migrations/0002_more_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Generated by Django 4.2.8 on 2024-01-16 07:49

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('ansible_base', '0011_RBAC'),
('test_app', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='ExampleEvent',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64, unique=True)),
],
),
migrations.CreateModel(
name='InstanceGroup',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=512)),
],
options={
'default_permissions': ('change', 'delete', 'view'),
},
),
migrations.AddField(
model_name='team',
name='singleton_roles',
field=models.ManyToManyField(to='ansible_base.roledefinition'),
),
migrations.AddField(
model_name='user',
name='singleton_roles',
field=models.ManyToManyField(related_name='singleton_users', to='ansible_base.roledefinition'),
),
migrations.CreateModel(
name='Namespace',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64, unique=True)),
('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.organization')),
],
),
migrations.CreateModel(
name='Inventory',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=512)),
('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.organization')),
],
options={
'permissions': [('update_inventory', 'Do inventory updates')],
},
),
migrations.CreateModel(
name='EncryptionModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(default=None, editable=False, help_text='The date/time this resource was created')),
('modified_on', models.DateTimeField(default=None, editable=False, help_text='The date/time this resource was created')),
('name', models.CharField(help_text='The name of this resource', max_length=512)),
('testing1', models.CharField(default='a', max_length=1, null=True)),
('testing2', models.CharField(default='b', max_length=1, null=True)),
('created_by', models.ForeignKey(default=None, editable=False, help_text='The user who created this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_created+', to=settings.AUTH_USER_MODEL)),
('modified_by', models.ForeignKey(default=None, editable=False, help_text='The user who last modified this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_modified+', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='CollectionImport',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64, unique=True)),
('namespace', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.namespace')),
],
),
migrations.AddField(
model_name='organization',
name='teams',
field=models.ManyToManyField(help_text='The list of teams in this organization.', related_name='organizations', to='test_app.team'),
),
migrations.AddField(
model_name='organization',
name='users',
field=models.ManyToManyField(help_text='The list of users in this organization.', related_name='organizations', to=settings.AUTH_USER_MODEL),
),
]
40 changes: 0 additions & 40 deletions test_app/migrations/0002_team_organization.py

This file was deleted.

Loading

0 comments on commit 752e0c6

Please sign in to comment.