Compare commits

...

2 Commits

4 changed files with 37 additions and 14 deletions

View File

@ -33,6 +33,7 @@ class Plugin:
blueprint = None # You have to override blueprint = None # You have to override
permissions = [] # You have to override permissions = [] # You have to override
id = "dev.flaschengeist.plugin" # You have to override
name = "plugin" # You have to override name = "plugin" # You have to override
models = None # You have to override models = None # You have to override
@ -94,7 +95,7 @@ class Plugin:
db.session.commit() db.session.commit()
def notify(self, user, text: str, data=None): def notify(self, user, text: str, data=None):
n = Notification(text=text, data=data, plugin=self.name, user_=user) n = Notification(text=text, data=data, plugin=self.id, user_=user)
db.session.add(n) db.session.add(n)
db.session.commit() db.session.commit()

View File

@ -11,6 +11,7 @@ from . import permissions, models
class EventPlugin(Plugin): class EventPlugin(Plugin):
name = "events" name = "events"
id = "dev.flaschengeist.events"
plugin = LocalProxy(lambda: current_app.config["FG_PLUGINS"][EventPlugin.name]) plugin = LocalProxy(lambda: current_app.config["FG_PLUGINS"][EventPlugin.name])
permissions = permissions.permissions permissions = permissions.permissions
blueprint = Blueprint(name, __name__) blueprint = Blueprint(name, __name__)

View File

@ -153,8 +153,9 @@ def delete_event(event_id):
Raises: Raises:
NotFound if not found NotFound if not found
""" """
event = get_event(event_id) event = get_event(event_id, True)
logger.info(f"{type(event)} {event.__str__}") for job in event.jobs:
delete_job(job)
db.session.delete(event) db.session.delete(event)
db.session.commit() db.session.commit()
@ -202,25 +203,42 @@ def update():
def delete_job(job: Job): def delete_job(job: Job):
for service in job.services:
unassign_job(service=service, notify=True)
db.session.delete(job) db.session.delete(job)
db.session.commit() db.session.commit()
def assign_to_job(job: Job, user, value): def assign_job(job: Job, user, value):
assert value > 0
service = Service.query.get((job.id, user.id_)) service = Service.query.get((job.id, user.id_))
if value < 0: if service:
if not service: service.value = value
raise BadRequest
db.session.delete(service)
else: else:
if service: service = Service(user_=user, value=value, job_=job)
service.value = value db.session.add(service)
else:
service = Service(user_=user, value=value, job_=job)
db.session.add(service)
db.session.commit() db.session.commit()
def unassign_job(job: Job = None, user=None, service=None, notify=False):
if service is None:
assert(job is not None and user is not None)
service = Service.query.get((job.id, user.id_))
else:
user = service.user_
if not service:
raise BadRequest
event_id = service.job_.event_id_
db.session.delete(service)
db.session.commit()
if notify:
EventPlugin.plugin.notify(
user, "Your assignmet was cancelled", {"event_id": event_id}
)
@scheduled @scheduled
def assign_backups(): def assign_backups():
logger.debug("Notifications") logger.debug("Notifications")

View File

@ -415,7 +415,10 @@ def update_job(event_id, job_id, current_session: Session):
user != current_session.user_ and not current_session.user_.has_permission(permissions.ASSIGN_OTHER) user != current_session.user_ and not current_session.user_.has_permission(permissions.ASSIGN_OTHER)
): ):
raise Forbidden raise Forbidden
event_controller.assign_to_job(job, user, value) if value > 0:
event_controller.assign_job(job, user, value)
else:
event_controller.unassign_job(job, user, notify=user != current_session.user_)
except (KeyError, ValueError): except (KeyError, ValueError):
raise BadRequest raise BadRequest