2017-10-31 11:20:18 +01:00
|
|
|
from django.conf import settings
|
2017-10-27 17:25:51 +02:00
|
|
|
from django.db.models import Sum
|
|
|
|
from django.db.models.query import F
|
|
|
|
from django.db.models.functions import Length
|
2019-02-02 23:45:30 +01:00
|
|
|
from zerver.models import BotStorageData, UserProfile
|
2017-10-27 17:25:51 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import Optional, List, Tuple
|
2017-10-27 17:25:51 +02:00
|
|
|
|
2017-10-31 11:10:56 +01:00
|
|
|
class StateError(Exception):
|
2017-10-31 11:04:46 +01:00
|
|
|
pass
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_bot_storage(bot_profile: UserProfile, key: str) -> str:
|
2017-10-30 16:21:01 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
return BotStorageData.objects.get(bot_profile=bot_profile, key=key).value
|
|
|
|
except BotStorageData.DoesNotExist:
|
2017-11-20 14:49:25 +01:00
|
|
|
raise StateError("Key does not exist.")
|
2017-10-27 17:25:51 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_bot_storage_size(bot_profile: UserProfile, key: Optional[str]=None) -> int:
|
2017-10-27 17:25:51 +02:00
|
|
|
if key is None:
|
2017-11-24 10:18:29 +01:00
|
|
|
return BotStorageData.objects.filter(bot_profile=bot_profile) \
|
|
|
|
.annotate(key_size=Length('key'), value_size=Length('value')) \
|
|
|
|
.aggregate(sum=Sum(F('key_size')+F('value_size')))['sum'] or 0
|
2017-10-27 17:25:51 +02:00
|
|
|
else:
|
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
return len(key) + len(BotStorageData.objects.get(bot_profile=bot_profile, key=key).value)
|
|
|
|
except BotStorageData.DoesNotExist:
|
2017-10-27 17:25:51 +02:00
|
|
|
return 0
|
2017-10-31 11:04:46 +01:00
|
|
|
|
2017-11-27 05:27:04 +01:00
|
|
|
def set_bot_storage(bot_profile: UserProfile, entries: List[Tuple[str, str]]) -> None:
|
2017-11-24 10:18:29 +01:00
|
|
|
storage_size_limit = settings.USER_STATE_SIZE_LIMIT
|
|
|
|
storage_size_difference = 0
|
2017-11-20 14:54:41 +01:00
|
|
|
for key, value in entries:
|
2020-04-22 01:59:09 +02:00
|
|
|
if not isinstance(key, str):
|
2020-06-09 00:25:09 +02:00
|
|
|
raise StateError(f"Key type is {type(key)}, but should be str.")
|
2020-04-22 01:59:09 +02:00
|
|
|
if not isinstance(value, str):
|
2020-06-09 00:25:09 +02:00
|
|
|
raise StateError(f"Value type is {type(value)}, but should be str.")
|
2017-11-24 10:18:29 +01:00
|
|
|
storage_size_difference += (len(key) + len(value)) - get_bot_storage_size(bot_profile, key)
|
|
|
|
new_storage_size = get_bot_storage_size(bot_profile) + storage_size_difference
|
|
|
|
if new_storage_size > storage_size_limit:
|
2017-11-20 14:49:25 +01:00
|
|
|
raise StateError("Request exceeds storage limit by {} characters. The limit is {} characters."
|
2017-11-24 10:18:29 +01:00
|
|
|
.format(new_storage_size - storage_size_limit, storage_size_limit))
|
2017-10-31 11:04:46 +01:00
|
|
|
else:
|
2017-11-20 14:54:41 +01:00
|
|
|
for key, value in entries:
|
2017-11-24 10:18:29 +01:00
|
|
|
BotStorageData.objects.update_or_create(bot_profile=bot_profile, key=key,
|
|
|
|
defaults={'value': value})
|
2017-10-31 11:04:46 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def remove_bot_storage(bot_profile: UserProfile, keys: List[str]) -> None:
|
2017-11-24 10:18:29 +01:00
|
|
|
queryset = BotStorageData.objects.filter(bot_profile=bot_profile, key__in=keys)
|
2017-11-20 14:54:41 +01:00
|
|
|
if len(queryset) < len(keys):
|
|
|
|
raise StateError("Key does not exist.")
|
|
|
|
queryset.delete()
|
2017-10-31 11:04:46 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def is_key_in_bot_storage(bot_profile: UserProfile, key: str) -> bool:
|
2017-11-24 10:18:29 +01:00
|
|
|
return BotStorageData.objects.filter(bot_profile=bot_profile, key=key).exists()
|
2017-11-20 14:40:51 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_keys_in_bot_storage(bot_profile: UserProfile) -> List[str]:
|
2017-11-24 10:18:29 +01:00
|
|
|
return list(BotStorageData.objects.filter(bot_profile=bot_profile).values_list('key', flat=True))
|