ki-backend/ki/models.py

122 lines
3.8 KiB
Python
Raw Normal View History

2021-06-07 17:26:07 +02:00
from datetime import datetime
2021-06-12 09:46:21 +02:00
from sqlalchemy import 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"
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-06-12 13:24:26 +02:00
created = Column(DateTime, nullable=False, default=datetime.now)
updated = Column(DateTime, onupdate=datetime.now, nullable=False, default=datetime.now)
auth_id = Column(String(50), nullable=False, unique=True)
2021-06-06 22:25:10 +02:00
2021-06-07 17:26:07 +02:00
contacts = relationship("Contact")
address = relationship("Address", uselist=False, back_populates="user")
2021-06-12 12:00:34 +02:00
tokens = relationship("Token", uselist=False, back_populates="user")
2021-06-12 13:24:26 +02:00
skills = relationship("UserSkill", back_populates="user")
languages = relationship("UserLanguage", back_populates="user")
2021-06-07 17:26:07 +02:00
2021-06-13 19:41:32 +02:00
def to_dict(self):
return {
"id": self.id,
"nickname": self.nickname
}
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-07 17:26:07 +02:00
class Contact(db.Model):
__tablename__ = "contact"
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-07 17:26:07 +02:00
user = relationship("User", back_populates="contacts")
2021-06-13 14:47: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)
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
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-13 14:47:16 +02:00
user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
2021-06-06 22:25:10 +02:00
user = relationship("User", back_populates="address")
class Skill(db.Model):
__tablename__ = "skill"
id = Column(Integer, primary_key=True)
name = Column(String(25), unique=True, nullable=False)
2021-06-12 13:24:26 +02:00
users = relationship("UserSkill", back_populates="skill")
2021-06-06 22:25:10 +02:00
2021-06-07 17:52:14 +02:00
def to_dict(self):
return {"id": self.id, "name": self.name}
2021-06-06 22:25:10 +02:00
2021-06-12 09:46:21 +02:00
class UserSkill(db.Model):
__tablename__ = "user_skill"
user_id = Column(Integer, ForeignKey("user.id"), primary_key=True)
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
user = relationship("User", back_populates="skills")
skill = relationship("Skill", back_populates="users")
2021-06-06 22:25:10 +02:00
class Language(db.Model):
__tablename__ = "language"
id = Column(String(2), primary_key=True)
2021-06-06 22:25:10 +02:00
name = Column(String(25), nullable=False)
2021-06-07 17:26:07 +02:00
2021-06-12 13:24:26 +02:00
users = relationship("UserLanguage", back_populates="language")
def to_dict(self):
return {"id": self.id, "name": self.name}
2021-06-12 09:46:21 +02:00
class UserLanguage(db.Model):
__tablename__ = "user_language"
user_id = Column(Integer, ForeignKey("user.id"), primary_key=True)
language_id = Column(Integer, 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
user = relationship("User", back_populates="languages")
language = relationship("Language", back_populates="users")