migrate identities
This commit is contained in:
parent
a466168d61
commit
9b16f46215
|
|
@ -1,7 +1,10 @@
|
||||||
from django.db.models import Value, IntegerField, F
|
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from transfer.models import Profiles, Accounts
|
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
|
from transfer.models import Accounts, Identities
|
||||||
from transfer.serializers.account import UserSerializer
|
from transfer.serializers.account import UserSerializer
|
||||||
|
from transfer.serializers.user_social_auth import UserSocialAuthSerializer
|
||||||
|
|
||||||
STOP_LIST = (
|
STOP_LIST = (
|
||||||
'cyril@tomatic.net',
|
'cyril@tomatic.net',
|
||||||
|
|
@ -14,8 +17,8 @@ STOP_LIST = (
|
||||||
|
|
||||||
def transfer_user():
|
def transfer_user():
|
||||||
# queryset = Profiles.objects.all()
|
# queryset = Profiles.objects.all()
|
||||||
# queryset = queryset.annotate(nickname=F("account__nickname"))
|
# queryset = queryset.annotate(nickname=F('account__nickname'))
|
||||||
# queryset = queryset.annotate(email=F("account__email"))
|
# queryset = queryset.annotate(email=F('account__email'))
|
||||||
|
|
||||||
queryset = Accounts.objects.filter(confirmed_at__isnull=False).exclude(email__in=STOP_LIST)
|
queryset = Accounts.objects.filter(confirmed_at__isnull=False).exclude(email__in=STOP_LIST)
|
||||||
|
|
||||||
|
|
@ -24,9 +27,29 @@ def transfer_user():
|
||||||
if serialized_data.is_valid():
|
if serialized_data.is_valid():
|
||||||
serialized_data.save()
|
serialized_data.save()
|
||||||
else:
|
else:
|
||||||
pprint(f"News serializer errors: {serialized_data.errors}")
|
pprint(f'News serializer errors: {serialized_data.errors}')
|
||||||
|
|
||||||
|
|
||||||
|
def transfer_identities():
|
||||||
|
queryset = Identities.objects.exclude(
|
||||||
|
Q(account_id__isnull=True) |
|
||||||
|
Q(account__confirmed_at__isnull=True) |
|
||||||
|
Q(account__email__in=STOP_LIST)
|
||||||
|
).values_list(
|
||||||
|
'account_id',
|
||||||
|
'provider',
|
||||||
|
'uid',
|
||||||
|
)
|
||||||
|
|
||||||
|
serialized_data = UserSocialAuthSerializer(data=list(queryset.values()), many=True)
|
||||||
|
|
||||||
|
if serialized_data.is_valid():
|
||||||
|
serialized_data.save()
|
||||||
|
else:
|
||||||
|
pprint(f'UserSocialAuth serializer errors: {serialized_data.errors}')
|
||||||
|
|
||||||
|
|
||||||
data_types = {
|
data_types = {
|
||||||
"account": [transfer_user]
|
'account': [transfer_user],
|
||||||
|
'identities': [transfer_identities],
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ class Command(BaseCommand):
|
||||||
'menu',
|
'menu',
|
||||||
'location_establishment',
|
'location_establishment',
|
||||||
'whirligig',
|
'whirligig',
|
||||||
|
'identities',
|
||||||
]
|
]
|
||||||
|
|
||||||
LONG_DATA_TYPES = [
|
LONG_DATA_TYPES = [
|
||||||
|
|
|
||||||
|
|
@ -987,3 +987,17 @@ class MetadatumAliases(MigrateMixin):
|
||||||
class Meta:
|
class Meta:
|
||||||
managed = False
|
managed = False
|
||||||
db_table = 'metadatum_aliases'
|
db_table = 'metadatum_aliases'
|
||||||
|
|
||||||
|
|
||||||
|
class Identities(MigrateMixin):
|
||||||
|
using = 'legacy'
|
||||||
|
|
||||||
|
account = models.ForeignKey(Accounts, models.DO_NOTHING, blank=True, null=True)
|
||||||
|
provider = models.CharField(max_length=255, blank=True, null=True)
|
||||||
|
uid = models.CharField(max_length=255, blank=True, null=True)
|
||||||
|
created_at = models.DateTimeField()
|
||||||
|
updated_at = models.DateTimeField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
managed = False
|
||||||
|
db_table = 'identities'
|
||||||
|
|
|
||||||
30
apps/transfer/serializers/user_social_auth.py
Normal file
30
apps/transfer/serializers/user_social_auth.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
from rest_framework import serializers
|
||||||
|
from social_django.models import UserSocialAuth
|
||||||
|
|
||||||
|
from account.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class UserSocialAuthSerializer(serializers.Serializer):
|
||||||
|
account_id = serializers.IntegerField()
|
||||||
|
provider = serializers.CharField()
|
||||||
|
uid = serializers.CharField()
|
||||||
|
|
||||||
|
def validate(self, data):
|
||||||
|
data.update({
|
||||||
|
'user': self.get_account(data),
|
||||||
|
})
|
||||||
|
data.pop('account_id')
|
||||||
|
return data
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
try:
|
||||||
|
return UserSocialAuth.objects.create(**validated_data)
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f"Error creating UserSocialAuth with {validated_data}: {e}")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_account(data):
|
||||||
|
user = User.objects.filter(old_id=data['account_id']).first()
|
||||||
|
if not user:
|
||||||
|
raise ValueError(f"User account not found with old_id {data['account_id']}")
|
||||||
|
return user
|
||||||
Loading…
Reference in New Issue
Block a user