Skip to content

Commit

Permalink
Add organization and team models (ansible#2094)
Browse files Browse the repository at this point in the history
* Add organization and team models

* 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.
* Team model has a one-to-one relationship with the standard Group model.
* When new Team record is created, a related Group model is
  automatically created as well.

No-Issue

* Create related teams for manually created groups

And drop group prefix for the default organization.

No-Issue
  • Loading branch information
cutwater authored Mar 12, 2024
1 parent a5cabc1 commit 62e1476
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 45 deletions.
24 changes: 12 additions & 12 deletions galaxy_ng/app/api/resource_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
ServiceAPIConfig,
SharedResource,
)
from ansible_base.resource_registry.shared_types import UserType, TeamType
from ansible_base.resource_registry.shared_types import OrganizationType, TeamType, UserType

from galaxy_ng.app import models


Expand All @@ -14,18 +15,17 @@ class APIConfig(ServiceAPIConfig):
RESOURCE_LIST = (
ResourceConfig(
models.auth.User,
shared_resource=SharedResource(
serializer=UserType,
is_provider=False
),
name_field="username"
shared_resource=SharedResource(serializer=UserType, is_provider=False),
name_field="username",
),
ResourceConfig(
models.Team,
shared_resource=SharedResource(serializer=TeamType, is_provider=True),
name_field="name",
),
ResourceConfig(
models.auth.Group,
shared_resource=SharedResource(
serializer=TeamType,
is_provider=True
),
name_field="name"
models.Organization,
shared_resource=SharedResource(serializer=OrganizationType, is_provider=False),
name_field="name",
),
)
176 changes: 176 additions & 0 deletions galaxy_ng/app/migrations/0049_organization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# 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,
),
),
(
"group",
models.OneToOneField(
help_text="Related group record.",
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="galaxy.group",
),

)
],
options={
"ordering": ("organization__name", "name"),
"abstract": False,
"unique_together": {("organization", "name")},
},
),
]
48 changes: 48 additions & 0 deletions galaxy_ng/app/migrations/0050_organization_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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()
org = Organization.objects.using(db_alias).create(
name=settings.DEFAULT_ORGANIZATION_NAME,
description="A default organization.",
created_on=now,
modified_on=now,
)

schema_editor.execute(
"""
INSERT INTO galaxy_team (name, description, created_on, modified_on, group_id, organization_id)
SELECT grp.name, '', now(), now(), grp.id, %s
FROM auth_group AS grp
""",
(org.id,),
)


def delete_default_organization(apps, schema_editor):
db_alias = schema_editor.connection.alias
Team = apps.get_model("galaxy", "Team")
Organization = apps.get_model("galaxy", "Organization")

Team.objects.using(db_alias).delete()

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",
)
Loading

0 comments on commit 62e1476

Please sign in to comment.