From b775aa6d75a928f2b1fbce94e85f83dc4953d113 Mon Sep 17 00:00:00 2001 From: Situphen Date: Sun, 29 Sep 2024 01:08:40 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20d'une=20commande=20pour=20la=20migratio?= =?UTF-8?q?n=20de=20Gravatar=20=C3=A0=20jdenticon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commands/migrate_from_gravatar.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 zds/member/management/commands/migrate_from_gravatar.py diff --git a/zds/member/management/commands/migrate_from_gravatar.py b/zds/member/management/commands/migrate_from_gravatar.py new file mode 100644 index 0000000000..2c4890fa1d --- /dev/null +++ b/zds/member/management/commands/migrate_from_gravatar.py @@ -0,0 +1,29 @@ +from hashlib import md5 +from time import sleep + +import requests +from django.core.management.base import BaseCommand +from django.db.models import Q + +from zds.member.models import Profile + + +class Command(BaseCommand): + help = "Migrate from Gravatar" + + def handle(self, *args, **options): + # We have profiles with either NULL or empty avatar_url field + profiles_without_avatar_url = Profile.objects.filter(Q(avatar_url__isnull=True) | Q(avatar_url="")) + total = profiles_without_avatar_url.count() + i = 1 + for profile in profiles_without_avatar_url.iterator(): + hash = md5(profile.user.email.lower().encode("utf-8")).hexdigest() + gravatar_url = f"https://secure.gravatar.com/avatar/{hash}" + r = requests.get(f"{gravatar_url}?d=404") + if r.status_code == 200: + profile.avatar_url = f"{gravatar_url}?s=200" + profile.save() + self.stdout.write(f"\rProgress: {i}/{total}", ending="") + i += 1 + sleep(1) + self.stdout.write(self.style.SUCCESS("\nSuccessfully migrated from Gravatar!"))