From 92626dc0c642ca8bf7744313290994255060b926 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 20 Oct 2020 19:25:10 +0200 Subject: [PATCH] Some work on event models --- .../system/controller/userController.py | 2 +- flaschengeist/system/models/event.py | 57 ++++++++----------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/flaschengeist/system/controller/userController.py b/flaschengeist/system/controller/userController.py index b58cc04..1e33f24 100644 --- a/flaschengeist/system/controller/userController.py +++ b/flaschengeist/system/controller/userController.py @@ -20,7 +20,7 @@ def login_user(username, password): def update_user(user): current_app.config["FG_AUTH_BACKEND"].update_user(user) if not user.display_name: - user.display_name = user.firstname[0] + user.lastname + user.display_name = "{} {}".format(user.firstname, user.lastname) db.session.commit() diff --git a/flaschengeist/system/models/event.py b/flaschengeist/system/models/event.py index 09ae7e9..00d8a43 100644 --- a/flaschengeist/system/models/event.py +++ b/flaschengeist/system/models/event.py @@ -1,76 +1,65 @@ +from . import ModelSerializeMixin from ..database import db -class Event(db.Model): +class Event(db.Model, ModelSerializeMixin): """Model for an Event""" - __tablename__ = "event" id = db.Column(db.Integer, primary_key=True) begin = db.Column(db.DateTime, nullable=False) end = db.Column(db.DateTime) description = db.Column(db.String(240)) - kind_id = db.Column(db.Integer, db.ForeignKey("event_kind.id", ondelete="CASCADE"), nullable=False) kind = db.relationship("EventKind") slots = db.relationship("EventSlot", back_populates="event", cascade="all, delete") # notices = db.relationship("EventNotice", back_populates="event") - def serialize(self): - return {"id": self.id, "begin": self.begin, "end": self.end, "description": self.description, "kind": self.kind} + _kind_id = db.Column(db.Integer, db.ForeignKey("event_kind.id", ondelete="CASCADE"), nullable=False) -class EventKind(db.Model): +class EventKind(db.Model, ModelSerializeMixin): """Model for an EventKind""" - __tablename__ = "event_kind" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), nullable=False, unique=True) - def serialize(self): - return {"id": self.id, "name": self.name} - -class EventSlot(db.Model): +class EventSlot(db.Model, ModelSerializeMixin): """Model for an EventSlot""" - __tablename__ = "event_slot" id = db.Column(db.Integer, primary_key=True) start = db.Column(db.DateTime) end = db.Column(db.DateTime) - event_id = db.Column(db.Integer, db.ForeignKey("event.id"), nullable=False) - event = db.relationship("Event", back_populates="slots") slots = db.relationship("JobSlot", back_populates="event_slot") - def serialize(self): - return { - "id": self.id, - "start": self.start, - "end": self.end, - "event": self.event_id, - } + _event_id = db.Column(db.Integer, db.ForeignKey("event.id"), nullable=False) + _event = db.relationship("Event", back_populates="slots") -class JobSlot(db.Model): +class JobSlot(db.Model, ModelSerializeMixin): __tablename__ = "job_slot" id = db.Column(db.Integer, primary_key=True) needed_persons = db.Column(db.Numeric(precision=4, scale=2)) - event_slot_id = db.Column(db.Integer, db.ForeignKey("event_slot.id")) - event_slot = db.relationship("EventSlot", back_populates="slots") - kind_id = db.Column(db.Integer, db.ForeignKey("job_kind.id")) kind = db.relationship("JobKind") jobs = db.relationship("Job", back_populates="slot") + _event_slot_id = db.Column(db.Integer, db.ForeignKey("event_slot.id")) + _event_slot = db.relationship("EventSlot", back_populates="slots") + _kind_id = db.Column(db.Integer, db.ForeignKey("job_kind.id")) -class Job(db.Model): + +class Job(db.Model, ModelSerializeMixin): __tablename__ = "job" - id = db.Column(db.Integer, primary_key=True) - value = db.Column(db.Numeric(precision=3, scale=2)) - user_id = db.Column(db.Integer, db.ForeignKey("user.id")) user = db.relationship("User") - slot_id = db.Column(db.Integer, db.ForeignKey("job_slot.id")) - slot = db.relationship("JobSlot") + value = db.Column(db.Numeric(precision=3, scale=2)) + + _user_id = db.Column(db.Integer, db.ForeignKey("user.id")) + _slot_id = db.Column(db.Integer, db.ForeignKey("job_slot.id")) + _slot = db.relationship("JobSlot") + _id = db.Column(db.Integer, primary_key=True) -class JobKind(db.Model): +class JobKind(db.Model, ModelSerializeMixin): __tablename__ = "job_kind" - id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(30)) + name = db.Column(db.String(30), nullable=False, unique=True) + + _id = db.Column(db.Integer, primary_key=True)