lib.itmens/mastodon/management/commands/wrong_sites.py

24 lines
850 B
Python
Raw Normal View History

2022-01-06 04:22:12 +00:00
from django.core.management.base import BaseCommand
from mastodon.models import MastodonApplication
from django.conf import settings
2022-04-01 03:34:34 -04:00
from mastodon.api import get_instance_info
2022-01-06 04:22:12 +00:00
from users.models import User
class Command(BaseCommand):
2023-01-11 19:11:31 -05:00
help = "Find wrong sites"
2022-01-06 04:22:12 +00:00
def handle(self, *args, **options):
for site in MastodonApplication.objects.all():
d = site.domain_name
2023-01-11 19:11:31 -05:00
login_domain = (
d.strip().lower().split("//")[-1].split("/")[0].split("@")[-1]
)
2022-04-01 03:34:34 -04:00
domain, version = get_instance_info(login_domain)
2022-01-06 04:22:12 +00:00
if d != domain:
2023-01-11 19:11:31 -05:00
print(f"{d} should be {domain}")
2022-01-06 04:22:12 +00:00
for u in User.objects.filter(mastodon_site=d, is_active=True):
u.mastodon_site = domain
2023-01-11 19:11:31 -05:00
print(f"fixing {u}")
2022-01-06 04:22:12 +00:00
u.save()