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
|
|
|
|
|
|
|
|
from zerver.lib import bugdown
|
2017-11-20 03:27:04 +01:00
|
|
|
from zerver.lib.integrations import CATEGORIES, INTEGRATIONS, HubotIntegration, \
|
2018-01-20 19:39:06 +01:00
|
|
|
WebhookIntegration, EmailIntegration
|
2017-10-28 00:07:31 +02:00
|
|
|
from zerver.lib.request import has_request_variables, REQ
|
2017-10-19 07:21:57 +02:00
|
|
|
from zerver.lib.subdomains import get_subdomain
|
2017-10-20 02:56:49 +02:00
|
|
|
from zerver.models import Realm
|
2017-07-12 02:50:27 +02:00
|
|
|
from zerver.templatetags.app_filters import render_markdown_path
|
2018-06-01 20:31:16 +02:00
|
|
|
from zerver.context_processors import zulip_default_context
|
2016-07-29 15:06:41 +02:00
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def add_api_uri_context(context: Dict[str, Any], request: HttpRequest) -> None:
|
2018-06-01 20:31:16 +02:00
|
|
|
context.update(zulip_default_context(request))
|
|
|
|
|
2017-10-02 07:59:20 +02:00
|
|
|
subdomain = get_subdomain(request)
|
2017-10-20 02:56:49 +02:00
|
|
|
if (subdomain != Realm.SUBDOMAIN_FOR_ROOT_DOMAIN
|
|
|
|
or not settings.ROOT_DOMAIN_LANDING_PAGE):
|
2017-10-02 07:59:20 +02:00
|
|
|
display_subdomain = subdomain
|
|
|
|
html_settings_links = True
|
|
|
|
else:
|
|
|
|
display_subdomain = 'yourZulipDomain'
|
|
|
|
html_settings_links = False
|
2016-07-19 14:35:08 +02:00
|
|
|
|
2017-10-30 22:13:13 +01:00
|
|
|
display_host = Realm.host_for_subdomain(display_subdomain)
|
|
|
|
api_url_scheme_relative = display_host + "/api"
|
|
|
|
api_url = settings.EXTERNAL_URI_SCHEME + api_url_scheme_relative
|
2016-09-14 07:07:21 +02:00
|
|
|
|
2018-02-17 23:04:17 +01:00
|
|
|
context['external_uri_scheme'] = settings.EXTERNAL_URI_SCHEME
|
2017-10-30 22:04:15 +01:00
|
|
|
context['api_url'] = api_url
|
2017-10-30 22:13:13 +01:00
|
|
|
context['api_url_scheme_relative'] = api_url_scheme_relative
|
2018-04-05 20:31:43 +02:00
|
|
|
|
2016-07-19 14:35:08 +02:00
|
|
|
context["html_settings_links"] = html_settings_links
|
2018-04-05 20:31:43 +02:00
|
|
|
if html_settings_links:
|
2018-04-05 23:39:35 +02:00
|
|
|
settings_html = '<a href="/#settings">Zulip settings page</a>'
|
|
|
|
subscriptions_html = '<a target="_blank" href="/#streams">streams page</a>'
|
2018-04-05 20:31:43 +02:00
|
|
|
else:
|
|
|
|
settings_html = 'Zulip settings page'
|
|
|
|
subscriptions_html = 'streams page'
|
|
|
|
context['settings_html'] = settings_html
|
|
|
|
context['subscriptions_html'] = subscriptions_html
|
2016-09-14 07:07:21 +02:00
|
|
|
|
2016-09-13 19:09:03 +02:00
|
|
|
class ApiURLView(TemplateView):
|
2017-11-27 09:28:57 +01:00
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, str]:
|
2017-10-27 08:28:23 +02:00
|
|
|
context = super().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'
|
|
|
|
|
|
|
|
|
2017-07-25 02:35:22 +02:00
|
|
|
class MarkdownDirectoryView(ApiURLView):
|
|
|
|
path_template = ""
|
2016-11-09 01:45:36 +01:00
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def get_path(self, article: str) -> str:
|
2016-11-09 01:45:36 +01:00
|
|
|
if article == "":
|
|
|
|
article = "index"
|
2018-08-18 15:42:01 +02:00
|
|
|
elif article == "include/sidebar_index":
|
|
|
|
pass
|
2017-10-02 19:51:17 +02:00
|
|
|
elif "/" in article:
|
|
|
|
article = "missing"
|
2016-11-09 01:45:36 +01:00
|
|
|
return self.path_template % (article,)
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
2016-11-09 01:45:36 +01:00
|
|
|
article = kwargs["article"]
|
2017-07-25 02:35:22 +02:00
|
|
|
context = super().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")
|
2018-08-18 15:42:01 +02:00
|
|
|
if self.path_template == '/zerver/help/%s.md':
|
2017-11-08 22:08:23 +01:00
|
|
|
context["page_is_help_center"] = True
|
2018-08-18 15:42:01 +02:00
|
|
|
context["doc_root"] = "/help/"
|
|
|
|
sidebar_index = self.get_path("include/sidebar_index")
|
|
|
|
# We want the sliding/collapsing behavior for /help pages only
|
|
|
|
sidebar_class = "sidebar slide"
|
2017-11-08 22:08:23 +01:00
|
|
|
else:
|
|
|
|
context["page_is_api_center"] = True
|
2018-08-18 15:42:01 +02:00
|
|
|
context["doc_root"] = "/api/"
|
|
|
|
sidebar_index = self.get_path("sidebar_index")
|
|
|
|
sidebar_class = "sidebar"
|
|
|
|
|
|
|
|
context["sidebar_index"] = sidebar_index
|
|
|
|
context["sidebar_class"] = sidebar_class
|
2017-11-08 05:39:42 +01:00
|
|
|
# An "article" might require the api_uri_context to be rendered
|
|
|
|
api_uri_context = {} # type: Dict[str, Any]
|
|
|
|
add_api_uri_context(api_uri_context, self.request)
|
|
|
|
context["api_uri_context"] = api_uri_context
|
2016-11-09 01:45:36 +01:00
|
|
|
return context
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def get(self, request: HttpRequest, article: str="") -> HttpResponse:
|
2016-11-09 01:45:36 +01:00
|
|
|
path = self.get_path(article)
|
2017-07-25 02:35:22 +02:00
|
|
|
result = super().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
|
2017-10-02 19:51:17 +02:00
|
|
|
if "/" in article:
|
|
|
|
result.status_code = 404
|
2016-11-09 01:45:36 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def add_integrations_context(context: 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()))
|
2018-02-06 16:55:20 +01:00
|
|
|
enabled_integrations_count = len(list(filter(lambda v: v.is_enabled(), INTEGRATIONS.values())))
|
|
|
|
# Subtract 1 so saying "Over X integrations" is correct. Then,
|
|
|
|
# round down to the nearest multiple of 10.
|
|
|
|
integrations_count_display = ((enabled_integrations_count - 1) // 10) * 10
|
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
|
2018-02-06 16:55:20 +01:00
|
|
|
context['integrations_count_display'] = integrations_count_display
|
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
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
2017-10-27 08:28:23 +02:00
|
|
|
context = super().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
|
2017-11-27 09:28:57 +01:00
|
|
|
def integration_doc(request: HttpRequest, integration_name: str=REQ(default=None)) -> HttpResponse:
|
2018-05-11 16:35:03 +02:00
|
|
|
if not request.is_ajax():
|
|
|
|
return HttpResponseNotFound()
|
2017-07-12 02:50:27 +02:00
|
|
|
try:
|
|
|
|
integration = INTEGRATIONS[integration_name]
|
|
|
|
except KeyError:
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
2017-11-22 03:07:51 +01:00
|
|
|
context = {} # type: Dict[str, Any]
|
2018-04-05 20:31:43 +02:00
|
|
|
add_api_uri_context(context, request)
|
2017-11-20 03:27:04 +01:00
|
|
|
|
|
|
|
context['integration_name'] = integration.name
|
|
|
|
context['integration_display_name'] = integration.display_name
|
|
|
|
if hasattr(integration, 'stream_name'):
|
|
|
|
context['recommended_stream_name'] = integration.stream_name
|
|
|
|
if isinstance(integration, WebhookIntegration):
|
|
|
|
context['integration_url'] = integration.url[3:]
|
|
|
|
if isinstance(integration, HubotIntegration):
|
|
|
|
context['hubot_docs_url'] = integration.hubot_docs_url
|
2018-01-20 19:39:06 +01:00
|
|
|
if isinstance(integration, EmailIntegration):
|
|
|
|
context['email_gateway_example'] = settings.EMAIL_GATEWAY_EXAMPLE
|
2018-08-14 22:27:03 +02:00
|
|
|
if integration.name == 'freshdesk':
|
|
|
|
# In our Freshdesk docs, some nested code blocks have characters such
|
|
|
|
# as '{' encoded as '{' to prevent clashes with Jinja2 syntax,
|
|
|
|
# but the encoded form never gets rendered because the text ends up
|
|
|
|
# inside a <pre> tag. So here, we explicitly set a directive that
|
|
|
|
# a particular template should be "unescaped" before being displayed.
|
|
|
|
# Note that this value is used by render_markdown_path.
|
|
|
|
context['unescape_rendered_html'] = True
|
2017-11-20 03:27:04 +01:00
|
|
|
|
2017-07-12 02:50:27 +02:00
|
|
|
doc_html_str = render_markdown_path(integration.doc, context)
|
|
|
|
|
|
|
|
return HttpResponse(doc_html_str)
|