2017-11-20 14:40:51 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from zerver.lib.bot_storage import (
|
2017-11-24 10:18:29 +01:00
|
|
|
get_bot_storage,
|
|
|
|
set_bot_storage,
|
|
|
|
remove_bot_storage,
|
|
|
|
get_keys_in_bot_storage,
|
|
|
|
is_key_in_bot_storage,
|
2017-11-20 14:40:51 +01:00
|
|
|
StateError,
|
|
|
|
)
|
|
|
|
from zerver.decorator import has_request_variables, REQ
|
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.lib.validator import check_dict, check_list, check_string
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
from typing import Dict, List, Optional
|
|
|
|
|
|
|
|
@has_request_variables
|
2018-03-12 00:47:54 +01:00
|
|
|
def update_storage(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
storage: Dict[str, str]=REQ(validator=check_dict([]))) -> HttpResponse:
|
2017-11-20 14:40:51 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
set_bot_storage(user_profile, list(storage.items()))
|
2017-11-20 14:40:51 +01:00
|
|
|
except StateError as e:
|
|
|
|
return json_error(str(e))
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
@has_request_variables
|
2018-03-12 00:47:54 +01:00
|
|
|
def get_storage(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
keys: Optional[List[str]]=REQ(validator=check_list(check_string), default=None)
|
|
|
|
) -> HttpResponse:
|
2017-11-24 10:18:29 +01:00
|
|
|
keys = keys or get_keys_in_bot_storage(user_profile)
|
2017-11-20 14:40:51 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
storage = {key: get_bot_storage(user_profile, key) for key in keys}
|
2017-11-20 14:40:51 +01:00
|
|
|
except StateError as e:
|
|
|
|
return json_error(str(e))
|
2017-11-24 10:18:29 +01:00
|
|
|
return json_success({'storage': storage})
|
2017-11-20 14:40:51 +01:00
|
|
|
|
|
|
|
@has_request_variables
|
2018-03-12 00:47:54 +01:00
|
|
|
def remove_storage(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
keys: Optional[List[str]]=REQ(validator=check_list(check_string), default=None)
|
|
|
|
) -> HttpResponse:
|
2017-11-24 10:18:29 +01:00
|
|
|
keys = keys or get_keys_in_bot_storage(user_profile)
|
2017-11-20 14:40:51 +01:00
|
|
|
try:
|
2017-11-24 10:18:29 +01:00
|
|
|
remove_bot_storage(user_profile, keys)
|
2017-11-20 14:40:51 +01:00
|
|
|
except StateError as e:
|
|
|
|
return json_error(str(e))
|
|
|
|
return json_success()
|