add command to clean social accounts from unnecessary data
[wolnelektury.git] / src / wolnelektury / management / commands / clean_social_accounts.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from allauth.socialaccount.models import SocialAccount
6 from django.core.management.base import BaseCommand
7
8
9 KEPT_FIELDS = {
10     'facebook': ['link', 'name', 'id', 'locale', 'timezone', 'updated_time', 'verified'],
11     'google': ['name', 'picture', 'locale', 'id', 'verified_email', 'link'],
12 }
13
14
15 class Command(BaseCommand):
16     def handle(self, *args, **options):
17         for provider, kept_fields in KEPT_FIELDS.iteritems():
18             for sa in SocialAccount.objects.filter(provider=provider):
19                 trimmed_data = {k: v for k, v in sa.extra_data.iteritems() if k in kept_fields}
20                 sa.extra_data = trimmed_data
21                 sa.save()