1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from __future__
import unicode_literals
19 from flask
import session
20 from sqlalchemy
.orm
import joinedload
22 from indico
.core
import signals
23 from indico
.core
.db
import db
24 from indico
.core
.logger
import Logger
25 from indico
.core
.settings
import SettingsProxy
26 from indico
.modules
.rb
.models
.blocking_principals
import BlockingPrincipal
27 from indico
.modules
.rb
.models
.blockings
import Blocking
28 from indico
.modules
.rb
.models
.reservations
import Reservation
29 from indico
.modules
.rb
.models
.rooms
import Room
32 logger
= Logger
.get('rb')
35 settings
= SettingsProxy('roombooking', {
36 'assistance_emails': [],
37 'vc_support_emails': [],
38 'notification_hour': 6,
39 'notification_before_days': 1,
40 'notifications_enabled': True
41 }, acls
={'admin_principals', 'authorized_principals'})
44 @signals.users
.merged
.connect
45 def _merge_users(target
, source
, **kwargs
):
46 source_principals
= set(source
.in_blocking_acls
.options(joinedload(BlockingPrincipal
.blocking
)))
47 target_blockings
= {x
.blocking
for x
in target
.in_blocking_acls
.options(joinedload(BlockingPrincipal
.blocking
))}
48 for principal
in source_principals
:
49 if principal
.blocking
not in target_blockings
:
50 principal
.user_id
= target
.id
52 db
.session
.delete(principal
)
53 Blocking
.find(created_by_id
=source
.id).update({Blocking
.created_by_id
: target
.id})
54 Reservation
.find(created_by_id
=source
.id).update({Reservation
.created_by_id
: target
.id})
55 Reservation
.find(booked_for_id
=source
.id).update({Reservation
.booked_for_id
: target
.id})
56 Room
.find(owner_id
=source
.id).update({Room
.owner_id
: target
.id})
57 settings
.acls
.merge_users(target
, source
)
60 @signals.event
.deleted
.connect
61 def _event_deleted(event
, user
, **kwargs
):
62 if not event
.id.isdigit():
64 reservations
= Reservation
.find(Reservation
.event_id
== int(event
.id),
65 ~Reservation
.is_cancelled
,
66 ~Reservation
.is_rejected
)
67 for resv
in reservations
:
69 resv
.cancel(user
or session
.user
, 'Associated event was deleted')