forked from sr2/cloud-api
minor: ruff formatter
All changes are either: - Correcting tabs - Adding/removing line breaks - Adding trailing commas
This commit is contained in:
parent
b2e5dd2ebb
commit
c689ac1e10
91 changed files with 1710 additions and 689 deletions
|
|
@ -1,3 +1,3 @@
|
|||
"""
|
||||
Configurations for the organisation module
|
||||
"""
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ Classes:
|
|||
- Status(StrEnum): PARTIAL, SUBMITTED, REMEDIATION, APPROVED, REJECTED, REMOVED
|
||||
- ContactType(StrEnum): BILLING, SECURITY, OWNER
|
||||
"""
|
||||
|
||||
from enum import StrEnum, auto
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ Exports:
|
|||
- org_model_query_dependency: org_model: Gets org model from db, if it exists. Uses org_id from query param. Also verifies if the org has been approved.
|
||||
- org_model_body_dependency: org_model: Gets org model from db, if it exists. Uses org_id from request body. Also verifies if the org has been approved.
|
||||
"""
|
||||
|
||||
from typing import Annotated, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
|
|
@ -25,25 +26,40 @@ def get_org_model(db: Session, request: Request, org_id: int):
|
|||
|
||||
root = "/api/v1"
|
||||
|
||||
pre_approval_endpoints = [f"PATCH{root}/org/status", f"PATCH{root}/org/questionnaire", f"GET{root}/org", f"GET{root}/org/contact", f"PATCH{root}/org/contact"]
|
||||
pre_approval_endpoints = [
|
||||
f"PATCH{root}/org/status",
|
||||
f"PATCH{root}/org/questionnaire",
|
||||
f"GET{root}/org",
|
||||
f"GET{root}/org/contact",
|
||||
f"PATCH{root}/org/contact",
|
||||
]
|
||||
current_request = f"{request.method}{request.url.path}"
|
||||
if current_request not in pre_approval_endpoints and org_model.status != OrgStatus.APPROVED:
|
||||
if (
|
||||
current_request not in pre_approval_endpoints
|
||||
and org_model.status != OrgStatus.APPROVED
|
||||
):
|
||||
raise AwaitingApprovalException(org_id)
|
||||
|
||||
return org_model
|
||||
|
||||
|
||||
def get_org_model_query(db: db_dependency, request: Request, org_id: Annotated[int, Query(gt=0)]) -> type[Org]:
|
||||
def get_org_model_query(
|
||||
db: db_dependency, request: Request, org_id: Annotated[int, Query(gt=0)]
|
||||
) -> type[Org]:
|
||||
return get_org_model(db, request, org_id)
|
||||
|
||||
|
||||
org_model_query_dependency = Annotated[type[Org], Depends(get_org_model_query)]
|
||||
|
||||
|
||||
def get_org_model_body(db: db_dependency, request: Request, request_model: OrgIDMixin) -> type[Org]:
|
||||
def get_org_model_body(
|
||||
db: db_dependency, request: Request, request_model: OrgIDMixin
|
||||
) -> type[Org]:
|
||||
org_id: Optional[int] = getattr(request_model, "organisation_id", None)
|
||||
if org_id is None:
|
||||
raise OrgNotFoundException
|
||||
|
||||
return get_org_model(db, request, org_id)
|
||||
|
||||
|
||||
org_model_body_dependency = Annotated[type[Org], Depends(get_org_model_body)]
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ Exceptions:
|
|||
- OrgNotFoundException: Takes an optional org_id int
|
||||
- AwaitingApprovalException: Takes an optional org_id int
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
|
|
@ -12,15 +13,24 @@ from fastapi import HTTPException, status
|
|||
|
||||
class OrgNotFoundException(HTTPException):
|
||||
def __init__(self, org_id: Optional[int] = None) -> None:
|
||||
detail = "Organisation not found" if org_id is None else f"Organisation with ID '{org_id}' was not found."
|
||||
detail = (
|
||||
"Organisation not found"
|
||||
if org_id is None
|
||||
else f"Organisation with ID '{org_id}' was not found."
|
||||
)
|
||||
super().__init__(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=detail,
|
||||
)
|
||||
|
||||
|
||||
class AwaitingApprovalException(HTTPException):
|
||||
def __init__(self, org_id: Optional[int] = None) -> None:
|
||||
detail = "Organisation has not been approved." if org_id is None else f"Organisation with ID '{org_id}' has not been approved."
|
||||
detail = (
|
||||
"Organisation has not been approved."
|
||||
if org_id is None
|
||||
else f"Organisation with ID '{org_id}' has not been approved."
|
||||
)
|
||||
super().__init__(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=detail,
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ Models:
|
|||
- owner_contact_rel: ORM relationship to Contact with owner_contact FK
|
||||
- OrgUsers: org_id[FK][PK], user_id[FK][PK]
|
||||
"""
|
||||
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, JSON
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
|
|
@ -34,9 +35,7 @@ class Organisation(Base):
|
|||
owner_contact_id = Column(Integer, ForeignKey("contact.id"))
|
||||
|
||||
user_rel = relationship(
|
||||
"User",
|
||||
secondary="orgusers",
|
||||
back_populates="organisation_rel"
|
||||
"User", secondary="orgusers", back_populates="organisation_rel"
|
||||
)
|
||||
|
||||
group_rel = relationship("Group", back_populates="org_rel")
|
||||
|
|
@ -54,5 +53,9 @@ class Organisation(Base):
|
|||
class OrgUsers(Base):
|
||||
__tablename__ = "orgusers"
|
||||
|
||||
org_id = Column(Integer, ForeignKey("organisation.id", ondelete="CASCADE"), primary_key=True)
|
||||
user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True)
|
||||
org_id = Column(
|
||||
Integer, ForeignKey("organisation.id", ondelete="CASCADE"), primary_key=True
|
||||
)
|
||||
user_id = Column(
|
||||
Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True
|
||||
)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ Endpoints:
|
|||
- [GET](/org/contact): [root user]: Gets the (contact_type) contact for an org(id)
|
||||
- [PATCH](/org/contact): [root user]: Updates the (contact_type) contact for an org(id). Any number of details can be changed.
|
||||
"""
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, status
|
||||
|
|
@ -28,17 +29,40 @@ from src.contact.models import Contact
|
|||
from src.contact.schemas import ContactAddress
|
||||
from src.contact.exceptions import ContactNotFoundException
|
||||
from src.database import db_dependency
|
||||
from src.user.dependencies import user_model_body_dependency, user_model_claims_dependency
|
||||
from src.auth.dependencies import super_admin_dependency, org_model_root_claim_query_dependency, org_model_root_claim_body_dependency
|
||||
from src.user.dependencies import (
|
||||
user_model_body_dependency,
|
||||
user_model_claims_dependency,
|
||||
)
|
||||
from src.auth.dependencies import (
|
||||
super_admin_dependency,
|
||||
org_model_root_claim_query_dependency,
|
||||
org_model_root_claim_body_dependency,
|
||||
)
|
||||
|
||||
from src.organisation.dependencies import org_model_body_dependency
|
||||
from src.organisation.constants import ContactType
|
||||
from src.organisation.models import Organisation as Org
|
||||
from src.organisation.schemas import OrgPostOrgRequest, OrgPatchQuestionnaireRequest, OrgPatchStatusRequest, \
|
||||
OrgPatchContactRequest, \
|
||||
OrgPostUserRequest, OrgGetUserResponse, OrgGetContactResponse, OrgGetOrgResponse, OrgPatchRootRequest, \
|
||||
OrgGetGroupResponse, OrgDeleteUserRequest, OrgDeleteOrgRequest, OrgPostOrgResponse, OrgPatchQuestionnaireResponse, \
|
||||
OrgPatchStatusResponse, OrgPostUserResponse, OrgPatchRootResponse, Questionnaire, OrgPatchContactResponse
|
||||
from src.organisation.schemas import (
|
||||
OrgPostOrgRequest,
|
||||
OrgPatchQuestionnaireRequest,
|
||||
OrgPatchStatusRequest,
|
||||
OrgPatchContactRequest,
|
||||
OrgPostUserRequest,
|
||||
OrgGetUserResponse,
|
||||
OrgGetContactResponse,
|
||||
OrgGetOrgResponse,
|
||||
OrgPatchRootRequest,
|
||||
OrgGetGroupResponse,
|
||||
OrgDeleteUserRequest,
|
||||
OrgDeleteOrgRequest,
|
||||
OrgPostOrgResponse,
|
||||
OrgPatchQuestionnaireResponse,
|
||||
OrgPatchStatusResponse,
|
||||
OrgPostUserResponse,
|
||||
OrgPatchRootResponse,
|
||||
Questionnaire,
|
||||
OrgPatchContactResponse,
|
||||
)
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/org",
|
||||
|
|
@ -46,16 +70,22 @@ router = APIRouter(
|
|||
)
|
||||
|
||||
|
||||
@router.get("",
|
||||
summary="Get org details by ID.",
|
||||
response_model=OrgGetOrgResponse,
|
||||
status_code=status.HTTP_200_OK,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval from database"},
|
||||
status.HTTP_404_NOT_FOUND: {"description": "Organisation not found"},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Missing or invalid org_id query parameter"},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
})
|
||||
@router.get(
|
||||
"",
|
||||
summary="Get org details by ID.",
|
||||
response_model=OrgGetOrgResponse,
|
||||
status_code=status.HTTP_200_OK,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval from database"},
|
||||
status.HTTP_404_NOT_FOUND: {"description": "Organisation not found"},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Missing or invalid org_id query parameter"
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def get_org_by_id(org_model: org_model_root_claim_query_dependency):
|
||||
"""
|
||||
Returns organisation details including key member email addresses
|
||||
|
|
@ -68,23 +98,35 @@ async def get_org_by_id(org_model: org_model_root_claim_query_dependency):
|
|||
"billing_contact": org_model.billing_contact_rel.email,
|
||||
"security_contact": org_model.security_contact_rel.email,
|
||||
"root_user": org_model.root_user_email,
|
||||
"intake_questionnaire": org_model.intake_questionnaire
|
||||
"intake_questionnaire": org_model.intake_questionnaire,
|
||||
}
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@router.post("",
|
||||
summary="Create new organisation.",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=OrgPostOrgResponse,
|
||||
responses={
|
||||
status.HTTP_201_CREATED: {"description": "Successfully created organisation."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "User must be logged in with OIDC to create organisation."},
|
||||
status.HTTP_409_CONFLICT: {"description": "Organisation with this name already exists."},
|
||||
})
|
||||
async def create_org(db: db_dependency, user_model: user_model_claims_dependency, request_model: OrgPostOrgRequest):
|
||||
@router.post(
|
||||
"",
|
||||
summary="Create new organisation.",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=OrgPostOrgResponse,
|
||||
responses={
|
||||
status.HTTP_201_CREATED: {"description": "Successfully created organisation."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "User must be logged in with OIDC to create organisation."
|
||||
},
|
||||
status.HTTP_409_CONFLICT: {
|
||||
"description": "Organisation with this name already exists."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def create_org(
|
||||
db: db_dependency,
|
||||
user_model: user_model_claims_dependency,
|
||||
request_model: OrgPostOrgRequest,
|
||||
):
|
||||
"""
|
||||
Creates a new organisation with optional questionnaire (to be completed or submitted).
|
||||
ALl organisations are given the "partial" status on creation. See update_questionnaire() for more details.
|
||||
|
|
@ -102,11 +144,17 @@ async def create_org(db: db_dependency, user_model: user_model_claims_dependency
|
|||
db.flush()
|
||||
except IntegrityError as e:
|
||||
if isinstance(e.orig, UniqueViolation):
|
||||
raise ConflictException(message="Organisation with this name already exists")
|
||||
raise ConflictException(
|
||||
message="Organisation with this name already exists"
|
||||
)
|
||||
# Adds currently logged-in user to org users list and sets them as root_user
|
||||
org_model.user_rel.append(user_model)
|
||||
org_model.root_user_rel = user_model
|
||||
for contact_type in ["billing_contact_id", "security_contact_id", "owner_contact_id"]:
|
||||
for contact_type in [
|
||||
"billing_contact_id",
|
||||
"security_contact_id",
|
||||
"owner_contact_id",
|
||||
]:
|
||||
contact_model = Contact(org_id=org_model.id)
|
||||
db.add(contact_model)
|
||||
db.flush()
|
||||
|
|
@ -116,16 +164,26 @@ async def create_org(db: db_dependency, user_model: user_model_claims_dependency
|
|||
return response
|
||||
|
||||
|
||||
@router.patch("/questionnaire",
|
||||
summary="Update questionnaire.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchQuestionnaireResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully updated questionnaire."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
})
|
||||
async def update_questionnaire(db: db_dependency, org_model: org_model_root_claim_body_dependency, request_model: OrgPatchQuestionnaireRequest):
|
||||
@router.patch(
|
||||
"/questionnaire",
|
||||
summary="Update questionnaire.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchQuestionnaireResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully updated questionnaire."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def update_questionnaire(
|
||||
db: db_dependency,
|
||||
org_model: org_model_root_claim_body_dependency,
|
||||
request_model: OrgPatchQuestionnaireRequest,
|
||||
):
|
||||
"""
|
||||
Route for updating questionnaire.
|
||||
The partial bool allows for submission of partially completed questionnaire and/or
|
||||
|
|
@ -150,16 +208,29 @@ async def update_questionnaire(db: db_dependency, org_model: org_model_root_clai
|
|||
return response
|
||||
|
||||
|
||||
@router.patch("/status",
|
||||
summary="Update status of organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchStatusResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully updated organisation status."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be super admin."},
|
||||
})
|
||||
async def update_status(db: db_dependency, org_model: org_model_body_dependency, su: super_admin_dependency, request_model: OrgPatchStatusRequest):
|
||||
@router.patch(
|
||||
"/status",
|
||||
summary="Update status of organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchStatusResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {
|
||||
"description": "Successfully updated organisation status."
|
||||
},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be super admin."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def update_status(
|
||||
db: db_dependency,
|
||||
org_model: org_model_body_dependency,
|
||||
su: super_admin_dependency,
|
||||
request_model: OrgPatchStatusRequest,
|
||||
):
|
||||
"""
|
||||
Sets an organisation's status. This is the endpoint for approving or denying an organisation after reviewing the questionnaire.
|
||||
"""
|
||||
|
|
@ -170,33 +241,57 @@ async def update_status(db: db_dependency, org_model: org_model_body_dependency,
|
|||
return response
|
||||
|
||||
|
||||
@router.get("/users",
|
||||
summary="Get email addresses of users of the organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgGetUserResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval of users."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Org ID missing or invalid."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
})
|
||||
@router.get(
|
||||
"/users",
|
||||
summary="Get email addresses of users of the organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgGetUserResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval of users."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Org ID missing or invalid."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def get_users(org_model: org_model_root_claim_query_dependency):
|
||||
"""
|
||||
Returns a list of the email addresses of all users of the organisation.
|
||||
"""
|
||||
return {"users": [user.email for user in org_model.user_rel], "organisation": org_model}
|
||||
return {
|
||||
"users": [user.email for user in org_model.user_rel],
|
||||
"organisation": org_model,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/user",
|
||||
summary="Add user to the organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPostUserResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully added user to the organisation."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
status.HTTP_409_CONFLICT: {"description": "User is already a member of the organisation."},
|
||||
})
|
||||
async def add_user_to_org(db: db_dependency, org_model: org_model_root_claim_body_dependency, user_model: user_model_body_dependency, request_model: OrgPostUserRequest):
|
||||
@router.post(
|
||||
"/user",
|
||||
summary="Add user to the organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPostUserResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {
|
||||
"description": "Successfully added user to the organisation."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
status.HTTP_409_CONFLICT: {
|
||||
"description": "User is already a member of the organisation."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def add_user_to_org(
|
||||
db: db_dependency,
|
||||
org_model: org_model_root_claim_body_dependency,
|
||||
user_model: user_model_body_dependency,
|
||||
request_model: OrgPostUserRequest,
|
||||
):
|
||||
"""
|
||||
Adds a user to the organisation.
|
||||
"""
|
||||
|
|
@ -209,15 +304,28 @@ async def add_user_to_org(db: db_dependency, org_model: org_model_root_claim_bod
|
|||
return response
|
||||
|
||||
|
||||
@router.delete("",
|
||||
summary="Delete organisation from the hub.",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
responses={
|
||||
status.HTTP_204_NO_CONTENT: {"description": "Successfully deleted organisation."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be super admin."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Org ID missing or invalid."},
|
||||
})
|
||||
async def delete_organisation_by_id(db: db_dependency, org_model: org_model_body_dependency, su: super_admin_dependency, request_model: OrgDeleteOrgRequest):
|
||||
@router.delete(
|
||||
"",
|
||||
summary="Delete organisation from the hub.",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
responses={
|
||||
status.HTTP_204_NO_CONTENT: {
|
||||
"description": "Successfully deleted organisation."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be super admin."
|
||||
},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Org ID missing or invalid."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def delete_organisation_by_id(
|
||||
db: db_dependency,
|
||||
org_model: org_model_body_dependency,
|
||||
su: super_admin_dependency,
|
||||
request_model: OrgDeleteOrgRequest,
|
||||
):
|
||||
"""
|
||||
Removes an organisation from the hub.
|
||||
"""
|
||||
|
|
@ -225,37 +333,59 @@ async def delete_organisation_by_id(db: db_dependency, org_model: org_model_body
|
|||
db.commit()
|
||||
|
||||
|
||||
@router.patch("/root_user",
|
||||
summary="Update the root user of the organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchRootResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully updated root user."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be super admin."},
|
||||
})
|
||||
async def update_root_user(db: db_dependency, org_model: org_model_body_dependency, user_model: user_model_body_dependency, su: super_admin_dependency, request_model: OrgPatchRootRequest):
|
||||
@router.patch(
|
||||
"/root_user",
|
||||
summary="Update the root user of the organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchRootResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully updated root user."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be super admin."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def update_root_user(
|
||||
db: db_dependency,
|
||||
org_model: org_model_body_dependency,
|
||||
user_model: user_model_body_dependency,
|
||||
su: super_admin_dependency,
|
||||
request_model: OrgPatchRootRequest,
|
||||
):
|
||||
"""
|
||||
Promotes an existing organisation user to the root user, giving them full control of the org.
|
||||
"""
|
||||
if user_model not in org_model.user_rel:
|
||||
raise UnprocessableContentException(message="This user does not belong to your organisation.")
|
||||
raise UnprocessableContentException(
|
||||
message="This user does not belong to your organisation."
|
||||
)
|
||||
org_model.root_user_rel = user_model
|
||||
db.flush()
|
||||
response = OrgPatchRootResponse(name=org_model.name, root_user_email=org_model.root_user_email)
|
||||
response = OrgPatchRootResponse(
|
||||
name=org_model.name, root_user_email=org_model.root_user_email
|
||||
)
|
||||
db.commit()
|
||||
return response
|
||||
|
||||
|
||||
@router.get("/groups",
|
||||
summary="Get all organisation IAM groups.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgGetGroupResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval of IAM groups."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Org ID missing or invalid."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
})
|
||||
@router.get(
|
||||
"/groups",
|
||||
summary="Get all organisation IAM groups.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgGetGroupResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval of IAM groups."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Org ID missing or invalid."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def get_org_groups(org_model: org_model_root_claim_query_dependency):
|
||||
"""
|
||||
Returns a list of the names of all IAM groups created by the organisation.
|
||||
|
|
@ -263,15 +393,26 @@ async def get_org_groups(org_model: org_model_root_claim_query_dependency):
|
|||
return {"groups": [group.name for group in org_model.group_rel]}
|
||||
|
||||
|
||||
@router.delete("/user",
|
||||
summary="Remove user from organisation.",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
responses={
|
||||
status.HTTP_204_NO_CONTENT: {"description": "Successfully removed user."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
})
|
||||
async def remove_user_from_org(db: db_dependency, org_model: org_model_root_claim_body_dependency, user_model: user_model_body_dependency, request_model: OrgDeleteUserRequest):
|
||||
@router.delete(
|
||||
"/user",
|
||||
summary="Remove user from organisation.",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
responses={
|
||||
status.HTTP_204_NO_CONTENT: {"description": "Successfully removed user."},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def remove_user_from_org(
|
||||
db: db_dependency,
|
||||
org_model: org_model_root_claim_body_dependency,
|
||||
user_model: user_model_body_dependency,
|
||||
request_model: OrgDeleteUserRequest,
|
||||
):
|
||||
"""
|
||||
Revokes a user's membership in an organisation.
|
||||
"""
|
||||
|
|
@ -282,16 +423,27 @@ async def remove_user_from_org(db: db_dependency, org_model: org_model_root_clai
|
|||
db.commit()
|
||||
|
||||
|
||||
@router.get("/contact",
|
||||
summary="Get contact for organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgGetContactResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval of contact."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
})
|
||||
async def get_contact(org_model: org_model_root_claim_query_dependency, contact_type: Annotated[ContactType, Query(description="Must be billing|security|owner")]):
|
||||
@router.get(
|
||||
"/contact",
|
||||
summary="Get contact for organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgGetContactResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successful retrieval of contact."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def get_contact(
|
||||
org_model: org_model_root_claim_query_dependency,
|
||||
contact_type: Annotated[
|
||||
ContactType, Query(description="Must be billing|security|owner")
|
||||
],
|
||||
):
|
||||
"""
|
||||
Gets full details for a contact point at an organisation.
|
||||
"""
|
||||
|
|
@ -309,21 +461,33 @@ async def get_contact(org_model: org_model_root_claim_query_dependency, contact_
|
|||
raise ContactNotFoundException()
|
||||
|
||||
address = ContactAddress.model_validate(contact_model)
|
||||
contact_response = ContactModel.model_construct(**contact_model.__dict__, address=address)
|
||||
contact_response = ContactModel.model_construct(
|
||||
**contact_model.__dict__, address=address
|
||||
)
|
||||
|
||||
return {"contact": contact_response, "organisation": org_model}
|
||||
|
||||
|
||||
@router.patch("/contact",
|
||||
summary="Update contact for organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchContactResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully updated contact."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {"description": "Invalid data in request."},
|
||||
status.HTTP_401_UNAUTHORIZED: {"description": "Not authorised. Must be org root user."},
|
||||
})
|
||||
async def update_contact(db: db_dependency, org_model: org_model_root_claim_body_dependency, request_model: OrgPatchContactRequest):
|
||||
@router.patch(
|
||||
"/contact",
|
||||
summary="Update contact for organisation.",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=OrgPatchContactResponse,
|
||||
responses={
|
||||
status.HTTP_200_OK: {"description": "Successfully updated contact."},
|
||||
status.HTTP_422_UNPROCESSABLE_CONTENT: {
|
||||
"description": "Invalid data in request."
|
||||
},
|
||||
status.HTTP_401_UNAUTHORIZED: {
|
||||
"description": "Not authorised. Must be org root user."
|
||||
},
|
||||
},
|
||||
)
|
||||
async def update_contact(
|
||||
db: db_dependency,
|
||||
org_model: org_model_root_claim_body_dependency,
|
||||
request_model: OrgPatchContactRequest,
|
||||
):
|
||||
"""
|
||||
Updates details for a contact point at an organisation.
|
||||
"""
|
||||
|
|
@ -351,7 +515,9 @@ async def update_contact(db: db_dependency, org_model: org_model_root_claim_body
|
|||
db.flush()
|
||||
|
||||
address = ContactAddress.model_validate(contact_model)
|
||||
contact_response = ContactModel.model_construct(**contact_model.__dict__, address=address)
|
||||
contact_response = ContactModel.model_construct(
|
||||
**contact_model.__dict__, address=address
|
||||
)
|
||||
|
||||
db.commit()
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ Models follow the nomenclature of:
|
|||
- Mixins: "<Attribute>Mixin"
|
||||
- Models: "<Module><Method><Resource><Opt:Resource><Direction>" ie "OrgPostOrgRequest"
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import EmailStr, ConfigDict, Field
|
||||
|
|
@ -20,11 +21,13 @@ from src.organisation.constants import Status, ContactType
|
|||
class OrgIDMixin(CustomBaseModel):
|
||||
organisation_id: int = Field(gt=0)
|
||||
|
||||
|
||||
class Questionnaire(CustomBaseModel):
|
||||
question_one: Optional[str] = None
|
||||
question_two: Optional[str] = None
|
||||
question_three: Optional[str] = None
|
||||
|
||||
|
||||
class OrgSchema(CustomBaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
|
@ -34,26 +37,32 @@ class OrgPostOrgRequest(CustomBaseModel):
|
|||
name: str
|
||||
intake_questionnaire: Optional[Questionnaire] = None
|
||||
|
||||
|
||||
class OrgPostOrgResponse(CustomBaseModel):
|
||||
name: str
|
||||
status: Status
|
||||
|
||||
|
||||
class OrgPatchQuestionnaireRequest(OrgIDMixin):
|
||||
intake_questionnaire: Questionnaire
|
||||
partial: bool
|
||||
|
||||
|
||||
class OrgPatchQuestionnaireResponse(CustomBaseModel):
|
||||
name: str
|
||||
intake_questionnaire: Questionnaire
|
||||
status: Status
|
||||
|
||||
|
||||
class OrgPatchStatusRequest(OrgIDMixin):
|
||||
status: Status
|
||||
|
||||
|
||||
class OrgPatchStatusResponse(CustomBaseModel):
|
||||
name: str
|
||||
status: Status
|
||||
|
||||
|
||||
class OrgPatchContactRequest(OrgIDMixin):
|
||||
contact_type: ContactType
|
||||
|
||||
|
|
@ -70,41 +79,51 @@ class OrgPatchContactRequest(OrgIDMixin):
|
|||
country_code: Optional[str] = None
|
||||
postal_code: Optional[str] = None
|
||||
|
||||
|
||||
class OrgPostUserRequest(OrgIDMixin, UserIDMixin):
|
||||
pass
|
||||
|
||||
|
||||
class OrgPostUserResponse(CustomBaseModel):
|
||||
users: list[str]
|
||||
|
||||
|
||||
class OrgDeleteUserRequest(OrgIDMixin, UserIDMixin):
|
||||
pass
|
||||
|
||||
|
||||
class OrgPatchRootRequest(OrgIDMixin, UserIDMixin):
|
||||
pass
|
||||
|
||||
|
||||
class OrgPatchRootResponse(CustomBaseModel):
|
||||
name: str
|
||||
root_user_email: str
|
||||
|
||||
|
||||
class OrgGetUserResponse(CustomBaseModel):
|
||||
users: list[str]
|
||||
organisation: OrgSchema
|
||||
|
||||
|
||||
class OrgGetGroupResponse(CustomBaseModel):
|
||||
groups: list[str]
|
||||
|
||||
|
||||
class OrgGetContactResponse(CustomBaseModel):
|
||||
model_config = ConfigDict(from_attributes=True, extra="ignore")
|
||||
|
||||
contact: ContactModel
|
||||
organisation: OrgSchema
|
||||
|
||||
|
||||
class OrgPatchContactResponse(CustomBaseModel):
|
||||
model_config = ConfigDict(from_attributes=True, extra="ignore")
|
||||
|
||||
contact: ContactModel
|
||||
organisation: OrgSchema
|
||||
|
||||
|
||||
class OrgGetOrgResponse(CustomBaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
|
@ -115,5 +134,6 @@ class OrgGetOrgResponse(CustomBaseModel):
|
|||
security_contact: Optional[str] = None
|
||||
intake_questionnaire: Optional[Questionnaire] = None
|
||||
|
||||
|
||||
class OrgDeleteOrgRequest(OrgIDMixin):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
"""
|
||||
Reusable business logic functions for the organisation module
|
||||
"""
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
"""
|
||||
Non-business logic reusable functions and classes for the organisation module
|
||||
"""
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue