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
|
zerver/views: Revert to Python 2 typing syntax for now (storage, streams).
This reverts commit 620b2cd6e.
Contributors setting up a new development environment were getting
errors like this:
```
++ dirname tools/do-destroy-rebuild-database
[...]
+ ./manage.py purge_queue --all
Traceback (most recent call last):
[...]
File "/home/zulipdev/zulip/zproject/legacy_urls.py", line 3, in <module>
import zerver.views.streams
File "/home/zulipdev/zulip/zerver/views/streams.py", line 187, in <module>
method_kwarg_pairs: List[FuncKwargPair]) -> HttpResponse:
File "/usr/lib/python3.5/typing.py", line 1025, in __getitem__
tvars = _type_vars(params)
[...]
File "/usr/lib/python3.5/typing.py", line 277, in _get_type_vars
for t in types:
TypeError: 'ellipsis' object is not iterable
```
The issue appears to be that we're using the `typing` module from the
3.5 stdlib, rather than the `typing=3.6.2` in our requirements files,
and that doesn't understand the `Callable[..., HttpResponse]` that
appears in the definition of `FuncKwargPair`.
Revert for now to get provision working again; at least one person
reports that reverting this sufficed. We'll need to do more testing
before putting this change back in.
2017-12-13 19:20:11 +01:00
|
|
|
def update_storage(request, user_profile, storage=REQ(validator=check_dict([]))):
|
|
|
|
# type: (HttpRequest, UserProfile, Optional[Dict[str, str]]) -> 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
|
2017-11-24 10:18:29 +01:00
|
|
|
def get_storage(request, user_profile, keys=REQ(validator=check_list(check_string), default=None)):
|
2017-11-20 14:40:51 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Optional[List[str]]) -> 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
|
2017-11-24 10:18:29 +01:00
|
|
|
def remove_storage(request, user_profile, keys=REQ(validator=check_list(check_string), default=None)):
|
2017-11-20 14:40:51 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Optional[List[str]]) -> 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()
|