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
|
2017-04-03 11:49:13 +02:00
|
|
|
from django.template import loader
|
2017-03-16 14:24:33 +01:00
|
|
|
from django.shortcuts import render
|
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
|
|
|
|
|
2017-07-12 02:50:27 +02:00
|
|
|
from zerver.decorator import has_request_variables, REQ
|
2016-09-13 19:18:22 +02:00
|
|
|
from zerver.lib import bugdown
|
2017-07-05 22:07:46 +02:00
|
|
|
from zerver.lib.integrations import CATEGORIES, INTEGRATIONS, HUBOT_LOZENGES
|
2016-07-19 14:35:08 +02:00
|
|
|
from zerver.lib.utils import get_subdomain
|
2017-07-12 02:50:27 +02:00
|
|
|
from zerver.templatetags.app_filters import render_markdown_path
|
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
|
2017-10-02 07:59:20 +02:00
|
|
|
subdomain = get_subdomain(request)
|
|
|
|
if subdomain or not settings.ROOT_DOMAIN_LANDING_PAGE:
|
|
|
|
display_subdomain = subdomain
|
|
|
|
html_settings_links = True
|
|
|
|
else:
|
|
|
|
display_subdomain = 'yourZulipDomain'
|
|
|
|
html_settings_links = False
|
|
|
|
if display_subdomain != "":
|
|
|
|
external_api_path_subdomain = '%s.%s' % (display_subdomain,
|
|
|
|
settings.EXTERNAL_API_PATH)
|
2016-07-19 14:35:08 +02:00
|
|
|
else:
|
|
|
|
external_api_path_subdomain = settings.EXTERNAL_API_PATH
|
|
|
|
|
|
|
|
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):
|
2017-02-19 03:39:27 +01:00
|
|
|
# type: (**Any) -> Dict[str, str]
|
2016-09-13 19:09:03 +02:00
|
|
|
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'
|
2017-04-03 11:49:13 +02:00
|
|
|
path_template = 'zerver/help/%s.md'
|
2016-11-09 01:45:36 +01:00
|
|
|
|
|
|
|
def get_path(self, article):
|
2017-02-19 03:39:27 +01:00
|
|
|
# type: (str) -> str
|
2016-11-09 01:45:36 +01:00
|
|
|
if article == "":
|
|
|
|
article = "index"
|
|
|
|
return self.path_template % (article,)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-02-19 00:29:29 +01:00
|
|
|
# type: (**Any) -> Dict[str, Any]
|
2016-11-09 01:45:36 +01:00
|
|
|
article = kwargs["article"]
|
2017-02-19 00:29:29 +01:00
|
|
|
context = super(HelpView, self).get_context_data() # type: Dict[str, Any]
|
2016-11-09 01:45:36 +01:00
|
|
|
path = self.get_path(article)
|
2017-04-03 11:49:13 +02:00
|
|
|
try:
|
|
|
|
loader.get_template(path)
|
2016-11-09 01:45:36 +01:00
|
|
|
context["article"] = path
|
2017-04-03 11:49:13 +02:00
|
|
|
except loader.TemplateDoesNotExist:
|
2016-11-09 01:45:36 +01:00
|
|
|
context["article"] = self.get_path("missing")
|
2017-04-03 11:49:13 +02:00
|
|
|
|
2017-02-18 23:58:54 +01:00
|
|
|
# For disabling the "Back to home" on the homepage
|
|
|
|
context["not_index_page"] = not path.endswith("/index.md")
|
2017-08-04 02:11:50 +02:00
|
|
|
context["page_is_help_center"] = True
|
2016-11-09 01:45:36 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get(self, request, article=""):
|
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
|
|
|
path = self.get_path(article)
|
|
|
|
result = super(HelpView, self).get(self, article=article)
|
2017-04-03 11:49:13 +02:00
|
|
|
try:
|
|
|
|
loader.get_template(path)
|
|
|
|
except loader.TemplateDoesNotExist:
|
2016-11-09 01:45:36 +01:00
|
|
|
# Ensure a 404 response code if no such document
|
|
|
|
result.status_code = 404
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-02-28 07:18:45 +01:00
|
|
|
def add_integrations_context(context):
|
|
|
|
# type: (Dict[str, Any]) -> None
|
2017-07-05 22:07:46 +02:00
|
|
|
alphabetical_sorted_categories = OrderedDict(sorted(CATEGORIES.items()))
|
2017-02-28 07:18:45 +01:00
|
|
|
alphabetical_sorted_integration = OrderedDict(sorted(INTEGRATIONS.items()))
|
|
|
|
alphabetical_sorted_hubot_lozenges = OrderedDict(sorted(HUBOT_LOZENGES.items()))
|
2017-07-05 22:07:46 +02:00
|
|
|
context['categories_dict'] = alphabetical_sorted_categories
|
2017-02-28 07:18:45 +01:00
|
|
|
context['integrations_dict'] = alphabetical_sorted_integration
|
|
|
|
context['hubot_lozenges_dict'] = alphabetical_sorted_hubot_lozenges
|
|
|
|
|
2017-07-12 02:50:27 +02:00
|
|
|
if "html_settings_links" in context and context["html_settings_links"]:
|
2017-08-01 03:12:24 +02:00
|
|
|
settings_html = '<a href="../../#settings">Zulip settings page</a>'
|
|
|
|
subscriptions_html = '<a target="_blank" href="../../#streams">streams page</a>'
|
2017-02-28 07:18:45 +01:00
|
|
|
else:
|
|
|
|
settings_html = 'Zulip settings page'
|
2017-03-09 00:20:22 +01:00
|
|
|
subscriptions_html = 'streams page'
|
2017-02-28 07:18:45 +01:00
|
|
|
|
|
|
|
context['settings_html'] = settings_html
|
|
|
|
context['subscriptions_html'] = subscriptions_html
|
|
|
|
|
2017-04-05 07:21:42 +02:00
|
|
|
for name in alphabetical_sorted_integration:
|
|
|
|
alphabetical_sorted_integration[name].add_doc_context(context)
|
|
|
|
|
|
|
|
for name in alphabetical_sorted_hubot_lozenges:
|
|
|
|
alphabetical_sorted_hubot_lozenges[name].add_doc_context(context)
|
|
|
|
|
2017-02-28 07:18:45 +01:00
|
|
|
|
2016-09-13 19:09:03 +02:00
|
|
|
class IntegrationView(ApiURLView):
|
2017-03-31 07:40:52 +02:00
|
|
|
template_name = 'zerver/integrations/index.html'
|
2016-07-29 15:06:41 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-02-19 03:39:27 +01:00
|
|
|
# type: (**Any) -> Dict[str, Any]
|
2016-09-13 19:09:03 +02:00
|
|
|
context = super(IntegrationView, self).get_context_data(**kwargs) # type: Dict[str, Any]
|
2017-02-28 07:18:45 +01:00
|
|
|
add_integrations_context(context)
|
2016-07-29 15:06:41 +02:00
|
|
|
return context
|
2016-09-13 19:18:22 +02:00
|
|
|
|
|
|
|
|
2017-07-12 02:50:27 +02:00
|
|
|
@has_request_variables
|
|
|
|
def integration_doc(request, integration_name=REQ(default=None)):
|
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
|
|
|
try:
|
|
|
|
integration = INTEGRATIONS[integration_name]
|
|
|
|
except KeyError:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
context = integration.doc_context or {}
|
|
|
|
add_integrations_context(context)
|
|
|
|
doc_html_str = render_markdown_path(integration.doc, context)
|
|
|
|
|
|
|
|
return HttpResponse(doc_html_str)
|
|
|
|
|
2016-09-13 19:18:22 +02:00
|
|
|
def api_endpoint_docs(request):
|
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2017-05-17 22:10:22 +02:00
|
|
|
context = {} # type: Dict[str, Any]
|
2016-08-14 09:44:12 +02:00
|
|
|
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
|
2017-01-22 06:29:11 +01:00
|
|
|
call['rendered_response'] = bugdown.convert("~~~ .py\n" + extended_response + "\n~~~\n")
|
2016-09-13 19:18:22 +02:00
|
|
|
for example_type in ('request', 'response'):
|
|
|
|
for lang in call.get('example_' + example_type, []):
|
|
|
|
langs.add(lang)
|
2017-03-16 14:24:33 +01:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
'zerver/api_endpoints.html',
|
|
|
|
context={'content': calls, 'langs': langs},
|
|
|
|
)
|