Format heading field server-side in discovery API, returning HTML directly

This commit is contained in:
Mathieu Pillard 2016-05-26 13:47:33 +02:00
Родитель e78aeb65b1
Коммит 11bec15765
3 изменённых файлов: 42 добавлений и 15 удалений

Просмотреть файл

@ -19,6 +19,6 @@ Firefox (about:addons).
:>json int count: The number of results for this query.
:>json array results: The array containing the results for this query.
:>json string results[].heading: The heading for this item. May contain the following sub-strings, that clients need to use to format the string as they desire: ``{start_sub_heading}``, ``{end_sub_heading}`` and ``{addon_name}``.
:>json string results[].heading: The heading for this item. May contain some HTML tags.
:>json string|null results[].description: The description for this item, if any. May contain some HTML tags.
:>json object results[].addon: The :ref:`add-on <addon-detail-object>` for this item. Only a subset of fields are present: ``id``, ``current_version`` (with only the ``compatibility`` and ``files`` fields present), ``guid``, ``icon_url``, ``name``, ``slug``, ``theme_data``, ``type`` and ``url``.

Просмотреть файл

@ -29,4 +29,9 @@ class DiscoverySerializer(serializers.Serializer):
data = super(DiscoverySerializer, self).to_representation(instance)
if data['heading'] is None:
data['heading'] = unicode(instance.addon.name)
else:
data['heading'] = data['heading'].replace(
'{start_sub_heading}', '<span>').replace(
'{end_sub_heading}', '</span>').replace(
'{addon_name}', unicode(instance.addon.name))
return data

Просмотреть файл

@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
from olympia import amo
from olympia.discovery.data import discopane_items
from olympia.amo.urlresolvers import reverse
from olympia.amo.helpers import absolutify
from olympia.amo.tests import addon_factory, TestCase, user_factory
from olympia.amo.urlresolvers import reverse
class TestDiscoveryViewList(TestCase):
@ -13,18 +14,44 @@ class TestDiscoveryViewList(TestCase):
def test_reverse(self):
assert self.url == '/api/v3/discovery/'
def _check_disco_addon(self, result, item):
addon = self.addons[item.addon_id]
assert result['addon']['id'] == item.addon_id == addon.pk
assert result['addon']['name'] == unicode(addon.name)
assert result['addon']['slug'] == addon.slug
assert result['addon']['icon_url'] == absolutify(
addon.get_icon_url(64))
assert (result['addon']['current_version']['files'][0]['id'] ==
addon.current_version.all_files[0].pk)
assert unicode(addon.name) in result['heading']
assert '<span>' in result['heading']
assert '</span>' in result['heading']
assert result['description']
def _check_disco_theme(self, result, item):
addon = self.addons[item.addon_id]
assert result['addon']['id'] == item.addon_id == addon.pk
assert result['addon']['name'] == unicode(addon.name)
assert result['addon']['slug'] == addon.slug
assert result['heading'] == unicode(addon.name)
assert '<span>' not in result['heading']
assert '</span>' not in result['heading']
assert not result['description']
assert result['addon']['theme_data'] == addon.persona.theme_data
def test_list(self):
addons = {}
self.addons = {}
for item in discopane_items:
type_ = amo.ADDON_EXTENSION
if not item.heading and not item.description:
type_ = amo.ADDON_PERSONA
author = user_factory()
addons[item.addon_id] = addon_factory(id=item.addon_id, type=type_)
self.addons[item.addon_id] = addon_factory(
id=item.addon_id, type=type_)
if type_ == amo.ADDON_PERSONA:
addons[item.addon_id].addonuser_set.create(user=author)
self.addons[item.addon_id].addonuser_set.create(user=author)
response = self.client.get(self.url)
response = self.client.get(self.url, {'lang': 'en-US'})
assert response.data
assert response.data['count'] == len(discopane_items)
@ -32,16 +59,11 @@ class TestDiscoveryViewList(TestCase):
assert response.data['previous'] is None
assert response.data['results']
for i, item in enumerate(discopane_items):
result = response.data['results'][i]
assert result['addon']['id'] == item.addon_id
if item.heading:
assert result['heading'] == item.heading
for i, result in enumerate(response.data['results']):
if 'theme_data' in result['addon']:
self._check_disco_theme(result, discopane_items[i])
else:
assert result['heading'] == unicode(addons[item.addon_id].name)
assert result['description'] == item.description
assert result['addon']['current_version']
assert result['addon']['slug'] == addons[item.addon_id].slug
self._check_disco_addon(result, discopane_items[i])
def test_missing_addon(self):
addon_factory(id=discopane_items[0].addon_id, type=amo.ADDON_PERSONA)