from pyramid.events import subscriber, NewRequest
from pyramid.httpexceptions import HTTPServiceUnavailable
from brewman.models.master import DbSetting


@subscriber(NewRequest)
def maintenance_mode(event):
    maintenance = DbSetting.by_name('Maintenance')
    if maintenance is not None and maintenance.data != event.request.authenticated_userid:
        raise HTTPServiceUnavailable
