Merge pull request #256 from dod-ccpo/environment-users
Show how many members are in an environment
This commit is contained in:
commit
011a5371b5
@ -0,0 +1,38 @@
|
|||||||
|
"""create environment_roles table
|
||||||
|
|
||||||
|
Revision ID: c1d074288e99
|
||||||
|
Revises: 06aa23166ca9
|
||||||
|
Create Date: 2018-09-06 13:40:12.332241
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'c1d074288e99'
|
||||||
|
down_revision = '06aa23166ca9'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('environment_roles',
|
||||||
|
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
|
||||||
|
sa.Column('environment_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||||
|
sa.Column('role', sa.String(), nullable=True),
|
||||||
|
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['environment_id'], ['environments.id'], ),
|
||||||
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index('environments_role_user_environment', 'environment_roles', ['user_id', 'environment_id'], unique=True)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_index('environments_role_user_environment', table_name='environment_roles')
|
||||||
|
op.drop_table('environment_roles')
|
||||||
|
# ### end Alembic commands ###
|
@ -1,5 +1,6 @@
|
|||||||
from atst.database import db
|
from atst.database import db
|
||||||
from atst.models.environment import Environment
|
from atst.models.environment import Environment
|
||||||
|
from atst.models.environment_role import EnvironmentRole, CSPRole
|
||||||
|
|
||||||
|
|
||||||
class Environments(object):
|
class Environments(object):
|
||||||
@ -16,3 +17,13 @@ class Environments(object):
|
|||||||
environment = Environment(project=project, name=name)
|
environment = Environment(project=project, name=name)
|
||||||
db.session.add(environment)
|
db.session.add(environment)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def add_member(cls, user, environment, member):
|
||||||
|
environment_user = EnvironmentRole(
|
||||||
|
user=member, environment=environment, role=CSPRole.NONSENSE_ROLE.value
|
||||||
|
)
|
||||||
|
db.session.add(environment_user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return environment
|
||||||
|
@ -14,3 +14,11 @@ class Environment(Base, TimestampsMixin):
|
|||||||
|
|
||||||
project_id = Column(ForeignKey("projects.id"))
|
project_id = Column(ForeignKey("projects.id"))
|
||||||
project = relationship("Project")
|
project = relationship("Project")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def users(self):
|
||||||
|
return [r.user for r in self.roles]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def num_users(self):
|
||||||
|
return len(self.users)
|
||||||
|
32
atst/models/environment_role.py
Normal file
32
atst/models/environment_role.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from enum import Enum
|
||||||
|
from sqlalchemy import Index, ForeignKey, Column, String
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
|
from atst.models import Base
|
||||||
|
from .types import Id
|
||||||
|
|
||||||
|
|
||||||
|
class CSPRole(Enum):
|
||||||
|
NONSENSE_ROLE = "nonesense_role"
|
||||||
|
|
||||||
|
|
||||||
|
class EnvironmentRole(Base):
|
||||||
|
__tablename__ = "environment_roles"
|
||||||
|
|
||||||
|
id = Id()
|
||||||
|
environment_id = Column(UUID(as_uuid=True), ForeignKey("environments.id"))
|
||||||
|
environment = relationship("Environment", backref="roles")
|
||||||
|
|
||||||
|
role = Column(String())
|
||||||
|
|
||||||
|
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"))
|
||||||
|
user = relationship("User", backref="environment_roles")
|
||||||
|
|
||||||
|
|
||||||
|
Index(
|
||||||
|
"environments_role_user_environment",
|
||||||
|
EnvironmentRole.user_id,
|
||||||
|
EnvironmentRole.environment_id,
|
||||||
|
unique=True,
|
||||||
|
)
|
@ -39,7 +39,7 @@
|
|||||||
</a>
|
</a>
|
||||||
|
|
||||||
<div class='project-list-item__environment__members'>
|
<div class='project-list-item__environment__members'>
|
||||||
<div class='label'>0</div>
|
<div class='label'>{{ environment.num_users }}</div>
|
||||||
<span>members</span>
|
<span>members</span>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
18
tests/models/test_environments.py
Normal file
18
tests/models/test_environments.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
from atst.domain.environments import Environments
|
||||||
|
from atst.domain.workspaces import Workspaces
|
||||||
|
from atst.domain.projects import Projects
|
||||||
|
from tests.factories import RequestFactory, UserFactory
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_user_to_environment():
|
||||||
|
owner = UserFactory.create()
|
||||||
|
developer = UserFactory.from_atat_role("developer")
|
||||||
|
|
||||||
|
workspace = Workspaces.create(RequestFactory.create(creator=owner))
|
||||||
|
project = Projects.create(
|
||||||
|
workspace, "my test project", "It's mine.", ["dev", "staging", "prod"]
|
||||||
|
)
|
||||||
|
dev_environment = project.environments[0]
|
||||||
|
|
||||||
|
dev_environment = Environments.add_member(owner, dev_environment, developer)
|
||||||
|
assert developer in dev_environment.users
|
Loading…
x
Reference in New Issue
Block a user