refactor comment migration

This commit is contained in:
alex 2019-11-06 14:24:14 +03:00
parent c17f9ce820
commit a372d29677
6 changed files with 70 additions and 80 deletions

View File

@ -3,17 +3,21 @@ from pprint import pprint
from transfer.models import Profiles, Accounts from transfer.models import Profiles, Accounts
from transfer.serializers.account import UserSerializer from transfer.serializers.account import UserSerializer
STOP_LIST = (
'cyril@tomatic.net',
'cyril2@tomatic.net',
'd.sadykova@id-east.ru',
'd.sadykova@octopod.ru',
'n.yurchenko@id-east.ru',
)
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"))
stop_list = ['cyril@tomatic.net',
'cyril2@tomatic.net', queryset = Accounts.objects.filter(confirmed_at__isnull=False).exclude(email__in=STOP_LIST)
'd.sadykova@id-east.ru',
'd.sadykova@octopod.ru',
'n.yurchenko@id-east.ru']
queryset = Accounts.objects.filter(confirmed_at__isnull=False).exclude(email__in=stop_list)
serialized_data = UserSerializer(data=list(queryset.values()), many=True) serialized_data = UserSerializer(data=list(queryset.values()), many=True)

View File

@ -6,3 +6,4 @@ from . import models
@admin.register(models.Comment) @admin.register(models.Comment)
class CommentModelAdmin(admin.ModelAdmin): class CommentModelAdmin(admin.ModelAdmin):
"""Model admin for model Comment""" """Model admin for model Comment"""
raw_id_fields = ('user', 'country')

View File

