2018-11-03 23:40:20 +01:00
|
|
|
from typing import Any, Callable, Dict, List
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2019-08-14 12:19:20 +02:00
|
|
|
from openslides.utils.cache_providers import Cachable, MemoryCacheProvider
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
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():
|
2019-01-06 16:22:33 +01:00
|
|
|
if key == "id":
|
2018-07-09 23:22:26 +02:00
|
|
|
restricted_element[key] = value
|
|
|
|
else:
|
2019-01-12 23:01:42 +01:00
|
|
|
restricted_element[key] = f"restricted_{value}"
|
2018-07-09 23:22:26 +02:00
|
|
|
out.append(restricted_element)
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2018-10-28 10:04:52 +01:00
|
|
|
class Collection1:
|
2019-09-02 13:57:12 +02:00
|
|
|
personalized_model = False
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
def get_collection_string(self) -> str:
|
2019-01-06 16:22:33 +01:00
|
|
|
return "app/collection1"
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
def get_elements(self) -> List[Dict[str, Any]]:
|
2019-01-06 16:22:33 +01:00
|
|
|
return [{"id": 1, "value": "value1"}, {"id": 2, "value": "value2"}]
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
async def restrict_elements(
|
|
|
|
self, user_id: int, elements: List[Dict[str, Any]]
|
|
|
|
) -> List[Dict[str, Any]]:
|
2018-11-03 23:40:20 +01:00
|
|
|
return restrict_elements(elements)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2018-10-28 10:04:52 +01:00
|
|
|
class Collection2:
|
2019-09-02 13:57:12 +02:00
|
|
|
personalized_model = False
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
def get_collection_string(self) -> str:
|
2019-01-06 16:22:33 +01:00
|
|
|
return "app/collection2"
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
def get_elements(self) -> List[Dict[str, Any]]:
|
2019-01-06 16:22:33 +01:00
|
|
|
return [{"id": 1, "key": "value1"}, {"id": 2, "key": "value2"}]
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
async def restrict_elements(
|
|
|
|
self, user_id: int, elements: List[Dict[str, Any]]
|
|
|
|
) -> List[Dict[str, Any]]:
|
2018-11-03 23:40:20 +01:00
|
|
|
return restrict_elements(elements)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2019-09-02 13:57:12 +02:00
|
|
|
class PersonalizedCollection:
|
|
|
|
personalized_model = True
|
|
|
|
|
|
|
|
def get_collection_string(self) -> str:
|
|
|
|
return "app/personalized-collection"
|
|
|
|
|
|
|
|
def get_elements(self) -> List[Dict[str, Any]]:
|
|
|
|
return [
|
|
|
|
{"id": 1, "key": "value1", "user_id": 1},
|
|
|
|
{"id": 2, "key": "value2", "user_id": 2},
|
|
|
|
]
|
|
|
|
|
|
|
|
async def restrict_elements(
|
|
|
|
self, user_id: int, elements: List[Dict[str, Any]]
|
|
|
|
) -> List[Dict[str, Any]]:
|
|
|
|
return [element for element in elements if element["user_id"] == user_id]
|
|
|
|
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
def get_cachable_provider(
|
2019-09-02 13:57:12 +02:00
|
|
|
cachables: List[Cachable] = [Collection1(), Collection2(), PersonalizedCollection()]
|
2019-01-06 16:22:33 +01:00
|
|
|
) -> Callable[[], List[Cachable]]:
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
|
|
|
Returns a cachable_provider.
|
|
|
|
"""
|
|
|
|
return lambda: cachables
|
|
|
|
|
|
|
|
|
|
|
|
def example_data():
|
|
|
|
return {
|
2019-01-06 16:22:33 +01:00
|
|
|
"app/collection1": [{"id": 1, "value": "value1"}, {"id": 2, "value": "value2"}],
|
|
|
|
"app/collection2": [{"id": 1, "key": "value1"}, {"id": 2, "key": "value2"}],
|
2019-09-02 13:57:12 +02:00
|
|
|
"app/personalized-collection": [
|
|
|
|
{"id": 1, "key": "value1", "user_id": 1},
|
|
|
|
{"id": 2, "key": "value2", "user_id": 2},
|
|
|
|
],
|
2019-01-06 16:22:33 +01:00
|
|
|
}
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2019-08-14 12:19:20 +02:00
|
|
|
class TTestCacheProvider(MemoryCacheProvider):
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
2019-08-14 12:19:20 +02:00
|
|
|
CacheProvider simular to the MemoryCacheProvider with special methods for
|
2018-07-09 23:22:26 +02:00
|
|
|
testing.
|
|
|
|
|
2019-12-03 08:25:48 +01:00
|
|
|
Currently just a dummy for future extensions.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-12-03 08:25:48 +01:00
|
|
|
pass
|