De-avatarify most of room booking (except room owner)
[cds-indico.git] / indico / modules / rb / services / rooms.py
blob730c9faa77442b971c6f0b52502c48eb193e30a8
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 flask import session
18 from sqlalchemy.orm.exc import NoResultFound
20 from indico.core.config import Config
21 from indico.modules.rb.models.blockings import Blocking
22 from indico.modules.rb.models.locations import Location
23 from indico.modules.rb.models.reservations import RepeatFrequency
24 from indico.modules.rb.models.rooms import Room
25 from indico.util.date_time import get_datetime_from_request
26 from indico.util.string import natural_sort_key
27 from MaKaC.services.implementation.base import LoggedOnlyService, ServiceBase
28 from MaKaC.services.interface.rpc.common import ServiceError
29 from MaKaC.webinterface.linking import RoomLinker
32 class RoomBookingListRooms(ServiceBase):
33 UNICODE_PARAMS = True
35 def _checkParams(self):
36 try:
37 location = Location.find_one(name=self._params['location'])
38 except NoResultFound:
39 raise ServiceError('ERR-RB0', 'Invalid location name: {0}.'.format(self._params['location']))
40 self._rooms = sorted(location.rooms, key=lambda r: natural_sort_key(r.full_name))
42 def _getAnswer(self):
43 return [(room.name, room.name) for room in self._rooms]
46 class RoomBookingFullNameListRooms(RoomBookingListRooms):
47 def _getAnswer(self):
48 return [(room.name, room.full_name) for room in self._rooms]
51 class RoomBookingAvailabilitySearchRooms(ServiceBase):
52 UNICODE_PARAMS = True
54 def _checkParams(self):
55 self._start_dt = get_datetime_from_request(prefix='start_', source=self._params)
56 self._end_dt = get_datetime_from_request(prefix='end_', source=self._params)
57 repetition = map(int, self._params['repeatability'].split('|'))
58 self._repetition = RepeatFrequency(repetition[0]), repetition[1]
60 def _getAnswer(self):
61 rooms = Room.find_all(Room.filter_available(self._start_dt, self._end_dt, self._repetition))
62 return [room.id for room in rooms]
65 class RoomBookingListLocationsAndRoomsWithGuids(ServiceBase):
66 UNICODE_PARAMS = True
68 def _checkParams(self):
69 self._isActive = self._params.get('isActive', None)
71 def _getAnswer(self):
72 if not Config.getInstance().getIsRoomBookingActive():
73 return {}
74 criteria = {'_eager': Room.location}
75 if self._isActive is not None:
76 criteria['is_active'] = self._isActive
77 rooms = Room.find_all(**criteria)
78 return {room.id: '{}: {}'.format(room.location_name, room.full_name) for room in rooms}
81 class RoomBookingLocationsAndRoomsGetLink(ServiceBase):
82 UNICODE_PARAMS = True
84 def _checkParams(self):
85 self._location = self._params['location']
86 self._room = self._params['room']
88 def _getAnswer(self):
89 return RoomLinker().getURLByName(self._room, self._location)
92 class BookingPermission(LoggedOnlyService):
93 UNICODE_PARAMS = True
95 def _checkParams(self):
96 blocking_id = self._params.get('blocking_id')
97 self._room = Room.get(self._params['room_id'])
98 self._blocking = Blocking.get(blocking_id) if blocking_id else None
100 def _getAnswer(self):
101 user = session.user
102 return {
103 'blocked': not self._blocking.can_be_overridden(user, self._room) if self._blocking else False,
104 'is_reservable': self._room.is_reservable,
105 'can_book': self._room.can_be_booked(user) or self._room.can_be_prebooked(user),
106 'group': self._room.get_attribute_value('allowed-booking-group')