2026-04-06 12:41:49 +01:00
|
|
|
"""
|
|
|
|
|
Database models for user module
|
|
|
|
|
|
|
|
|
|
Models:
|
|
|
|
|
- User - id[pk], email, first_name, last_name, oidc_id
|
|
|
|
|
"""
|
2026-05-25 12:06:24 +01:00
|
|
|
from collections import defaultdict
|
|
|
|
|
|
2026-04-06 12:41:49 +01:00
|
|
|
from sqlalchemy import Column, Integer, String
|
2026-05-25 10:21:15 +01:00
|
|
|
from sqlalchemy.orm import relationship
|
2026-04-06 12:41:49 +01:00
|
|
|
|
|
|
|
|
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)
|
2026-05-25 10:21:15 +01:00
|
|
|
|
2026-05-25 12:06:24 +01:00
|
|
|
organisation_rel = relationship(
|
2026-05-25 10:21:15 +01:00
|
|
|
"Organisation",
|
|
|
|
|
secondary="orgusers",
|
2026-05-25 12:06:24 +01:00
|
|
|
back_populates="user_rel"
|
2026-05-25 10:21:15 +01:00
|
|
|
)
|
2026-05-25 12:06:24 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def organisations(self):
|
|
|
|
|
return [org.name 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(group.name)
|
|
|
|
|
return dict(result)
|