ki-backend/ki/models.py

231 lines
7.6 KiB
Python
Raw Permalink Normal View History

2021-07-05 19:37:05 +02:00
# SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
2021-06-07 17:26:07 +02:00
from datetime import datetime
2021-07-02 16:26:36 +02:00
from sqlalchemy import Boolean, Column, Integer, SmallInteger, String, DateTime, ForeignKey
2021-06-06 22:25:10 +02:00
from sqlalchemy.orm import relationship
from app import db
class User(db.Model):
__tablename__ = "user"
2021-06-20 19:25:27 +02:00
id = Column(Integer, primary_key=True)
auth_id = Column(String(50), nullable=False, unique=True)
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=True)
2021-06-22 17:35:27 +02:00
tokens = relationship("Token", back_populates="user")
2021-06-20 19:25:27 +02:00
profile = relationship("Profile", back_populates="user")
def to_dict(self):
return {"id": self.id}
class Profile(db.Model):
__tablename__ = "profile"
2021-06-06 22:25:10 +02:00
id = Column(Integer, primary_key=True)
nickname = Column(String(25), unique=True, nullable=False)
pronouns = Column(String(25), default="")
volunteerwork = Column(String(4000), default="")
freetext = Column(String(4000), default="")
2021-10-04 18:10:31 +02:00
availability_status = Column(Boolean, default=False)
availability_text = Column(String(4000), default="")
availability_hours_per_week = Column(Integer, default=0)
2021-10-04 18:10:31 +02:00
2021-07-02 16:26:36 +02:00
visible = Column(Boolean, nullable=False, default=False)
2021-06-12 13:24:26 +02:00
created = Column(DateTime, nullable=False, default=datetime.now)
2021-06-27 13:38:16 +02:00
updated = Column(DateTime, onupdate=datetime.now, nullable=False, default=datetime.now)
2021-06-06 22:25:10 +02:00
2021-06-20 19:25:27 +02:00
user = relationship("User", back_populates="profile", uselist=False)
2021-06-07 17:26:07 +02:00
contacts = relationship("Contact")
2021-06-20 19:25:27 +02:00
address = relationship("Address", uselist=False, back_populates="profile")
skills = relationship("ProfileSkill", back_populates="profile")
2021-07-05 20:39:49 +02:00
searchtopics = relationship("ProfileSearchtopic", back_populates="profile")
2021-06-20 19:25:27 +02:00
languages = relationship("ProfileLanguage", back_populates="profile")
2021-06-07 17:26:07 +02:00
2021-06-13 19:41:32 +02:00
def to_dict(self):
return {
2021-06-26 12:16:14 +02:00
"user_id": self.user.id,
2021-06-20 19:25:27 +02:00
"nickname": self.nickname,
"pronouns": self.pronouns,
"volunteerwork": self.volunteerwork,
"availability_status": self.availability_status,
"availability_text": self.availability_text,
"availability_hours_per_week": self.availability_hours_per_week,
2021-06-26 12:16:14 +02:00
"freetext": self.freetext,
2021-07-02 16:26:36 +02:00
"visible": self.visible,
"address": self.address.to_dict() if self.address else None,
2021-06-27 13:38:16 +02:00
"contacts": list(map(lambda contact: contact.to_dict(), self.contacts)),
2021-06-26 12:16:14 +02:00
"skills": list(map(lambda skill: skill.to_dict(), self.skills)),
2021-07-05 20:39:49 +02:00
"searchtopics": list(map(lambda searchtopic: searchtopic.to_dict(), self.searchtopics)),
2021-06-27 13:38:16 +02:00
"languages": list(map(lambda language: language.to_dict(), self.languages))
2021-06-13 19:41:32 +02:00
}
2021-06-07 17:26:07 +02:00
2021-06-12 12:00:34 +02:00
class Token(db.Model):
__tablename__ = "token"
id = Column(Integer, primary_key=True)
2021-06-13 14:47:16 +02:00
user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
2021-06-12 12:00:34 +02:00
token = Column(String(36), nullable=False)
user = relationship("User", back_populates="tokens")
2021-06-26 12:16:14 +02:00
def to_dict(self):
return {"user_id": self.user_id, "token": self.token}
2021-06-12 12:00:34 +02:00
2021-06-07 17:26:07 +02:00
class Contact(db.Model):
__tablename__ = "contact"
id = Column(Integer, primary_key=True)
2021-06-20 19:25:27 +02:00
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False)
profile = relationship("Profile", back_populates="contacts")
2021-06-27 13:38:16 +02:00
contacttype_id = Column(Integer, ForeignKey("contacttype.id"), nullable=False)
2021-06-07 17:26:07 +02:00
contacttype = relationship("ContactType")
content = Column(String(200), nullable=False)
2021-06-26 12:16:14 +02:00
def to_dict(self):
return {
"id": self.id,
"profile_id": self.profile_id,
"contacttype": self.contacttype.to_dict(),
"content": self.content
}
2021-06-07 17:26:07 +02:00
class ContactType(db.Model):
__tablename__ = "contacttype"
id = Column(Integer, primary_key=True)
name = Column(String(25), nullable=False)
2021-06-06 22:25:10 +02:00
2021-06-26 12:16:14 +02:00
def to_dict(self):
return {"id": self.id, "name": self.name}
2021-06-06 22:25:10 +02:00
class Address(db.Model):
__tablename__ = "address"
id = Column(Integer, primary_key=True)
name = Column(String(25), default="")
2021-06-06 22:25:10 +02:00
street = Column(String(25), default="")
house_number = Column(String(10), default="")
additional = Column(String(25), default="")
postcode = Column(String(10), default="")
city = Column(String(25), default="")
country = Column(String(25), default="")
2021-06-20 19:25:27 +02:00
profile_id = Column(Integer, ForeignKey("profile.id"), nullable=False)
profile = relationship("Profile", back_populates="address")
2021-06-06 22:25:10 +02:00
2021-06-26 12:16:14 +02:00
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"street": self.street,
"house_number": self.house_number,
"additional": self.additional,
"postcode": self.postcode,
"city": self.city,
"country": self.country,
"profile_id": self.profile_id
}
2021-06-06 22:25:10 +02:00
class Skill(db.Model):
__tablename__ = "skill"
id = Column(Integer, primary_key=True)
2021-11-22 20:11:58 +01:00
name = Column(String(50), unique=True, nullable=False)
2021-06-06 22:25:10 +02:00
2021-06-20 19:25:27 +02:00
profiles = relationship("ProfileSkill", back_populates="skill")
2021-07-05 20:39:49 +02:00
searchtopics = relationship("ProfileSearchtopic", back_populates="skill")
2021-06-06 22:25:10 +02:00
2021-06-07 17:52:14 +02:00
def to_dict(self):
2021-06-27 13:38:16 +02:00
return {"id": self.id, "name": self.name, "icon_url": "/skills/{}/icon".format(self.id)}
2021-06-07 17:52:14 +02:00
2021-06-06 22:25:10 +02:00
2021-06-20 19:25:27 +02:00
class ProfileSkill(db.Model):
2021-08-14 12:14:01 +02:00
level1_text = "bis 6 Monate"
level2_text = "bis 1 Jahr"
level3_text = "bis 3 Jahre"
level4_text = "bis 5 Jahre"
level5_text = "mehr als 5 Jahre"
level_texts = {
1: level1_text,
2: level2_text,
3: level3_text,
4: level4_text,
5: level5_text,
}
2021-06-20 19:25:27 +02:00
__tablename__ = "profile_skill"
2021-06-12 09:46:21 +02:00
2021-06-20 19:25:27 +02:00
profile_id = Column(Integer, ForeignKey("profile.id"), primary_key=True)
2021-06-12 09:46:21 +02:00
skill_id = Column(Integer, ForeignKey("skill.id"), primary_key=True)
2021-06-12 12:00:34 +02:00
level = Column(SmallInteger, nullable=False)
2021-06-12 09:46:21 +02:00
2021-06-20 19:25:27 +02:00
profile = relationship("Profile", back_populates="skills")
skill = relationship("Skill", back_populates="profiles")
2021-06-12 09:46:21 +02:00
2021-06-26 12:16:14 +02:00
def to_dict(self):
2021-06-27 13:38:16 +02:00
return {"profile_id": self.profile_id, "skill": self.skill.to_dict(), "level": self.level}
2021-06-26 12:16:14 +02:00
2021-06-12 09:46:21 +02:00
2021-07-05 20:39:49 +02:00
class ProfileSearchtopic(db.Model):
__tablename__ = "profile_searchtopic"
profile_id = Column(Integer, ForeignKey("profile.id"), primary_key=True)
skill_id = Column(Integer, ForeignKey("skill.id"), primary_key=True)
profile = relationship("Profile", back_populates="searchtopics")
skill = relationship("Skill", back_populates="searchtopics")
def to_dict(self):
return {"profile_id": self.profile_id, "skill": self.skill.to_dict()}
2021-06-06 22:25:10 +02:00
class Language(db.Model):
__tablename__ = "language"
2021-08-02 21:44:38 +02:00
id = Column(String(4), primary_key=True)
name = Column(String(100), nullable=False)
2021-06-07 17:26:07 +02:00
2021-06-20 19:25:27 +02:00
profiles = relationship("ProfileLanguage", back_populates="language")
def to_dict(self):
2021-06-27 13:38:16 +02:00
return {"id": self.id, "name": self.name, "icon_url": "/languages/{}/icon".format(self.id)}
2021-06-12 09:46:21 +02:00
2021-06-20 19:25:27 +02:00
class ProfileLanguage(db.Model):
2021-08-14 12:14:01 +02:00
level1_text = "keine Angabe"
level2_text = "Grundkenntnisse"
level3_text = "Gut"
level4_text = "Fließend"
level5_text = "Muttersprache"
level_texts = {
1: level1_text,
2: level2_text,
3: level3_text,
4: level4_text,
5: level5_text,
}
2021-06-20 19:25:27 +02:00
__tablename__ = "profile_language"
2021-06-12 09:46:21 +02:00
2021-06-20 19:25:27 +02:00
profile_id = Column(Integer, ForeignKey("profile.id"), primary_key=True)
2021-08-02 21:44:38 +02:00
language_id = Column(String(4), ForeignKey("language.id"), primary_key=True)
2021-06-12 12:00:34 +02:00
level = Column(SmallInteger, nullable=False)
2021-06-12 09:46:21 +02:00
2021-06-20 19:25:27 +02:00
profile = relationship("Profile", back_populates="languages")
language = relationship("Language", back_populates="profiles")
2021-06-26 12:16:14 +02:00
def to_dict(self):
2021-06-27 13:38:16 +02:00
return {"profile_id": self.profile_id, "language": self.language.to_dict(), "level": self.level}