Skip to content

Commit

Permalink
Ajout d'une commande pour la migration de Gravatar à jdenticon
Browse files Browse the repository at this point in the history
  • Loading branch information
Situphen committed Oct 2, 2024
1 parent 60901dd commit fb46769
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions zds/utils/management/commands/migrate_from_gravatar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from hashlib import md5
from time import sleep

import requests
from django.core.management.base import BaseCommand

from zds.member.models import Profile


class Command(BaseCommand):
help = "Migrate from Gravatar"

def handle(self, *args, **options):
total = Profile.objects.filter(avatar_url__isnull=True).count()
i = 1
for profile in Profile.objects.select_related("user").filter(avatar_url__isnull=True).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!"))

0 comments on commit fb46769

Please sign in to comment.