[damned-lies] Fixed #783594 - Allow Libravatar usage option as profile picture



commit cde193a5b77cdcb78d4b4b11d3a5bff3748667fb
Author: Claude Paroz <claude 2xlibre net>
Date:   Fri Jun 23 18:44:07 2017 +0200

    Fixed #783594 - Allow Libravatar usage option as profile picture
    
    Thanks tirifto posteo cz for the report.

 people/forms.py                                    |    2 +-
 people/migrations/0003_person_avatar_service.py    |   17 +++++++++++++++++
 people/migrations/0004_migrate_use_gravatar.py     |   16 ++++++++++++++++
 .../migrations/0005_remove_person_use_gravatar.py  |   16 ++++++++++++++++
 people/models.py                                   |   12 ++++++++++--
 people/templatetags/people.py                      |   16 +++++++++++-----
 people/tests.py                                    |   10 ++++++++--
 stats/fixtures/sample_data.json                    |    6 +++---
 8 files changed, 82 insertions(+), 13 deletions(-)
---
diff --git a/people/forms.py b/people/forms.py
index 56b7565..ff6f5db 100644
--- a/people/forms.py
+++ b/people/forms.py
@@ -91,7 +91,7 @@ class RegistrationForm(forms.Form):
 class DetailForm(forms.ModelForm):
     class Meta:
         model = Person
-        fields = ('first_name', 'last_name', 'email', 'image', 'use_gravatar',
+        fields = ('first_name', 'last_name', 'email', 'image', 'avatar_service',
                   'webpage_url', 'irc_nick', 'bugzilla_account')
 
     def clean_image(self):
diff --git a/people/migrations/0003_person_avatar_service.py b/people/migrations/0003_person_avatar_service.py
new file mode 100644
index 0000000..0196e94
--- /dev/null
+++ b/people/migrations/0003_person_avatar_service.py
@@ -0,0 +1,17 @@
+# Generated by Django 1.11.2 on 2017-06-23 18:27
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('people', '0002_set_use_gravatar_verbose_name'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='person',
+            name='avatar_service',
+            field=models.CharField(blank=True, choices=[('libravatar.org', 'libravatar.org'), 
('gravatar.com', 'gravatar.com')], max_length=50, verbose_name='Avatar provider'),
+        ),
+    ]
diff --git a/people/migrations/0004_migrate_use_gravatar.py b/people/migrations/0004_migrate_use_gravatar.py
new file mode 100644
index 0000000..2ee056d
--- /dev/null
+++ b/people/migrations/0004_migrate_use_gravatar.py
@@ -0,0 +1,16 @@
+from django.db import migrations
+
+
+def migrate_gravatar(apps, schema_editor):
+    Person = apps.get_model("people", "Person")
+    Person.objects.filter(use_gravatar=True).update(avatar_service='gravatar.com')
+
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('people', '0003_person_avatar_service'),
+    ]
+
+    operations = [migrations.RunPython(migrate_gravatar)]
diff --git a/people/migrations/0005_remove_person_use_gravatar.py 
b/people/migrations/0005_remove_person_use_gravatar.py
new file mode 100644
index 0000000..fac6785
--- /dev/null
+++ b/people/migrations/0005_remove_person_use_gravatar.py
@@ -0,0 +1,16 @@
+# Generated by Django 1.11.2 on 2017-06-23 18:32
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('people', '0004_migrate_use_gravatar'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='person',
+            name='use_gravatar',
+        ),
+    ]
diff --git a/people/models.py b/people/models.py
index 59f3c88..60fe033 100644
--- a/people/models.py
+++ b/people/models.py
@@ -8,6 +8,12 @@ from django.utils.safestring import mark_safe
 from django.utils.translation import ugettext_lazy as _
 from django.contrib.auth.models import User, UserManager
 
+AVATAR_SERVICES = {
+    'gravatar.com': 'https://secure.gravatar.com/avatar/{hash}.jpg?{qs}',
+    # See https://wiki.libravatar.org/api/
+    'libravatar.org': 'https://seccdn.libravatar.org/avatar/{hash}?{qs}',
+}
+
 
 def obfuscate_email(email):
     if email:
