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 5, 2024
1 parent 60901dd commit b775aa6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions zds/member/management/commands/migrate_from_gravatar.py
Original file line number Diff line number Diff line change
@@ -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!"))

0 comments on commit b775aa6

Please sign in to comment.