2020-09-07 14:13:18 +00:00
|
|
|
from werkzeug.exceptions import BadRequest, NotFound
|
2020-09-05 20:26:00 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
|
|
|
|
from flaschengeist import logger
|
2020-10-30 02:30:46 +00:00
|
|
|
from flaschengeist.database import db
|
2020-11-02 02:41:57 +00:00
|
|
|
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")
|
2020-09-05 20:26:00 +00:00
|
|
|
|
|
|
|
|
2020-09-07 14:13:18 +00:00
|
|
|
def get_event(id):
|
|
|
|
return Event.query.get(id)
|
|
|
|
|
|
|
|
|
|
|
|
def get_events(begin, end):
|
|
|
|
"""Query events which start from begin until end
|
|
|
|
Args:
|
|
|
|
begin (datetime): Earliest start
|
|
|
|
end (datetime): Latest start
|
|
|
|
|
|
|
|
Returns: collection of Event objects
|
|
|
|
"""
|
|
|
|
return Event.query.filter((begin <= Event.begin), (Event.begin < end))
|
|
|
|
|
|
|
|
|
|
|
|
def delete_event(id):
|
|
|
|
"""Delete event with given ID
|
|
|
|
Args:
|
|
|
|
id: id of Event to delete
|
|
|
|
|
|
|
|
Returns: True if successful, False if Event is not found
|
|
|
|
"""
|
|
|
|
return Event.query.filter(Event.id == id).delete() == 1
|
|
|
|
|
|
|
|
|
2020-09-05 20:26:00 +00:00
|
|
|
def create_event(begin, kind, end=None, description=None):
|
|
|
|
try:
|
|
|
|
event = Event(begin=begin, end=end, description=description, kind=kind)
|
|
|
|
db.session.add(event)
|
|
|
|
db.session.commit()
|
|
|
|
return event
|
|
|
|
except IntegrityError:
|
|
|
|
logger.debug("Database error when creating new event", exc_info=True)
|
|
|
|
raise BadRequest
|
|
|
|
|
|
|
|
|
2020-09-07 14:13:18 +00:00
|
|
|
def create_job_kind(name):
|
|
|
|
try:
|
|
|
|
kind = JobKind(name=name)
|
|
|
|
db.session.add(kind)
|
|
|
|
db.session.commit()
|
|
|
|
return kind
|
|
|
|
except IntegrityError:
|
|
|
|
logger.debug("IntegrityError: Looks like there is a name collision", exc_info=True)
|
|
|
|
raise BadRequest("Name already exists")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_slot(event, job_slots, needed_persons, start=None, end=None):
|
|
|
|
event_slot = EventSlot(start=start, end=end)
|
|
|
|
for slot in job_slots:
|
|
|
|
kind = JobKind.query.get(slot.id)
|
|
|
|
job_slot = JobSlot(kind=kind, needed_persons=slot.needed_persons)
|
|
|
|
event_slot.add_slot(job_slot)
|
|
|
|
event.add_slot(event_slot)
|
|
|
|
db.session.commit()
|