From 25ba4d21aa77918d44db18c8ca84e76ac177d97b Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sat, 18 Dec 2021 04:23:53 +0100 Subject: [PATCH] feat(balance): Add option to allow active services to debit other users --- flaschengeist/flaschengeist.toml | 2 + flaschengeist/plugins/balance/__init__.py | 58 +++++++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/flaschengeist/flaschengeist.toml b/flaschengeist/flaschengeist.toml index 77af5b4..d1351a7 100644 --- a/flaschengeist/flaschengeist.toml +++ b/flaschengeist/flaschengeist.toml @@ -119,3 +119,5 @@ If this was not you, please contact the support. # enabled = true # Enable a default limit, will be set if no other limit is set # limit = -10.00 +# Uncomment to allow active services to debit other users (requires events plugin) +# allow_service_debit = true diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index cb4d929..ca61a5f 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -3,29 +3,79 @@ Extends users plugin with balance functions """ -from werkzeug.local import LocalProxy from flask import Blueprint, current_app +from werkzeug.local import LocalProxy +from werkzeug.exceptions import NotFound from flaschengeist import logger -from flaschengeist.plugins import Plugin, before_update_user +from flaschengeist.plugins import Plugin, plugins_loaded, before_update_user +from flaschengeist.plugins.scheduler import add_scheduled from . import permissions, models +def service_debit(): + from flaschengeist.database import db + from flaschengeist.models import UtcDateTime + from flaschengeist.models.user import User + from flaschengeist.controller import roleController + from flaschengeist_events.models import Service, Job + + role = BalancePlugin.plugin.get_setting("service_role", default=None) + if role is None: + try: + role = roleController.get("__has_service") + except NotFound: + role = roleController.create_role("__has_service", [permissions.DEBIT]) + BalancePlugin.plugin.set_setting("service_role", role.id) + else: + role = roleController.get(role) + + active_services = ( + User.query.join(Service, User.id_ == Service._user_id) + .join(Job, Service.job_) + .filter(Job.start <= UtcDateTime.current_utc(), Job.end.is_(None) | (Job.end >= UtcDateTime.current_utc())) + .distinct(User.id_) + .all() + ) + previous_services = BalancePlugin.plugin.get_setting("service_debit", default=[]) + + logger.debug(f"Found {len(active_services)} users doing their service.") + + if len(previous_services) > 0: + previous_services = User.query.filter(User.userid.in_(previous_services)).all() + # Remove not active users + for user in [u for u in previous_services if u not in active_services]: + user.roles_ = [r for r in user.roles_ if r.id != role.id] + # Add active + for user in [u for u in active_services if u not in previous_services]: + if role not in user.roles_: + user.roles_.append(role) + db.session.commit() + BalancePlugin.plugin.set_setting("service_debit", [u.userid for u in active_services]) + + class BalancePlugin(Plugin): name = "balance" id = "dev.flaschengeist.balance" blueprint = Blueprint(name, __name__) permissions = permissions.permissions - plugin = LocalProxy(lambda: current_app.config["FG_PLUGINS"][BalancePlugin.name]) + plugin: "BalancePlugin" = LocalProxy(lambda: current_app.config["FG_PLUGINS"][BalancePlugin.name]) models = models def __init__(self, config): super(BalancePlugin, self).__init__(config) - from . import routes, balance_controller + from . import routes + + @plugins_loaded + def post_loaded(*args, **kwargs): + if config.get("allow_service_debit", False) and "events" in current_app.config["FG_PLUGINS"]: + add_scheduled(f"{id}.service_debit", service_debit, minutes=1) @before_update_user def set_default_limit(user): + from . import balance_controller + try: limit = self.get_setting("limit") logger.debug("Setting default limit of {} to user {}".format(limit, user.userid))