Prevent broken Personas from raising an exception

If the Persona does not exist, raise a 404 in the legacy site.
This commit is contained in:
Mathieu Pillard 2018-10-01 15:15:01 +02:00
Родитель 48f929549b
Коммит 0013d6d956
3 изменённых файлов: 17 добавлений и 10 удалений

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

@ -455,10 +455,9 @@ class AddonSerializer(serializers.ModelSerializer):
something on the Persona instance, explicitly or not, to avoid 500
errors and/or SQL queries in ESAddonSerializer."""
try:
# Sadly, https://code.djangoproject.com/ticket/14368 prevents us
# from setting obj.persona = None in ESAddonSerializer.fake_object
# below. This is fixed in Django 1.9, but in the meantime we work
# around it by creating a Persona instance with a custom '_broken'
# Setting obj.persona = None in ESAddonSerializer.fake_object()
# below sadly isn't enough, so we work around it in that method by
# creating a Persona instance with a custom '_broken'
# attribute indicating that it should not be used.
if obj.type == amo.ADDON_PERSONA and (
obj.persona is None or hasattr(obj.persona, '_broken')):
@ -646,11 +645,10 @@ class ESAddonSerializer(BaseESSerializer, AddonSerializer):
popularity=data.get('average_daily_users'),
)
else:
# Sadly, https://code.djangoproject.com/ticket/14368 prevents
# us from setting obj.persona = None. This is fixed in
# Django 1.9, but in the meantime, work around it by creating
# a Persona instance with a custom attribute indicating that
# it should not be used.
# Sadly, although we can set obj.persona = None, this does not
# seem to prevent the query later on. So instead, work around
# it by creating a Persona instance with a custom attribute
# indicating that it should not be used.
obj.persona = Persona()
obj.persona._broken = True

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

@ -378,6 +378,12 @@ class TestDetailPage(TestCase):
assert response.status_code == 200
assert response.context['addon'].id == 15663
def test_broken_persona(self):
persona = Persona.objects.get(addon_id=15663)
persona.delete()
response = self.client.get(reverse('addons.detail', args=['a15663']))
assert response.status_code == 404
def test_review_microdata_personas(self):
a = Addon.objects.get(id=15663)
a.name = '<script>alert("fff")</script>'

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

@ -153,7 +153,10 @@ def persona_detail(request, addon):
if not (addon.is_public() or addon.is_pending()):
raise http.Http404
persona = addon.persona
try:
persona = addon.persona
except Persona.DoesNotExist:
raise http.Http404
# This persona's categories.
categories = addon.categories.all()