def _build_tenant_list(self, params): if not self._has_a_tenant_uuid(): return tenant_uuid = Tenant.autodetect().uuid recurse = params.get('recurse', False) if not recurse: return [tenant_uuid] tenants = [] auth_client = get_auth_client() token_object = get_token() auth_client.set_token(token_object.uuid) try: tenants = auth_client.tenants.list( tenant_uuid=tenant_uuid)['items'] except HTTPError as e: response = getattr(e, 'response', None) status_code = getattr(response, 'status_code', None) if status_code == 401: return [tenant_uuid] raise return [t['uuid'] for t in tenants]
def get(self, profile): args = parser.parse_args() term = args['term'] logger.info('Lookup for %s with profile %s', term, profile) tenant = Tenant.autodetect() try: profile_config = self.profile_service.get_by_name( tenant.uuid, profile) display = self.build_display(profile_config) except OldAPIException as e: return e.body, e.status_code token = request.headers['X-Auth-Token'] token_infos = auth.client().token.get(token) user_uuid = token_infos['metadata']['uuid'] raw_results = self.lookup_service.lookup(profile_config, tenant.uuid, term, user_uuid, token=token) favorites = self.favorite_service.favorite_ids(profile_config, user_uuid).by_name formatter = _ResultFormatter(display) response = formatter.format_results(raw_results, favorites) response.update({'term': term}) return response
def get(self, category, filename): tenant = Tenant.autodetect() parameters = SoundQueryParametersSchema().load(request.args) parameters['file_name'] = filename sound = self.service.get(tenant.uuid, category, parameters) response = self.service.load_first_file(sound) return response
def post(self, tenant): scoping_tenant = Tenant.autodetect() matching_tenant = self._find_tenant(scoping_tenant, tenant) return self.phonebook_service.create_phonebook( matching_tenant['uuid'], request.json, ), 201
def get(self, source_uuid): user_uuid = token.user_uuid token_from_request = request.headers.get('X-Auth-Token') tenant = Tenant.autodetect() list_params = contact_list_schema.load( self._map_list_params(request.args)) source = self.source_service.get(self.BACKEND, source_uuid, [tenant.uuid]) microsoft_token = get_microsoft_access_token(user_uuid, token_from_request, **source['auth']) contacts, total = self.office365.get_contacts(microsoft_token, source['endpoint'], **list_params) return ( { 'filtered': total, 'items': contacts, 'total': total }, 200, )
def delete(self, line_id, device_id): tenant_uuids = self._build_tenant_list({'recurse': True}) line = self.line_dao.get(line_id, tenant_uuids=tenant_uuids) tenant_uuid = Tenant.autodetect().uuid device = self.device_dao.get(device_id, tenant_uuid=tenant_uuid) self.service.dissociate(line, device) return '', 204
def delete(self, category, filename): tenant = Tenant.autodetect() parameters = SoundQueryParametersSchema().load(request.args) parameters['file_name'] = filename sound = self.service.get(tenant.uuid, category, parameters) self.service.delete_files(sound) return '', 204
def get(self): tenant_uuid = Tenant.autodetect().uuid args = CELListRequestSchema().load(request.args) args['tenant_uuid'] = tenant_uuid cels = self.cel_service.list(args) result = {'items': CELSchema().dump(cels, many=True)} return result
def post(self): tenant = Tenant.autodetect() args = self.source_schema.load(request.get_json()) body = self._service.create(self._backend, tenant_uuid=tenant.uuid, **args) return self.source_schema.dump(body)
def get(self, profile): logger.debug('Listing personal with profile %s', profile) token = request.headers.get('X-Auth-Token', '') token_infos = auth.client().token.get(token) tenant = Tenant.autodetect() try: profile_config = self.profile_service.get_by_name(tenant.uuid, profile) display = self.build_display(profile_config) except OldAPIException as e: return e.body, e.status_code raw_results = self.personal_service.list_contacts( tenant.uuid, token_infos['xivo_user_uuid'], ) try: favorites = self.favorite_service.favorite_ids( profile_config, token_infos['xivo_user_uuid'], ).by_name except self.favorite_service.NoSuchProfileException as e: return _error(404, str(e)) formatter = _ResultFormatter(display) return formatter.format_results(raw_results, favorites)
def post(self): tenant = Tenant.autodetect() fax_infos = fax_creation_request_schema.load(request.args).data fax = self._service.send_fax(tenant.uuid, content=request.data, fax_infos=fax_infos) return fax_schema.dump(fax).data, 201
def post(self, tenant, phonebook_id): scoping_tenant = Tenant.autodetect() matching_tenant = self._find_tenant(scoping_tenant, tenant) charset = request.mimetype_params.get('charset', 'utf-8') try: data = request.data.decode(charset).split('\n') except LookupError as e: if 'unknown encoding:' in str(e): return _make_error(str(e), 400) else: raise reader = csv.reader(data) fields = next(reader) duplicates = list(set([f for f in fields if fields.count(f) > 1])) if duplicates: return _make_error('duplicate columns: {}'.format(duplicates), 400) to_add = [c for c in csv.DictReader(data)] created, failed = self.phonebook_service.import_contacts( matching_tenant['uuid'], phonebook_id, to_add, ) return {'created': created, 'failed': failed}
def get(self, profile, user_uuid): token = request.headers['X-Auth-Token'] args = parser_reverse.parse_args() exten = args['exten'] tenant = Tenant.autodetect() try: profile_config = self.profile_service.get_by_name( tenant.uuid, profile) except OldAPIException as e: return e.body, e.status_code logger.info('Reverse for %s with profile %s', exten, profile) raw_result = self.reverse_service.reverse(profile_config, exten, profile, user_uuid=user_uuid, token=token) response = { 'display': None, 'exten': exten, 'fields': {}, 'source': None } if raw_result is not None: response['display'] = raw_result.fields.get('reverse') response['fields'] = raw_result.fields response['source'] = raw_result.source return response
def get(self, profile, user_uuid): args = parser.parse_args() term = args['term'] logger.info('Lookup %s for user %s with profile %s', term, user_uuid, profile) tenant_uuid = Tenant.autodetect().uuid try: profile_config = self.profile_service.get_by_name( tenant_uuid, profile) display = self.build_display(profile_config) except NoSuchProfile as e: raise NoSuchProfileAPIException(e.profile) token = request.headers['X-Auth-Token'] raw_results = self.lookup_service.lookup(profile_config, tenant_uuid, term, user_uuid, token=token) favorites = self.favorite_service.favorite_ids(profile_config, user_uuid).by_name formatter = _ResultFormatter(display) response = formatter.format_results(raw_results, favorites) response.update({'term': term}) return response
def put(self): tenant_uuid = Tenant.autodetect().uuid retention = self.service.find_or_create(tenant_uuid) retention_args = RetentionSchema().load(request.get_json()) update_model_instance(retention, retention_args) self.service.update(retention) return '', 204
def visible_tenants(self, recurse=True): tenant_uuid = Tenant.autodetect().uuid if recurse: return [ tenant.uuid for tenant in token.visible_tenants(tenant_uuid) ] else: return [tenant_uuid]
def add_tenant_to_form(self, form): if not self._has_write_tenant_uuid(): return form tenant = Tenant.autodetect() tenant_dao.find_or_create_tenant(tenant.uuid) form['tenant_uuid'] = tenant.uuid return form
def get(self, device_id): tenant_uuid = Tenant.autodetect().uuid device = self.device_dao.get(device_id, tenant_uuid=tenant_uuid) line_devices = self.service.find_all_associations_from_device(device) return { 'total': len(line_devices), 'items': self.schema().dump(line_devices, many=True), }
def put(self, tenant, phonebook_id): scoping_tenant = Tenant.autodetect() matching_tenant = self._find_tenant(scoping_tenant, tenant) return ( self.phonebook_service.edit_phonebook(matching_tenant['uuid'], phonebook_id, request.json), 200, )
def get(self, tenant, phonebook_id): scoping_tenant = Tenant.autodetect() matching_tenant = self._find_tenant(scoping_tenant, tenant) return ( self.phonebook_service.get_phonebook(matching_tenant['uuid'], phonebook_id), 200, )
def post(self, tenant, phonebook_id): scoping_tenant = Tenant.autodetect() matching_tenant = self._find_tenant(scoping_tenant, tenant) return ( self.phonebook_service.create_contact(matching_tenant['uuid'], phonebook_id, request.json), 201, )
def put(self, profile_uuid): tenant_uuid = Tenant.autodetect().uuid visible_tenants = self.get_visible_tenants(tenant_uuid) args = profile_schema.load(request.get_json()).data self._profile_service.edit(profile_uuid, visible_tenants=visible_tenants, **args) return '', 204
def delete(self, tenant, phonebook_id, contact_uuid): scoping_tenant = Tenant.autodetect() matching_tenant = self._find_tenant(scoping_tenant, tenant) self.phonebook_service.delete_contact( matching_tenant['uuid'], phonebook_id, contact_uuid, ) return '', 204
def get(self, conference_id): tenant = Tenant.autodetect() participants = self._service.list_participants(conference_id, tenant.uuid) items = { 'items': participant_schema.dump(participants, many=True).data, 'total': len(participants), } return items, 200
def query_or_header_visible_tenants(self, recurse=True): self._set_up_token_helper_to_verify_tenant() tenant_uuid = Tenant.autodetect(include_query=True).uuid if recurse: return [ tenant.uuid for tenant in token.visible_tenants(tenant_uuid) ] else: return [tenant_uuid]
def post(self): tenant = Tenant.autodetect() user_uuid = get_token_user_uuid_from_request(self._auth_client) fax_infos = user_fax_creation_request_schema.load(request.args).data fax = self._service.send_fax_from_user(tenant.uuid, user_uuid, content=request.data, fax_infos=fax_infos) return fax_schema.dump(fax).data, 201
def _build_tenant_list(self, params): if not self._has_a_tenant_uuid(): return tenant_uuid = Tenant.autodetect().uuid if not params.get('recurse', False): return [tenant_uuid] return [tenant.uuid for tenant in token.visible_tenants(tenant_uuid)]
def get(self, meeting_uuid): tenant = Tenant.autodetect() participants = self._service.list_participants(tenant.uuid, meeting_uuid) items = { 'items': participant_schema.dump(participants, many=True), 'total': len(participants), } return items, 200
def get(self, profile): args = ListSchema().load(request.args) tenant_uuid = Tenant.autodetect().uuid count, filtered, sources = self._profile_service.get_sources_from_profile_name( tenant_uuid=tenant_uuid, profile_name=profile, **args ) return {'total': count, 'filtered': filtered, 'items': sources}
def get(self, meeting_uuid): tenant = Tenant.autodetect() user_uuid = get_token_user_uuid_from_request() participants = self._service.user_list_participants( tenant.uuid, user_uuid, meeting_uuid) items = { 'items': participant_schema.dump(participants, many=True), 'total': len(participants), } return items, 200