integrations: Get logo URLs from staticfiles.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg
2019-07-16 16:52:30 -07:00
committed by Tim Abbott
parent 88b29c64ad
commit a97a2612bb
3 changed files with 32 additions and 38 deletions

View File

@@ -110,7 +110,7 @@
{% endif %} {% endif %}
data-categories="{{ integration.categories }}" data-categories="{{ integration.categories }}"
data-name="{{ integration.name }}"> data-name="{{ integration.name }}">
<img class="integration-logo" src="/{{ integration.logo }}" <img class="integration-logo" src="{{ integration.logo_url }}"
alt="{{ integration.display_name }} logo"/> alt="{{ integration.display_name }} logo"/>
{% if integration.secondary_line_text %} {% if integration.secondary_line_text %}
<h3 class="integration-name with-secondary">{{ integration.display_name }}</h3> <h3 class="integration-name with-secondary">{{ integration.display_name }}</h3>

View File

@@ -1,9 +1,9 @@
import os import os
import pathlib
from typing import Dict, List, Optional, Any from typing import Dict, List, Optional, Any
from django.conf import settings from django.conf import settings
from django.conf.urls import url from django.conf.urls import url
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls.resolvers import LocaleRegexProvider from django.urls.resolvers import LocaleRegexProvider
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@@ -46,8 +46,8 @@ CATEGORIES = {
} # type: Dict[str, str] } # type: Dict[str, str]
class Integration: class Integration:
DEFAULT_LOGO_STATIC_PATH_PNG = 'static/images/integrations/logos/{name}.png' DEFAULT_LOGO_STATIC_PATH_PNG = 'images/integrations/logos/{name}.png'
DEFAULT_LOGO_STATIC_PATH_SVG = 'static/images/integrations/logos/{name}.svg' DEFAULT_LOGO_STATIC_PATH_SVG = 'images/integrations/logos/{name}.svg'
def __init__(self, name: str, client_name: str, categories: List[str], def __init__(self, name: str, client_name: str, categories: List[str],
logo: Optional[str]=None, secondary_line_text: Optional[str]=None, logo: Optional[str]=None, secondary_line_text: Optional[str]=None,
@@ -68,8 +68,9 @@ class Integration:
self.categories = list(map((lambda c: CATEGORIES[c]), categories)) self.categories = list(map((lambda c: CATEGORIES[c]), categories))
if logo is None: if logo is None:
logo = self.get_logo_url() self.logo_url = self.get_logo_url()
self.logo = logo else:
self.logo_url = staticfiles_storage.url(logo)
if display_name is None: if display_name is None:
display_name = name.title() display_name = name.title()
@@ -83,25 +84,19 @@ class Integration:
return True return True
def get_logo_url(self) -> Optional[str]: def get_logo_url(self) -> Optional[str]:
logo_file_path_svg = str(pathlib.PurePath( logo_file_path_svg = self.DEFAULT_LOGO_STATIC_PATH_SVG.format(name=self.name)
settings.STATIC_ROOT, logo_file_path_png = self.DEFAULT_LOGO_STATIC_PATH_PNG.format(name=self.name)
*self.DEFAULT_LOGO_STATIC_PATH_SVG.format(name=self.name).split('/')[1:] if os.path.isfile(os.path.join(settings.STATIC_ROOT, logo_file_path_svg)):
)) return staticfiles_storage.url(logo_file_path_svg)
logo_file_path_png = str(pathlib.PurePath( elif os.path.isfile(os.path.join(settings.STATIC_ROOT, logo_file_path_png)):
settings.STATIC_ROOT, return staticfiles_storage.url(logo_file_path_png)
*self.DEFAULT_LOGO_STATIC_PATH_PNG.format(name=self.name).split('/')[1:]
))
if os.path.isfile(logo_file_path_svg):
return self.DEFAULT_LOGO_STATIC_PATH_SVG.format(name=self.name)
elif os.path.isfile(logo_file_path_png):
return self.DEFAULT_LOGO_STATIC_PATH_PNG.format(name=self.name)
return None return None
class BotIntegration(Integration): class BotIntegration(Integration):
DEFAULT_LOGO_STATIC_PATH_PNG = 'static/generated/bots/{name}/logo.png' DEFAULT_LOGO_STATIC_PATH_PNG = 'generated/bots/{name}/logo.png'
DEFAULT_LOGO_STATIC_PATH_SVG = 'static/generated/bots/{name}/logo.svg' DEFAULT_LOGO_STATIC_PATH_SVG = 'generated/bots/{name}/logo.svg'
ZULIP_LOGO_STATIC_PATH_PNG = 'static/images/logo/zulip-icon-128x128.png' ZULIP_LOGO_STATIC_PATH_PNG = 'images/logo/zulip-icon-128x128.png'
DEFAULT_DOC_PATH = '{name}/doc.md' DEFAULT_DOC_PATH = '{name}/doc.md'
def __init__(self, name: str, categories: List[str], logo: Optional[str]=None, def __init__(self, name: str, categories: List[str], logo: Optional[str]=None,
@@ -115,13 +110,12 @@ class BotIntegration(Integration):
) )
if logo is None: if logo is None:
logo_url = self.get_logo_url() self.logo_url = self.get_logo_url()
if logo_url is not None: if self.logo_url is None:
logo = logo_url
else:
# TODO: Add a test for this by initializing one in a test. # TODO: Add a test for this by initializing one in a test.
logo = self.ZULIP_LOGO_STATIC_PATH_PNG # nocoverage logo = staticfiles_storage.url(self.ZULIP_LOGO_STATIC_PATH_PNG) # nocoverage
self.logo = logo else:
self.logo_url = staticfiles_storage.url(logo)
if display_name is None: if display_name is None:
display_name = "{} Bot".format(name.title()) # nocoverage display_name = "{} Bot".format(name.title()) # nocoverage
@@ -237,14 +231,14 @@ WEBHOOK_INTEGRATIONS = [
WebhookIntegration( WebhookIntegration(
'bitbucket3', 'bitbucket3',
['version-control'], ['version-control'],
logo='static/images/integrations/logos/bitbucket.svg', logo='images/integrations/logos/bitbucket.svg',
display_name='Bitbucket Server', display_name='Bitbucket Server',
stream_name='bitbucket' stream_name='bitbucket'
), ),
WebhookIntegration( WebhookIntegration(
'bitbucket2', 'bitbucket2',
['version-control'], ['version-control'],
logo='static/images/integrations/logos/bitbucket.svg', logo='images/integrations/logos/bitbucket.svg',
display_name='Bitbucket', display_name='Bitbucket',
stream_name='bitbucket' stream_name='bitbucket'
), ),
@@ -266,7 +260,7 @@ WEBHOOK_INTEGRATIONS = [
WebhookIntegration( WebhookIntegration(
'deskdotcom', 'deskdotcom',
['customer-support'], ['customer-support'],
logo='static/images/integrations/logos/deskcom.png', logo='images/integrations/logos/deskcom.png',
display_name='Desk.com', display_name='Desk.com',
stream_name='desk' stream_name='desk'
), ),
@@ -278,7 +272,7 @@ WEBHOOK_INTEGRATIONS = [
'github', 'github',
['version-control'], ['version-control'],
display_name='GitHub', display_name='GitHub',
logo='static/images/integrations/logos/github.svg', logo='images/integrations/logos/github.svg',
function='zerver.webhooks.github.view.api_github_webhook', function='zerver.webhooks.github.view.api_github_webhook',
stream_name='github' stream_name='github'
), ),
@@ -386,7 +380,7 @@ INTEGRATIONS = {
'jira-plugin', 'jira-plugin',
'jira-plugin', 'jira-plugin',
['project-management'], ['project-management'],
logo='static/images/integrations/logos/jira.svg', logo='images/integrations/logos/jira.svg',
secondary_line_text='(locally installed)', secondary_line_text='(locally installed)',
display_name='JIRA', display_name='JIRA',
doc='zerver/integrations/jira-plugin.md', doc='zerver/integrations/jira-plugin.md',
@@ -427,7 +421,7 @@ INTEGRATIONS = {
'trello-plugin', 'trello-plugin',
'trello-plugin', 'trello-plugin',
['project-management'], ['project-management'],
logo='static/images/integrations/logos/trello.svg', logo='images/integrations/logos/trello.svg',
secondary_line_text='(legacy)', secondary_line_text='(legacy)',
display_name='Trello', display_name='Trello',
doc='zerver/integrations/trello-plugin.md', doc='zerver/integrations/trello-plugin.md',
@@ -436,7 +430,7 @@ INTEGRATIONS = {
), ),
'twitter': Integration('twitter', 'twitter', ['customer-support', 'marketing'], 'twitter': Integration('twitter', 'twitter', ['customer-support', 'marketing'],
# _ needed to get around adblock plus # _ needed to get around adblock plus
logo='static/images/integrations/logos/twitte_r.svg', logo='images/integrations/logos/twitte_r.svg',
doc='zerver/integrations/twitter.md'), doc='zerver/integrations/twitter.md'),
} # type: Dict[str, Integration] } # type: Dict[str, Integration]
@@ -444,7 +438,7 @@ BOT_INTEGRATIONS = [
BotIntegration('github_detail', ['version-control', 'bots'], BotIntegration('github_detail', ['version-control', 'bots'],
display_name='GitHub Detail'), display_name='GitHub Detail'),
BotIntegration('xkcd', ['bots', 'misc'], display_name='xkcd', BotIntegration('xkcd', ['bots', 'misc'], display_name='xkcd',
logo='static/images/integrations/logos/xkcd.png'), logo='images/integrations/logos/xkcd.png'),
] # type: List[BotIntegration] ] # type: List[BotIntegration]
HUBOT_INTEGRATIONS = [ HUBOT_INTEGRATIONS = [
@@ -458,14 +452,14 @@ HUBOT_INTEGRATIONS = [
logo_alt='Google Hangouts logo'), logo_alt='Google Hangouts logo'),
HubotIntegration('instagram', ['misc'], display_name='Instagram', HubotIntegration('instagram', ['misc'], display_name='Instagram',
# _ needed to get around adblock plus # _ needed to get around adblock plus
logo='static/images/integrations/logos/instagra_m.svg'), logo='images/integrations/logos/instagra_m.svg'),
HubotIntegration('mailchimp', ['communication', 'marketing'], HubotIntegration('mailchimp', ['communication', 'marketing'],
display_name='MailChimp'), display_name='MailChimp'),
HubotIntegration('google-translate', ['misc'], HubotIntegration('google-translate', ['misc'],
display_name="Google Translate", logo_alt='Google Translate logo'), display_name="Google Translate", logo_alt='Google Translate logo'),
HubotIntegration('youtube', ['misc'], display_name='YouTube', HubotIntegration('youtube', ['misc'], display_name='YouTube',
# _ needed to get around adblock plus # _ needed to get around adblock plus
logo='static/images/integrations/logos/youtub_e.svg'), logo='images/integrations/logos/youtub_e.svg'),
] # type: List[HubotIntegration] ] # type: List[HubotIntegration]
for hubot_integration in HUBOT_INTEGRATIONS: for hubot_integration in HUBOT_INTEGRATIONS:

View File

@@ -283,7 +283,7 @@ class HelpTest(ZulipTestCase):
class IntegrationTest(TestCase): class IntegrationTest(TestCase):
def test_check_if_every_integration_has_logo_that_exists(self) -> None: def test_check_if_every_integration_has_logo_that_exists(self) -> None:
for integration in INTEGRATIONS.values(): for integration in INTEGRATIONS.values():
self.assertTrue(os.path.isfile(os.path.join(DEPLOY_ROOT, integration.logo))) self.assertTrue(os.path.isfile(DEPLOY_ROOT + integration.logo_url), integration.name)
def test_api_url_view_subdomains_base(self) -> None: def test_api_url_view_subdomains_base(self) -> None:
context = dict() # type: Dict[str, Any] context = dict() # type: Dict[str, Any]