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

View File

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