2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-03-21 20:15:27 +01:00
|
|
|
from django.http import HttpResponse, HttpResponseNotAllowed
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2012-11-06 20:31:53 +01:00
|
|
|
|
2016-12-21 13:17:53 +01:00
|
|
|
from typing import Optional, Any, Dict, List, Text
|
2016-07-12 15:10:01 +02:00
|
|
|
from zerver.lib.str_utils import force_bytes
|
2016-06-04 20:38:42 +02:00
|
|
|
|
|
|
|
|
2013-03-21 20:15:27 +01:00
|
|
|
class HttpResponseUnauthorized(HttpResponse):
|
|
|
|
status_code = 401
|
|
|
|
|
2016-05-18 03:42:07 +02:00
|
|
|
def __init__(self, realm, www_authenticate=None):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Optional[Text]) -> None
|
2013-03-21 20:15:27 +01:00
|
|
|
HttpResponse.__init__(self)
|
2016-05-18 03:42:07 +02:00
|
|
|
if www_authenticate is None:
|
|
|
|
self["WWW-Authenticate"] = 'Basic realm="%s"' % (realm,)
|
|
|
|
elif www_authenticate == "session":
|
|
|
|
self["WWW-Authenticate"] = 'Session realm="%s"' % (realm,)
|
|
|
|
else:
|
2017-03-05 09:26:07 +01:00
|
|
|
raise AssertionError("Invalid www_authenticate value!")
|
2013-03-21 20:15:27 +01:00
|
|
|
|
2016-05-18 03:42:07 +02:00
|
|
|
def json_unauthorized(message, www_authenticate=None):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Optional[Text]) -> HttpResponse
|
2016-05-18 03:42:07 +02:00
|
|
|
resp = HttpResponseUnauthorized("zulip", www_authenticate=www_authenticate)
|
2016-07-12 15:10:01 +02:00
|
|
|
resp.content = force_bytes(ujson.dumps({"result": "error",
|
|
|
|
"msg": message}) + "\n")
|
2013-07-30 23:20:16 +02:00
|
|
|
return resp
|
|
|
|
|
2013-03-21 20:15:27 +01:00
|
|
|
def json_method_not_allowed(methods):
|
2017-05-24 03:04:36 +02:00
|
|
|
# type: (List[Text]) -> HttpResponseNotAllowed
|
2013-03-21 20:15:27 +01:00
|
|
|
resp = HttpResponseNotAllowed(methods)
|
2016-07-12 15:10:01 +02:00
|
|
|
resp.content = force_bytes(ujson.dumps({"result": "error",
|
2016-12-03 00:04:17 +01:00
|
|
|
"msg": "Method Not Allowed",
|
|
|
|
"allowed_methods": methods}))
|
2013-03-21 20:15:27 +01:00
|
|
|
return resp
|
|
|
|
|
2016-06-03 01:04:39 +02:00
|
|
|
def json_response(res_type="success", msg="", data=None, status=200):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Text, Optional[Dict[str, Any]], int) -> HttpResponse
|
2012-11-06 20:31:53 +01:00
|
|
|
content = {"result": res_type, "msg": msg}
|
2016-06-03 01:04:39 +02:00
|
|
|
if data is not None:
|
|
|
|
content.update(data)
|
2013-12-04 19:42:57 +01:00
|
|
|
return HttpResponse(content=ujson.dumps(content) + "\n",
|
2015-08-19 03:41:11 +02:00
|
|
|
content_type='application/json', status=status)
|
2012-11-06 20:31:53 +01:00
|
|
|
|
2016-06-03 01:04:39 +02:00
|
|
|
def json_success(data=None):
|
2016-06-04 20:38:42 +02:00
|
|
|
# type: (Optional[Dict[str, Any]]) -> HttpResponse
|
2012-11-06 20:31:53 +01:00
|
|
|
return json_response(data=data)
|
|
|
|
|
2016-06-03 01:04:39 +02:00
|
|
|
def json_error(msg, data=None, status=400):
|
2016-06-04 20:38:42 +02:00
|
|
|
# type: (str, Optional[Dict[str, Any]], int) -> HttpResponse
|
2012-11-06 20:31:53 +01:00
|
|
|
return json_response(res_type="error", msg=msg, data=data, status=status)
|