[Plugin] schedule: Fixed models and controller

This commit is contained in:
Ferdinand Thiessen 2020-11-02 03:41:57 +01:00
parent b4234c43b8
commit 7dec0144d9
2 changed files with 49 additions and 30 deletions

View File

@ -3,7 +3,46 @@ from sqlalchemy.exc import IntegrityError
from flaschengeist import logger from flaschengeist import logger
from flaschengeist.database import db from flaschengeist.database import db
from flaschengeist.plugins.schedule.models import EventKind, Event, EventSlot, JobSlot, JobKind from flaschengeist.plugins.schedule.models import EventType, Event, EventSlot, JobSlot, JobType
def get_event_types():
return EventType.query.all()
def get_event_type(name):
et = EventType.query.filter(EventType.name == name).one_or_none()
if not et:
raise NotFound
return et
def create_event_type(name):
try:
event = EventType(name=name)
db.session.add(event)
db.session.commit()
return event
except IntegrityError:
raise BadRequest("Name already exists")
def rename_event_type(name, new_name):
event_type = get_event_type(name)
event_type.name = new_name
try:
db.session.commit()
except IntegrityError:
raise BadRequest("Name already exists")
def delete_event_type(name):
event_type = get_event_type(name)
db.session.delete(event_type)
try:
db.session.commit()
except IntegrityError:
raise BadRequest("Type still in use")
def get_event(id): def get_event(id):
@ -53,26 +92,6 @@ def create_job_kind(name):
raise BadRequest("Name already exists") raise BadRequest("Name already exists")
def create_event_kind(name):
try:
event = EventKind(name=name)
db.session.add(event)
db.session.commit()
return event
except IntegrityError:
logger.debug("IntegrityError: Looks like there is a name collision", exc_info=True)
raise BadRequest("Name already exists")
def rename_event_kind(id, name):
ek = EventKind.query.get(id)
if not ek:
raise NotFound
ek.name = name
try:
db.session.commit()
except IntegrityError:
raise BadRequest("Name already exists")
def add_slot(event, job_slots, needed_persons, start=None, end=None): def add_slot(event, job_slots, needed_persons, start=None, end=None):

View File

@ -45,15 +45,15 @@ class Job(db.Model, ModelSerializeMixin):
class JobSlot(db.Model, ModelSerializeMixin): class JobSlot(db.Model, ModelSerializeMixin):
__tablename__ = "job_slot" __tablename__ = "job_slot"
_kind_id = db.Column("kind_id", db.Integer, db.ForeignKey("job_kind.id")) _type_id = db.Column("type_id", db.Integer, db.ForeignKey("job_type.id"))
_event_slot_id = db.Column("event_slot_id", db.Integer, db.ForeignKey("event_slot.id")) _event_slot_id = db.Column("event_slot_id", db.Integer, db.ForeignKey("event_slot.id"))
id: int = db.Column(db.Integer, primary_key=True) id: int = db.Column(db.Integer, primary_key=True)
type: JobType = db.relationship("JobKind") type: JobType = db.relationship("JobType")
users: [Job] = db.relationship("Job", back_populates="_slot") users: [Job] = db.relationship("Job", back_populates="slot_")
required_jobs: float = db.Column(db.Numeric(precision=4, scale=2)) required_jobs: float = db.Column(db.Numeric(precision=4, scale=2))
event_slot_ = db.relationship("EventSlot", back_populates="slots") event_slot_ = db.relationship("EventSlot", back_populates="jobs")
########## ##########
# Events # # Events #
@ -68,19 +68,19 @@ class EventSlot(db.Model, ModelSerializeMixin):
id: int = db.Column(db.Integer, primary_key=True) id: int = db.Column(db.Integer, primary_key=True)
start: datetime = db.Column(db.DateTime) start: datetime = db.Column(db.DateTime)
end: Optional[datetime] = db.Column(db.DateTime) end: Optional[datetime] = db.Column(db.DateTime)
jobs: [JobSlot] = db.relationship("JobSlot", back_populates="_event_slot") jobs: [JobSlot] = db.relationship("JobSlot", back_populates="event_slot_")
event_ = db.relationship("Event", back_populates="_slots") event_ = db.relationship("Event", back_populates="slots")
class Event(db.Model, ModelSerializeMixin): class Event(db.Model, ModelSerializeMixin):
"""Model for an Event""" """Model for an Event"""
__tablename__ = "event" __tablename__ = "event"
_kind_id = db.Column("kind_id", db.Integer, db.ForeignKey("event_kind.id", ondelete="CASCADE"), nullable=False) _type_id = db.Column("type_id", db.Integer, db.ForeignKey("event_type.id", ondelete="CASCADE"), nullable=False)
id: int = db.Column(db.Integer, primary_key=True) id: int = db.Column(db.Integer, primary_key=True)
begin: datetime = db.Column(db.DateTime, nullable=False) begin: datetime = db.Column(db.DateTime, nullable=False)
end: datetime = db.Column(db.DateTime) end: datetime = db.Column(db.DateTime)
description: str = db.Column(db.String(240)) description: str = db.Column(db.String(240))
type: EventType = db.relationship("EventKind") type: EventType = db.relationship("EventType")
slots: [EventSlot] = db.relationship("EventSlot", back_populates="_event", cascade="all, delete") slots: [EventSlot] = db.relationship("EventSlot", back_populates="event_", cascade="all, delete")