2018-08-22 22:00:08 +02:00
|
|
|
import asyncio
|
2018-11-03 23:40:20 +01:00
|
|
|
from typing import Any, Callable, Dict, List
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
from openslides.utils.cache_providers import Cachable, MemmoryCacheProvider
|
|
|
|
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
def restrict_elements(elements: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
|
|
|
Adds the prefix 'restricted_' to all values except id.
|
|
|
|
"""
|
|
|
|
out = []
|
|
|
|
for element in elements:
|
|
|
|
restricted_element = {}
|
|
|
|
for key, value in element.items():
|
|
|
|
if key == 'id':
|
|
|
|
restricted_element[key] = value
|
|
|
|
else:
|
|
|
|
restricted_element[key] = 'restricted_{}'.format(value)
|
|
|
|
out.append(restricted_element)
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2018-10-28 10:04:52 +01:00
|
|
|
class Collection1:
|
2018-07-09 23:22:26 +02:00
|
|
|
def get_collection_string(self) -> str:
|
|
|
|
return 'app/collection1'
|
|
|
|
|
|
|
|
def get_elements(self) -> List[Dict[str, Any]]:
|
|
|
|
return [
|
|
|
|
{'id': 1, 'value': 'value1'},
|
|
|
|
{'id': 2, 'value': 'value2'}]
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
async def restrict_elements(self, user_id: int, elements: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
|
|
return restrict_elements(elements)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2018-10-28 10:04:52 +01:00
|
|
|
class Collection2:
|
2018-07-09 23:22:26 +02:00
|
|
|
def get_collection_string(self) -> str:
|
|
|
|
return 'app/collection2'
|
|
|
|
|
|
|
|
def get_elements(self) -> List[Dict[str, Any]]:
|
|
|
|
return [
|
|
|
|
{'id': 1, 'key': 'value1'},
|
|
|
|
{'id': 2, 'key': 'value2'}]
|
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
async def restrict_elements(self, user_id: int, elements: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
|
|
return restrict_elements(elements)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_cachable_provider(cachables: List[Cachable] = [Collection1(), Collection2()]) -> Callable[[], List[Cachable]]:
|
|
|
|
"""
|
|
|
|
Returns a cachable_provider.
|
|
|
|
"""
|
|
|
|
return lambda: cachables
|
|
|
|
|
|
|
|
|
|
|
|
def example_data():
|
|
|
|
return {
|
|
|
|
'app/collection1': [
|
|
|
|
{'id': 1, 'value': 'value1'},
|
|
|
|
{'id': 2, 'value': 'value2'}],
|
|
|
|
'app/collection2': [
|
|
|
|
{'id': 1, 'key': 'value1'},
|
|
|
|
{'id': 2, 'key': 'value2'}]}
|
|
|
|
|
|
|
|
|
|
|
|
class TTestCacheProvider(MemmoryCacheProvider):
|
|
|
|
"""
|
|
|
|
CacheProvider simular to the MemmoryCacheProvider with special methods for
|
|
|
|
testing.
|
|
|
|
"""
|
|
|
|
|
2018-09-01 08:00:00 +02:00
|
|
|
async def del_lock_after_wait(self, lock_name: str, future: asyncio.Future = None) -> None:
|
2018-07-09 23:22:26 +02:00
|
|
|
if future is None:
|
2018-09-01 08:00:00 +02:00
|
|
|
asyncio.ensure_future(self.del_lock(lock_name))
|
2018-07-09 23:22:26 +02:00
|
|
|
else:
|
|
|
|
async def set_future() -> None:
|
2018-09-01 08:00:00 +02:00
|
|
|
await self.del_lock(lock_name)
|
2018-07-09 23:22:26 +02:00
|
|
|
future.set_result(1) # type: ignore
|
|
|
|
asyncio.ensure_future(set_future())
|