@@ -23,8 +29,10 @@ class Person(User):
     svn_account = models.SlugField(max_length=20, null=True, blank=True)
     image = models.URLField(_("Image"), null=True, blank=True,
                             help_text=_("URL to an image file (.jpg, .png, …) of an hackergotchi (max. 
100×100 pixels)"))
-    use_gravatar = models.BooleanField(_("Use gravatar"), default=False,
-        help_text=_("Display the image of your gravatar.com account"))
+    avatar_service = models.CharField(
+        _("Avatar provider"), max_length=50, blank=True,
+        choices=((name, name) for name in AVATAR_SERVICES.keys())
+    )
     webpage_url = models.URLField(_("Web page"), null=True, blank=True)
     irc_nick = models.SlugField(_("IRC nickname"), max_length=20, null=True, blank=True)
     bugzilla_account = models.EmailField(_("Bugzilla account"), null=True, blank=True,
diff --git a/people/templatetags/people.py b/people/templatetags/people.py
index 09ce262..9969666 100644
--- a/people/templatetags/people.py
+++ b/people/templatetags/people.py
@@ -6,6 +6,8 @@ from django.utils.html import format_html, format_html_join
 from django.utils.http import urlencode
 from django.utils.translation import ugettext as _
 
+from people.models import AVATAR_SERVICES
+
 register = template.Library()
 
 
@@ -24,13 +26,15 @@ def people_list(lst):
 @register.filter
 def people_image(person):
     nobody = settings.STATIC_URL + "img/nobody.png"
-    if person.use_gravatar:
+    if person.avatar_service:
         digest = hashlib.md5(person.email.lower().encode("utf-8")).hexdigest()
-        tag = format_html(
-            '<img src="https://secure.gravatar.com/avatar/{hash}.jpg?{qs}"; alt="gravatar icon">',
+        url = AVATAR_SERVICES[person.avatar_service].format(
             hash=digest,
             # Size, default image, rating
-            qs=urlencode([('s', '80'), ('d', 'identicon'), ('r', 'g')]),
+            qs=urlencode([('s', '80'), ('d', 'identicon'), ('r', 'g')])
+        )
+        tag = format_html(
+            '<img src="{url}" alt="{alt}">', url=url, alt=_("avatar icon")
         )
     elif person.image:
         tag = format_html(
@@ -38,5 +42,7 @@ def people_image(person):
             person.image, person.name, nobody
         )
     else:
-        tag = format_html('<img src="{}" alt="generic person icon">', nobody)
+        tag = format_html(
+            '<img src="{url}" alt="{alt}">', url=nobody, alt=_("generic person icon")
+        )
     return tag
diff --git a/people/tests.py b/people/tests.py
index 265e17a..a1338eb 100644
--- a/people/tests.py
+++ b/people/tests.py
@@ -137,11 +137,17 @@ class PeopleTestCase(TestCase):
             people.people_image(pn),
             '<img alt="John &lt;script&gt;Some XSS content&lt;/script&gt;" onerror="this.onerror = null; 
this.src=\'/static/img/nobody.png\'" src="http://www.example.org/my_image.png"; />'
         )
-        pn.use_gravatar = True
+        pn.avatar_service = 'gravatar.com'
         self.assertHTMLEqual(
             people.people_image(pn),
-            '<img alt="gravatar icon" 
src="https://secure.gravatar.com/avatar/618b8b6c1c973c780ec218242c49cbe7.jpg?s=80&d=identicon&r=g"; />'
+            '<img alt="avatar icon" 
src="https://secure.gravatar.com/avatar/618b8b6c1c973c780ec218242c49cbe7.jpg?s=80&d=identicon&r=g";>'
         )
+        pn.avatar_service = 'libravatar.org'
+        self.assertHTMLEqual(
+            people.people_image(pn),
+            '<img alt="avatar icon" 
src="https://seccdn.libravatar.org/avatar/618b8b6c1c973c780ec218242c49cbe7?s=80&d=identicon&r=g";>'
+        )
+
 
     def test_get_image_size(self):
         from people.forms import get_image_size
diff --git a/stats/fixtures/sample_data.json b/stats/fixtures/sample_data.json
index 413d09c..07d4dd2 100644
--- a/stats/fixtures/sample_data.json
+++ b/stats/fixtures/sample_data.json
@@ -63,7 +63,7 @@
   "svn_account": "bob1",
   "webpage_url": null,
   "user_permissions": [],
-  "use_gravatar": false
+  "avatar_service": ""
  },
  "model": "people.person",
  "pk": 2
@@ -78,7 +78,7 @@
   "svn_account": "coord_fr",
   "webpage_url": null,
   "user_permissions": [],
-  "use_gravatar": false
+  "avatar_service": ""
  },
  "model": "people.person",
  "pk": 3
@@ -93,7 +93,7 @@
   "svn_account": null,
   "webpage_url": null,
   "user_permissions": [],
-  "use_gravatar": false
+  "avatar_service": ""
  },
  "model": "people.person",
  "pk": 4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]