fix filter comment by establishment and user

This commit is contained in:
alex 2019-11-12 16:38:06 +03:00
parent 0db7972b5a
commit 3f2de46420
2 changed files with 15 additions and 13 deletions

View File

@ -1,8 +1,7 @@
from pprint import pprint
from django.db.models import Q
from account.transfer_data import STOP_LIST
from account.models import User
from establishment.models import Establishment
from transfer.models import Comments
from transfer.serializers.comments import CommentSerializer
@ -10,14 +9,11 @@ from transfer.serializers.comments import CommentSerializer
def transfer_comments():
# В queryset исключены объекты по условию в связанные моделях
# см. 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
establishments = Establishment.objects.all().values_list('old_id', flat=True)
users = User.objects.all().values_list('old_id', flat=True)
queryset = Comments.objects.filter(
establishment_id__in=list(establishments),
account_id__in=list(users),
).only(
'id',
'comment',

View File

@ -7,7 +7,7 @@ from establishment.models import Establishment
class CommentSerializer(serializers.Serializer):
id = serializers.IntegerField()
comment = serializers.CharField()
mark = serializers.DecimalField(max_digits=4, decimal_places=2)
mark = serializers.DecimalField(max_digits=4, decimal_places=2, allow_null=True)
account_id = serializers.IntegerField()
establishment_id = serializers.CharField()
@ -15,7 +15,7 @@ class CommentSerializer(serializers.Serializer):
data.update({
'old_id': data.pop('id'),
'text': data.pop('comment'),
'mark': data['mark'] * -1 if data['mark'] < 0 else data['mark'],
'mark': self.get_mark(data),
'content_object': self.get_content_object(data),
'user': self.get_account(data),
})
@ -42,3 +42,9 @@ class CommentSerializer(serializers.Serializer):
if not user:
raise ValueError(f"User account not found with old_id {data['account_id']}")
return user
@staticmethod
def get_mark(data):
if not data['mark']:
return None
return data['mark'] * -1 if data['mark'] < 0 else data['mark']