@ -32,21 +32,16 @@ class CommentQuerySet(ContentTypeQuerySetMixin):
class Comment(ProjectBaseMixin): class Comment(ProjectBaseMixin):
"""Comment model.""" """Comment model."""
text = models.TextField(verbose_name=_('Comment text')) text = models.TextField(verbose_name=_('Comment text'))
mark = models.PositiveIntegerField(blank=True, null=True, default=None, mark = models.PositiveIntegerField(blank=True, null=True, default=None, verbose_name=_('Mark'))
verbose_name=_('Mark')) user = models.ForeignKey('account.User', related_name='comments', on_delete=models.CASCADE, verbose_name=_('User'))
user = models.ForeignKey('account.User', country = models.ForeignKey(Country, verbose_name=_('Country'), on_delete=models.SET_NULL, null=True)
related_name='comments', old_id = models.IntegerField(null=True, blank=True, default=None)
on_delete=models.CASCADE,
verbose_name=_('User'))
content_type = models.ForeignKey(generic.ContentType, on_delete=models.CASCADE) content_type = models.ForeignKey(generic.ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField() object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id') content_object = generic.GenericForeignKey('content_type', 'object_id')
objects = CommentQuerySet.as_manager() objects = CommentQuerySet.as_manager()
country = models.ForeignKey(Country, verbose_name=_('Country'),
on_delete=models.SET_NULL, null=True)
old_id = models.IntegerField(null=True, blank=True, default=None)
class Meta: class Meta:
"""Meta class""" """Meta class"""

View File

@ -1,11 +1,31 @@
from pprint import pprint from pprint import pprint
from django.db.models import Q
from account.transfer_data import STOP_LIST
from transfer.models import Comments from transfer.models import Comments
from transfer.serializers.comments import CommentSerializer from transfer.serializers.comments import CommentSerializer
def transfer_comments(): def transfer_comments():
queryset = Comments.objects.filter(account__isnull=False, mark__isnull=False)\ # В queryset исключены объекты по условию в связанные моделях
.only("id", "comment", "mark", "locale", "establishment_id", "account_id") # см. transfer_establishment() и transfer_user()
queryset = Comments.objects.exclude(
Q(establishment__type='Wineyard') |
Q(establishment__location__timezone__isnull=True) |
Q(account__confirmed_at__isnull=True) |
Q(account__email__in=STOP_LIST)
).filter(
account__isnull=False,
mark__isnull=False
).only(
'id',
'comment',
'mark',
'locale',
'establishment_id',
'account_id',
)
serialized_data = CommentSerializer(data=list(queryset.values()), many=True) serialized_data = CommentSerializer(data=list(queryset.values()), many=True)
if serialized_data.is_valid(): if serialized_data.is_valid():
@ -15,7 +35,7 @@ def transfer_comments():
data_types = { data_types = {
"comment": [ 'comment': [
transfer_comments transfer_comments
] ]
} }

View File

@ -1,6 +1,5 @@
from pprint import pprint from pprint import pprint
import requests
from django.db.models import Q, F from django.db.models import Q, F
from establishment.models import Establishment from establishment.models import Establishment

View File

@ -4,7 +4,7 @@ from establishment.models import Establishment
from location.models import Country from location.models import Country
class CommentSerializer(serializers.ModelSerializer): class CommentSerializer(serializers.Serializer):
id = serializers.IntegerField() id = serializers.IntegerField()
comment = serializers.CharField() comment = serializers.CharField()
mark = serializers.DecimalField(max_digits=4, decimal_places=2) mark = serializers.DecimalField(max_digits=4, decimal_places=2)
@ -12,25 +12,18 @@ class CommentSerializer(serializers.ModelSerializer):
account_id = serializers.IntegerField() account_id = serializers.IntegerField()
establishment_id = serializers.CharField() establishment_id = serializers.CharField()
class Meta:
model = Comment
fields = (
"id",
"comment",
"mark",
"locale",
"account_id",
"establishment_id"
)
def validate(self, data): def validate(self, data):
data = self.set_old_id(data) data.update({
data = self.set_text(data) 'old_id': data.pop('id'),
data = self.set_mark(data) 'text': data.pop('comment'),
data = self.set_establishment(data) 'mark': data['mark'] * -1 if data['mark'] < 0 else data['mark'],
data = self.set_account(data) 'content_object': self.get_content_object(data),
data = self.set_country(data) 'user': self.get_account(data),
'country': self.get_country(data),
})
data.pop('establishment_id')
data.pop('account_id')
data.pop('locale')
return data return data
def create(self, validated_data): def create(self, validated_data):
@ -39,47 +32,25 @@ class CommentSerializer(serializers.ModelSerializer):
except Exception as e: except Exception as e:
raise ValueError(f"Error creating comment with {validated_data}: {e}") raise ValueError(f"Error creating comment with {validated_data}: {e}")
def set_text(self, data): @staticmethod
data['text'] = data.pop('comment') def get_content_object(data):
return data establishment = Establishment.objects.filter(old_id=data['establishment_id']).first()
if not establishment:
raise ValueError(f"Establishment not found with old_id {data['establishment_id']}: ")
return establishment
def set_mark(self, data): @staticmethod
if data['mark'] < 0: def get_account(data):
data['mark'] = data['mark'] * -1 user = User.objects.filter(old_id=data['account_id']).first()
return data if not user:
raise ValueError(f"User account not found with old_id {data['account_id']}")
return user
def set_account(self, data): @staticmethod
try: def get_country(data):
data['user'] = User.objects.filter(old_id=data['account_id']).first() locale = data['locale']
except User.DoesNotExist as e:
raise ValueError(f"User account not found with {data}: {e}")
del(data['account_id'])
return data
def set_establishment(self, data):
try:
data['content_object'] = Establishment.objects.filter(old_id=data['establishment_id']).first()
except Establishment.DoesNotExist as e:
raise ValueError(f"Establishment not found with {data}: {e}")
# print(f"Establishment not found with {data}: {e}")
del(data['establishment_id'])
return data
def set_old_id(self, data):
data['old_id'] = data.pop("id")
return data
def set_country(self, data):
locale = data.pop("locale")
country_code = locale[:locale.index("-")] if len(locale) > 2 else locale country_code = locale[:locale.index("-")] if len(locale) > 2 else locale
country = Country.objects.filter(code=country_code).first()
try: if not country:
data['country'] = Country.objects.filter(code=country_code).first() raise ValueError(f"Country not found with code {country_code}")
except Country.DoesNotExist as e: return country
raise ValueError(f"Country not found with {data}: {e}")
return data