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
|
2017-11-24 10:18:29 +01:00
|
|
|
from zerver.models import BotStorageData, UserProfile, Length
|
2017-10-27 17:25:51 +02:00
|
|
|
|
2017-11-20 14:54:41 +01:00
|
|
|
from typing import Text, 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
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_bot_storage(bot_profile: UserProfile, key: Text) -> Text:
|
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
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_bot_storage_size(bot_profile: UserProfile, key: Optional[Text]=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:
|
|
|
|
if type(key) is not str:
|
|
|
|
raise StateError("Key type is {}, but should be str.".format(type(key)))
|
|
|
|
if type(value) is not str:
|
|
|
|
raise StateError("Value type is {}, but should be str.".format(type(value)))
|
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
|
|
|
|
2017-11-27 05:27:04 +01:00
|
|
|
def remove_bot_storage(bot_profile: UserProfile, keys: List[Text]) -> 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
|
|
|
|
2017-11-27 05:27:04 +01:00
|
|
|
def is_key_in_bot_storage(bot_profile: UserProfile, key: Text) -> 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
|
|
|
|
2017-11-27 05:27:04 +01:00
|
|
|
def get_keys_in_bot_storage(bot_profile: UserProfile) -> List[Text]:
|
2017-11-24 10:18:29 +01:00
|
|
|
return list(BotStorageData.objects.filter(bot_profile=bot_profile).values_list('key', flat=True))
|