2024-11-12 12:27:04 +00:00
|
|
|
import os.path
|
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
from database import db
|
2024-11-12 01:33:31 +00:00
|
|
|
from models import Organization
|
|
|
|
|
|
|
|
|
|
|
|
class OrganizationService:
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_organization(name: str, username: str, full_name: str, email: str, public_key: str) -> Organization:
|
2024-11-12 01:33:31 +00:00
|
|
|
from services import UserService
|
2024-11-13 02:49:43 +00:00
|
|
|
user = UserService().get_user_by_username(username)
|
2024-11-12 01:33:31 +00:00
|
|
|
if not user:
|
2024-11-13 02:49:43 +00:00
|
|
|
user = UserService().create_user(username, full_name, email, public_key)
|
2024-11-12 01:33:31 +00:00
|
|
|
|
2024-11-12 12:27:04 +00:00
|
|
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
repos = os.path.join(project_root, "repository")
|
|
|
|
if not os.path.exists(os.path.join(repos, name)):
|
|
|
|
os.mkdir(os.path.join(repos, name))
|
|
|
|
|
2024-11-12 01:33:31 +00:00
|
|
|
organization = Organization(
|
|
|
|
name=name,
|
|
|
|
owner_id=user.id,
|
|
|
|
owner=user
|
|
|
|
)
|
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
db.add(organization)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(organization)
|
2024-11-12 01:33:31 +00:00
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
UserService().add_org_to_user(user, organization)
|
2024-11-15 01:18:23 +00:00
|
|
|
UserService().add_public_key_to_user(user, organization, public_key)
|
2024-11-12 01:33:31 +00:00
|
|
|
|
|
|
|
return organization
|
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_organization(org_id: int) -> Organization | None:
|
|
|
|
return db.query(Organization).filter(Organization.id == org_id).first()
|
2024-11-12 01:33:31 +00:00
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_organization_by_name(name: str) -> Organization | None:
|
|
|
|
return db.query(Organization).filter(Organization.name == name).first()
|
2024-11-12 01:33:31 +00:00
|
|
|
|
2024-11-13 02:49:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def list_organizations():
|
|
|
|
return db.query(Organization).all()
|