Skip to content

Commit

Permalink
Merge pull request #3173 from artragis/feature_url
Browse files Browse the repository at this point in the history
Force la cohérence des protocoles pour le chargement des ressources
  • Loading branch information
pierre-24 committed Nov 9, 2015
2 parents f097b9a + ff00a9f commit 307ab31
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
6 changes: 3 additions & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% load captureas %}
{% load thumbnail %}
{% load i18n %}

{% load remove_url_protocole %}


<!DOCTYPE html>
Expand Down Expand Up @@ -296,7 +296,7 @@
{% endif %}
<a href="{{ topic.last_read_post.get_absolute_url }}">
{% with p=last_answer.author.profile %}
<img src="{{ p.get_avatar_url }}" alt="" class="avatar">
<img src="{{ p.get_avatar_url|remove_url_protocole }}" alt="" class="avatar">
{% endwith %}
<span class="username">{{ last_answer.author.username }}</span>
<span class="date">{{ last_answer.pubdate|format_date:True|capfirst }}</span>
Expand Down Expand Up @@ -339,7 +339,7 @@
<li>
<a href="{{ first_unread.url }}">
{% with p=first_unread.author.profile %}
<img src="{{ p.get_avatar_url }}" alt="" class="avatar">
<img src="{{ p.get_avatar_url|remove_url_protocole }}" alt="" class="avatar">
{% endwith %}
<span class="username">{{ first_unread.author.username }}</span>
<span class="date">{{ first_unread.pubdate|format_date:True|capfirst }}</span>
Expand Down
3 changes: 2 additions & 1 deletion templates/featured/includes/featured_resource_item.part.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% load i18n %}
{% load remove_url_protocole %}

<article class="featured-resource-item">
<a href="{{ featured_resource.url }}">
<img src="{{ featured_resource.image_url }}" alt="" class="featured-resource-illu">
<img src="{{ featured_resource.image_url|remove_url_protocole }}" alt="" class="featured-resource-illu">
<div class="featured-resource-meta">
<h3>{{ featured_resource.title }}</h3>
<p class="featured-resource-description">{{ featured_resource.type }} {% if featured_resource.authors %} {% trans "par" %} <i>{{ featured_resource.authors }}</i>{% endif %}</p>
Expand Down
4 changes: 2 additions & 2 deletions templates/featured/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "featured/base.html" %}
{% load i18n %}

{% load remove_url_protocole %}

{% block title %}
{% trans "Liste des unes" %}
Expand Down Expand Up @@ -41,7 +41,7 @@
</div>
<div class="topic-description">
<a href="{% url 'featured-resource-update' featured_resource.pk %}" class="topic-title-link navigable-link">
<img src="{{ featured_resource.image_url }}"
<img src="{{ featured_resource.image_url|remove_url_protocole }}"
data-caption="{{ featured_resource.title }}"
alt="{{ featured_resource.title }}"
class="topic-image"
Expand Down
15 changes: 15 additions & 0 deletions zds/utils/templatetags/remove_url_protocole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django import template
from zds.settings import ZDS_APP


register = template.Library()


@register.filter('remove_url_protocole')
def remove_url_protocole(input_url):
"""
make every image url pointing to this website protocol independant so that https is not broken
"""
if ZDS_APP["site"]["dns"] in input_url:
return input_url.replace("http:/", "https:/").replace("https://" + ZDS_APP["site"]["dns"], "")
return input_url
16 changes: 16 additions & 0 deletions zds/utils/templatetags/tests/test_remove_url_protocole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from zds.settings import ZDS_APP
from django.test import TestCase
from zds.utils.templatetags.remove_url_protocole import remove_url_protocole


class RemoveUrlProtocolTest(TestCase):

def test_remove_protocole_when_local_url(self):
self.assertEqual("/bla.html", remove_url_protocole("http://" + ZDS_APP["site"]["dns"] + "/bla.html"))
self.assertEqual("/bla.html", remove_url_protocole("https://" + ZDS_APP["site"]["dns"] + "/bla.html"))

def test_no_change_when_no_protocole(self):
self.assertEqual("/bla.html", remove_url_protocole("/bla.html"))

def test_no_change_when_extern_address(self):
self.assertEqual("http://www.google.com/bla.html", remove_url_protocole("http://www.google.com/bla.html"))

0 comments on commit 307ab31

Please sign in to comment.