forked from sr2/cloud-api
All changes are either: - Correcting tabs - Adding/removing line breaks - Adding trailing commas
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
"""
|
|
Database models for user module
|
|
|
|
Models:
|
|
- User:
|
|
- id[PK], email, first_name, last_name, oidc_id
|
|
- organisation_rel: ORM relationship to Organisation via OrgUsers table
|
|
- group_rel: ORM relationship to Group via UserGroups table
|
|
- organisations: Calc property list of organisation_rel.name
|
|
- groups: Calc property dict of {group_rel.org_rel.name: group_rel.name}
|
|
"""
|
|
|
|
from collections import defaultdict
|
|
|
|
from sqlalchemy import Column, Integer, String
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from src.database import Base
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "user"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
email = Column(String)
|
|
first_name = Column(String)
|
|
last_name = Column(String)
|
|
oidc_id = Column(String, index=True, unique=True)
|
|
|
|
organisation_rel = relationship(
|
|
"Organisation", secondary="orgusers", back_populates="user_rel"
|
|
)
|
|
|
|
@property
|
|
def organisations(self):
|
|
return [{"name": org.name, "id": org.id} for org in self.organisation_rel]
|
|
|
|
group_rel = relationship(
|
|
"Group", secondary="user_groups", back_populates="user_rel"
|
|
)
|
|
|
|
@property
|
|
def groups(self):
|
|
result = defaultdict(list)
|
|
for group in self.group_rel:
|
|
result[group.org_rel.name].append({"name": group.name, "id": group.id})
|
|
return dict(result)
|