Skip to content

Commit

Permalink
Add organization and team models
Browse files Browse the repository at this point in the history
* Add new `Oranization` and `Team` models, that implement respective abstract models from DAB.
* Add related database migrations and data migration, that creates a new default model.
* Implement custom manager for the `Organization` model that retrieves the default organization.

No-Issue
  • Loading branch information
cutwater committed Feb 16, 2024
1 parent f6619a1 commit b6d5394
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 33 deletions.
167 changes: 167 additions & 0 deletions galaxy_ng/app/migrations/0049_organization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Generated by Django 4.2.10 on 2024-02-15 17:33

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


class Migration(migrations.Migration):

dependencies = [
("galaxy", "0048_update_collection_remote_rhcertified_url"),
]

operations = [
migrations.CreateModel(
name="Organization",
fields=[
(
"id",
models.AutoField(
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, unique=True
),
),
(
"description",
models.TextField(
blank=True, 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,
),
),
(
"users",
models.ManyToManyField(
help_text="The list of users in this organization.",
related_name="organizations",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"abstract": False,
},
),
migrations.CreateModel(
name="Team",
fields=[
(
"id",
models.AutoField(
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)),
(
"description",
models.TextField(blank=True, default="", help_text="The team 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,
),
),
(
"organization",
models.ForeignKey(
help_text="The organization of this team.",
on_delete=django.db.models.deletion.CASCADE,
related_name="teams",
to="galaxy.organization",
),
),
(
"users",
models.ManyToManyField(
help_text="The list of users in this team.",
related_name="teams",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ("organization__name", "name"),
"abstract": False,
"unique_together": {("organization", "name")},
},
),
]
36 changes: 36 additions & 0 deletions galaxy_ng/app/migrations/0050_organization_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.conf import settings
from django.db import migrations
from django.utils import timezone


def create_default_organization(apps, schema_editor):
db_alias = schema_editor.connection.alias
Organization = apps.get_model("galaxy", "Organization")
now = timezone.now()
Organization.objects.using(db_alias).create(
name=settings.DEFAULT_ORGANIZATION_NAME,
description="A default organization.",
created_on=now,
modified_on=now,
)


def delete_default_organization(apps, schema_editor):
db_alias = schema_editor.connection.alias
Organization = apps.get_model("galaxy", "Organization")
Organization.objects.using(db_alias).filter(
name=settings.DEFAULT_ORGANIZATION_NAME
).delete()


class Migration(migrations.Migration):
dependencies = [
("galaxy", "0049_organization"),
]

operations = [
migrations.RunPython(
code=create_default_organization,
reverse_code=delete_default_organization,
)
]
63 changes: 30 additions & 33 deletions galaxy_ng/app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
from .auth import (
Group,
User,
)
from .collectionimport import (
CollectionImport,
)
from .aiindex import AIIndexDenyList
from .auth import Group, User
from .collectionimport import CollectionImport
from .config import Setting
from .namespace import (
Namespace,
NamespaceLink,
)

from .synclist import (
SyncList,
)

from .container import (
ContainerDistribution,
ContainerDistroReadme,
ContainerNamespace,
ContainerRegistryRemote,
ContainerRegistryRepos

ContainerRegistryRepos,
)

from .aiindex import AIIndexDenyList
from .namespace import Namespace, NamespaceLink
from .organization import Organization, Team
from .synclist import SyncList

__all__ = (
'Group',
'User',
'CollectionImport',
'Namespace',
'NamespaceLink',
'Setting',
'SyncList',
'ContainerDistribution',
'ContainerDistroReadme',
'ContainerNamespace',
'ContainerRegistryRemote',
'ContainerRegistryRepos',
'AIIndexDenyList',
# aiindex
"AIIndexDenyList",
# auth
"Group",
"User",
# collectionimport
"CollectionImport",
# config
"Setting",
# container
"ContainerDistribution",
"ContainerDistroReadme",
"ContainerNamespace",
"ContainerRegistryRemote",
"ContainerRegistryRepos",
# namespace
"Namespace",
"NamespaceLink",
# organization
"Organization",
"Team",
# synclist
"SyncList",
)
34 changes: 34 additions & 0 deletions galaxy_ng/app/models/organization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

from ansible_base.lib.abstract_models import AbstractOrganization, AbstractTeam
from django.conf import settings
from django.db import models


class OrganizationManager(models.Manager):

def get_default(self) -> Organization:
"""Return default organization."""
return self.get(name=settings.DEFAULT_ORGANIZATION_NAME)


class Organization(AbstractOrganization):
"""An organization model."""

users = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="organizations",
help_text="The list of users in this organization."
)

objects = OrganizationManager()


class Team(AbstractTeam):
"""A team model."""

users = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="teams",
help_text="The list of users in this team."
)
5 changes: 5 additions & 0 deletions galaxy_ng/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,8 @@
# When set to True will enable the DYNAMIC settungs feature
# Individual allowed dynamic keys are set on ./dynamic_settings.py
GALAXY_DYNAMIC_SETTINGS = False

ANSIBLE_BASE_ORGANIZATION_MODEL = "galaxy.Organization"

# WARNING: This setting is used in database migrations to create a default organization.
DEFAULT_ORGANIZATION_NAME = "Default"

0 comments on commit b6d5394

Please sign in to comment.