2016-09-13 19:18:22 +02:00
|
|
|
from __future__ import absolute_import
|
2016-09-11 23:57:44 +02:00
|
|
|
from typing import Optional, Any, Dict
|
2016-07-29 15:06:41 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
from django.views.generic import TemplateView
|
2016-08-14 03:32:11 +02:00
|
|
|
from django.conf import settings
|
2016-11-09 01:45:36 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseNotFound
|
2016-07-29 15:06:41 +02:00
|
|
|
|
2016-11-09 01:45:36 +01:00
|
|
|
import os
|
2016-09-13 19:18:22 +02:00
|
|
|
import ujson
|
|
|
|
|
|
|
|
from zerver.lib import bugdown
|
2016-11-23 18:58:59 +01:00
|
|
|
from zerver.lib.integrations import INTEGRATIONS, HUBOT_LOZENGES
|
2016-07-19 14:35:08 +02:00
|
|
|
from zerver.lib.utils import get_subdomain
|
2016-09-13 19:18:22 +02:00
|
|
|
from zproject.jinja2 import render_to_response
|
2016-07-29 15:06:41 +02:00
|
|
|
|
2016-09-14 07:07:21 +02:00
|
|
|
def add_api_uri_context(context, request):
|
|
|
|
# type: (Dict[str, Any], HttpRequest) -> None
|
2016-07-19 14:35:08 +02:00
|
|
|
if settings.REALMS_HAVE_SUBDOMAINS:
|
|
|
|
subdomain = get_subdomain(request)
|
|
|
|
if subdomain:
|
|
|
|
display_subdomain = subdomain
|
|
|
|
html_settings_links = True
|
|
|
|
else:
|
|
|
|
display_subdomain = 'yourZulipDomain'
|
|
|
|
html_settings_links = False
|
|
|
|
external_api_path_subdomain = '%s.%s' % (display_subdomain,
|
|
|
|
settings.EXTERNAL_API_PATH)
|
|
|
|
else:
|
|
|
|
external_api_path_subdomain = settings.EXTERNAL_API_PATH
|
|
|
|
html_settings_links = True
|
|
|
|
|
|
|
|
external_api_uri_subdomain = '%s%s' % (settings.EXTERNAL_URI_SCHEME,
|
|
|
|
external_api_path_subdomain)
|
2016-09-14 07:07:21 +02:00
|
|
|
|
|
|
|
context['external_api_path_subdomain'] = external_api_path_subdomain
|
|
|
|
context['external_api_uri_subdomain'] = external_api_uri_subdomain
|
2016-07-19 14:35:08 +02:00
|
|
|
context["html_settings_links"] = html_settings_links
|
2016-09-14 07:07:21 +02:00
|
|
|
|
2016-09-13 19:09:03 +02:00
|
|
|
class ApiURLView(TemplateView):
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
# type: (Optional[Dict[str, Any]]) -> Dict[str, str]
|
|
|
|
context = super(ApiURLView, self).get_context_data(**kwargs)
|
2016-09-14 07:07:21 +02:00
|
|
|
add_api_uri_context(context, self.request)
|
2016-09-13 19:09:03 +02:00
|
|
|
return context
|
|
|
|
|
2016-08-14 09:44:12 +02:00
|
|
|
class APIView(ApiURLView):
|
|
|
|
template_name = 'zerver/api.html'
|
|
|
|
|
|
|
|
|
2016-11-09 01:45:36 +01:00
|
|
|
class HelpView(ApiURLView):
|
|
|
|
template_name = 'zerver/help/main.html'
|
|
|
|
path_template = os.path.join(settings.DEPLOY_ROOT, 'templates/zerver/help/%s.md')
|
|
|
|
|
|
|
|
def get_path(self, article):
|
|
|
|
# type: (**Any) -> str
|
|
|
|
if article == "":
|
|
|
|
article = "index"
|
|
|
|
return self.path_template % (article,)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
# type: (**Any) -> Dict[str, str]
|
|
|
|
article = kwargs["article"]
|
|
|
|
context = super(HelpView, self).get_context_data()
|
|
|
|
path = self.get_path(article)
|
|
|
|
if os.path.exists(path):
|
|
|
|
context["article"] = path
|
|
|
|
else:
|
|
|
|
context["article"] = self.get_path("missing")
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get(self, request, article=""):
|
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
|
|
|
path = self.get_path(article)
|
|
|
|
result = super(HelpView, self).get(self, article=article)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
# Ensure a 404 response code if no such document
|
|
|
|
result.status_code = 404
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2016-09-13 19:09:03 +02:00
|
|
|
class IntegrationView(ApiURLView):
|
2016-07-29 15:06:41 +02:00
|
|
|
template_name = 'zerver/integrations.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2016-09-13 19:09:03 +02:00
|
|
|
# type: (Optional[Dict[str, Any]]) -> Dict[str, Any]
|
|
|
|
context = super(IntegrationView, self).get_context_data(**kwargs) # type: Dict[str, Any]
|
2016-07-29 15:06:41 +02:00
|
|
|
alphabetical_sorted_integration = OrderedDict(sorted(INTEGRATIONS.items()))
|
2016-11-23 18:58:59 +01:00
|
|
|
alphabetical_sorted_hubot_lozenges = OrderedDict(sorted(HUBOT_LOZENGES.items()))
|
2016-07-29 15:06:41 +02:00
|
|
|
context['integrations_dict'] = alphabetical_sorted_integration
|
2016-11-23 18:58:59 +01:00
|
|
|
context['hubot_lozenges_dict'] = alphabetical_sorted_hubot_lozenges
|
2016-08-14 03:32:11 +02:00
|
|
|
|
2016-07-19 14:35:08 +02:00
|
|
|
if context["html_settings_links"]:
|
|
|
|
settings_html = '<a href="../#settings">Zulip settings page</a>'
|
|
|
|
subscriptions_html = '<a target="_blank" href="../#subscriptions">subscriptions page</a>'
|
|
|
|
else:
|
|
|
|
settings_html = 'Zulip settings page'
|
|
|
|
subscriptions_html = 'subscriptions page'
|
2016-08-14 03:32:11 +02:00
|
|
|
|
|
|
|
context['settings_html'] = settings_html
|
|
|
|
context['subscriptions_html'] = subscriptions_html
|
|
|
|
|
2016-07-29 15:06:41 +02:00
|
|
|
return context
|
2016-09-13 19:18:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def api_endpoint_docs(request):
|
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2016-08-14 09:44:12 +02:00
|
|
|
context = {} # type: Dict[str, Any]
|
|
|
|
add_api_uri_context(context, request)
|
|
|
|
|
2016-09-13 19:18:22 +02:00
|
|
|
raw_calls = open('templates/zerver/api_content.json', 'r').read()
|
|
|
|
calls = ujson.loads(raw_calls)
|
|
|
|
langs = set()
|
|
|
|
for call in calls:
|
2016-08-14 09:44:12 +02:00
|
|
|
call["endpoint"] = "%s/v1/%s" % (context["external_api_uri_subdomain"],
|
|
|
|
call["endpoint"])
|
2016-09-13 19:18:22 +02:00
|
|
|
call["example_request"]["curl"] = call["example_request"]["curl"].replace("https://api.zulip.com",
|
2016-08-14 09:44:12 +02:00
|
|
|
context["external_api_uri_subdomain"])
|
2016-09-13 19:18:22 +02:00
|
|
|
response = call['example_response']
|
|
|
|
if '\n' not in response:
|
|
|
|
# For 1-line responses, pretty-print them
|
|
|
|
extended_response = response.replace(", ", ",\n ")
|
|
|
|
else:
|
|
|
|
extended_response = response
|
|
|
|
call['rendered_response'] = bugdown.convert("~~~ .py\n" + extended_response + "\n~~~\n", "default")
|
|
|
|
for example_type in ('request', 'response'):
|
|
|
|
for lang in call.get('example_' + example_type, []):
|
|
|
|
langs.add(lang)
|
|
|
|
return render_to_response(
|
|
|
|
'zerver/api_endpoints.html', {
|
|
|
|
'content': calls,
|
|
|
|
'langs': langs,
|
|
|
|
},
|
|
|
|
request=request)
|