From 0833d4057f841b1573d88b51025726f9487cb14e Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Wed, 16 Oct 2019 14:11:07 +0300 Subject: [PATCH 1/5] Change images search fields sources --- apps/search_indexes/documents/news.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/search_indexes/documents/news.py b/apps/search_indexes/documents/news.py index 86b117e5..df3b8b48 100644 --- a/apps/search_indexes/documents/news.py +++ b/apps/search_indexes/documents/news.py @@ -24,8 +24,8 @@ class NewsDocument(Document): country = fields.ObjectField(properties={'id': fields.IntegerField(), 'code': fields.KeywordField()}) web_url = fields.KeywordField(attr='web_url') - image_url = fields.KeywordField(attr='image_url') - preview_image_url = fields.KeywordField(attr='preview_image_url') + image_url = fields.KeywordField(attr='image.original_url') + preview_image_url = fields.KeywordField(attr='image.auto_crop_images.preview_url') tags = fields.ObjectField( properties={ 'id': fields.IntegerField(attr='id'), From 3ace542deb437d815b5b28e3f54a5964f63a07de Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 12 Nov 2019 18:12:14 +0300 Subject: [PATCH 2/5] comment created date --- apps/comment/transfer_data.py | 1 + apps/transfer/serializers/comments.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/apps/comment/transfer_data.py b/apps/comment/transfer_data.py index c9d07585..1755b2e3 100644 --- a/apps/comment/transfer_data.py +++ b/apps/comment/transfer_data.py @@ -20,6 +20,7 @@ def transfer_comments(): 'mark', 'establishment_id', 'account_id', + 'created_at', ) serialized_data = CommentSerializer(data=list(queryset.values()), many=True) diff --git a/apps/transfer/serializers/comments.py b/apps/transfer/serializers/comments.py index 73c90802..f15c1930 100644 --- a/apps/transfer/serializers/comments.py +++ b/apps/transfer/serializers/comments.py @@ -10,6 +10,7 @@ class CommentSerializer(serializers.Serializer): mark = serializers.DecimalField(max_digits=4, decimal_places=2, allow_null=True) account_id = serializers.IntegerField() establishment_id = serializers.CharField() + created_at = serializers.DateTimeField(format='%m-%d-%Y %H:%M:%S') def validate(self, data): data.update({ @@ -18,6 +19,7 @@ class CommentSerializer(serializers.Serializer): 'mark': self.get_mark(data), 'content_object': self.get_content_object(data), 'user': self.get_account(data), + 'created': data.pop('created_at'), }) data.pop('establishment_id') data.pop('account_id') From fef5008d3501e07aa0a41549dd4f729ca573cb0d Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Tue, 12 Nov 2019 18:13:34 +0300 Subject: [PATCH 3/5] Revert "Change images search fields sources" This reverts commit 0833d40 --- apps/search_indexes/documents/news.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/search_indexes/documents/news.py b/apps/search_indexes/documents/news.py index df3b8b48..86b117e5 100644 --- a/apps/search_indexes/documents/news.py +++ b/apps/search_indexes/documents/news.py @@ -24,8 +24,8 @@ class NewsDocument(Document): country = fields.ObjectField(properties={'id': fields.IntegerField(), 'code': fields.KeywordField()}) web_url = fields.KeywordField(attr='web_url') - image_url = fields.KeywordField(attr='image.original_url') - preview_image_url = fields.KeywordField(attr='image.auto_crop_images.preview_url') + image_url = fields.KeywordField(attr='image_url') + preview_image_url = fields.KeywordField(attr='preview_image_url') tags = fields.ObjectField( properties={ 'id': fields.IntegerField(attr='id'), From 0dfa1c85d5bfbc582c2c477e46529420e867c7a2 Mon Sep 17 00:00:00 2001 From: evgeniy-st Date: Tue, 12 Nov 2019 18:15:53 +0300 Subject: [PATCH 4/5] Recalculate toque number fallback --- .../recalculate_toque_number_fallback.py | 54 +++++++++++++++++++ apps/establishment/models.py | 23 ++++++++ 2 files changed, 77 insertions(+) create mode 100644 apps/establishment/management/commands/recalculate_toque_number_fallback.py diff --git a/apps/establishment/management/commands/recalculate_toque_number_fallback.py b/apps/establishment/management/commands/recalculate_toque_number_fallback.py new file mode 100644 index 00000000..82b6d437 --- /dev/null +++ b/apps/establishment/management/commands/recalculate_toque_number_fallback.py @@ -0,0 +1,54 @@ +"""Run recalculating toque number for establishments without indexing.""" +from django.core.management.base import BaseCommand +from establishment.models import Establishment, RatingStrategy +from location.models import Country + + +class Command(BaseCommand): + + help = 'Recalculation toque number for all establishments without indexing.' + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def handle(self, *args, **options): + restaurants = Establishment.objects.restaurants() + + # update establishments with a specific rating strategy + strategies = RatingStrategy.objects.with_country() + for strategy in strategies: + qs = restaurants.by_country(strategy.country). \ + by_public_mark_range(strategy.public_mark_min_value, + strategy.public_mark_max_value) + qs.update(toque_number=strategy.toque_number) + + countries = Country.objects.filter(pk__in=strategies.values('country')) + + for country in countries: + rating_strategies = RatingStrategy.objects.by_country(country) + qs = Establishment.objects.restaurants(). \ + by_country(country). \ + exclude_public_mark_ranges( + ranges=[(strategy.public_mark_min_value, + strategy.public_mark_max_value) for + strategy in rating_strategies]) + qs.update(toque_number=0) + + # update establishments for other countries + strategy_for_other_countries = RatingStrategy.objects.with_country(False) + for strategy in strategy_for_other_countries: + qs = Establishment.objects.restaurants(). \ + exclude_countries(countries). \ + by_public_mark_range(strategy.public_mark_min_value, + strategy.public_mark_max_value) + qs.update(toque_number=strategy.toque_number) + + # set null for others + qs = Establishment.objects.restaurants(). \ + exclude_countries(countries). \ + exclude_public_mark_ranges( + ranges=[(strategy.public_mark_min_value, + strategy.public_mark_max_value) + for strategy in + strategy_for_other_countries]) + qs.update(toque_number=0) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 497ab300..092fa502 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -1,6 +1,7 @@ """Establishment models.""" from datetime import datetime from functools import reduce +from operator import or_ import elasticsearch_dsl from django.conf import settings @@ -159,6 +160,10 @@ class EstablishmentQuerySet(models.QuerySet): ids = [result.meta.id for result in search] return self.filter(id__in=ids) + def by_country(self, country): + """Return establishments by country code""" + return self.filter(address__city__country=country) + def by_country_code(self, code): """Return establishments by country code""" return self.filter(address__city__country__code=code) @@ -308,6 +313,20 @@ class EstablishmentQuerySet(models.QuerySet): """Return QuerySet with subtype by value.""" return self.filter(establishment_subtypes__index_name=value) + def by_public_mark_range(self, min_value, max_value): + """Filter by public mark range.""" + return self.filter(public_mark__gte=min_value, public_mark__lte=max_value) + + def exclude_public_mark_ranges(self, ranges): + """Exclude public mark ranges.""" + return self.exclude(reduce(or_, [Q(public_mark__gte=r[0], + public_mark__lte=r[1]) + for r in ranges])) + + def exclude_countries(self, countries): + """Exclude countries.""" + return self.exclude(address__city__country__in=countries) + class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin): """Establishment model.""" @@ -770,6 +789,10 @@ class RatingStrategyQuerySet(models.QuerySet): """Filter by country.""" return self.filter(country=country) + def with_country(self, switcher=True): + """With country.""" + return self.exclude(country__isnull=switcher) + def for_public_mark(self, public_mark): """Filter for value.""" return self.filter(public_mark_min_value__lte=public_mark, From 3a5c34fd4f8abaa5b449780610d4ff7ea0f649be Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 12 Nov 2019 18:29:24 +0300 Subject: [PATCH 5/5] comment command --- apps/comment/management/__init__.py | 0 apps/comment/management/commands/__init__.py | 0 .../commands/add_comment_publish_data.py | 29 +++++++++++++++++++ .../migrations/0006_comment_is_publish.py | 18 ++++++++++++ apps/comment/models.py | 1 + apps/comment/transfer_data.py | 1 - .../tag/migrations/0011_auto_20191112_1525.py | 18 ++++++++++++ apps/transfer/serializers/comments.py | 1 - 8 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 apps/comment/management/__init__.py create mode 100644 apps/comment/management/commands/__init__.py create mode 100644 apps/comment/management/commands/add_comment_publish_data.py create mode 100644 apps/comment/migrations/0006_comment_is_publish.py create mode 100644 apps/tag/migrations/0011_auto_20191112_1525.py diff --git a/apps/comment/management/__init__.py b/apps/comment/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/comment/management/commands/__init__.py b/apps/comment/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/comment/management/commands/add_comment_publish_data.py b/apps/comment/management/commands/add_comment_publish_data.py new file mode 100644 index 00000000..49f0af35 --- /dev/null +++ b/apps/comment/management/commands/add_comment_publish_data.py @@ -0,0 +1,29 @@ +from django.core.management.base import BaseCommand + +from account.models import User +from establishment.models import Establishment +from transfer.models import Comments +from comment.models import Comment as NewComment + + +class Command(BaseCommand): + help = 'Add publish values from old db to new db' + + def handle(self, *args, **kwargs): + count = 0 + + 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), + ) + for comment in queryset: + obj = NewComment.objects.filter(old_id=comment.id).first() + if obj: + count += 1 + obj.created = comment.created_at + obj.modified = comment.updated_at + obj.is_publish = comment.state == 'published' + obj.save() + self.stdout.write(self.style.WARNING(f'Updated {count} objects.')) diff --git a/apps/comment/migrations/0006_comment_is_publish.py b/apps/comment/migrations/0006_comment_is_publish.py new file mode 100644 index 00000000..3c886d32 --- /dev/null +++ b/apps/comment/migrations/0006_comment_is_publish.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-11-12 15:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('comment', '0005_remove_comment_country'), + ] + + operations = [ + migrations.AddField( + model_name='comment', + name='is_publish', + field=models.BooleanField(default=False, verbose_name='Publish status'), + ), + ] diff --git a/apps/comment/models.py b/apps/comment/models.py index 9bf93697..421a05fb 100644 --- a/apps/comment/models.py +++ b/apps/comment/models.py @@ -34,6 +34,7 @@ class Comment(ProjectBaseMixin): mark = models.PositiveIntegerField(blank=True, null=True, default=None, verbose_name=_('Mark')) user = models.ForeignKey('account.User', related_name='comments', on_delete=models.CASCADE, verbose_name=_('User')) old_id = models.IntegerField(null=True, blank=True, default=None) + is_publish = models.BooleanField(default=False, verbose_name=_('Publish status')) content_type = models.ForeignKey(generic.ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() diff --git a/apps/comment/transfer_data.py b/apps/comment/transfer_data.py index 1755b2e3..c9d07585 100644 --- a/apps/comment/transfer_data.py +++ b/apps/comment/transfer_data.py @@ -20,7 +20,6 @@ def transfer_comments(): 'mark', 'establishment_id', 'account_id', - 'created_at', ) serialized_data = CommentSerializer(data=list(queryset.values()), many=True) diff --git a/apps/tag/migrations/0011_auto_20191112_1525.py b/apps/tag/migrations/0011_auto_20191112_1525.py new file mode 100644 index 00000000..40a36164 --- /dev/null +++ b/apps/tag/migrations/0011_auto_20191112_1525.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-11-12 15:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tag', '0010_auto_20191112_0104'), + ] + + operations = [ + migrations.AlterField( + model_name='tagcategory', + name='value_type', + field=models.CharField(choices=[('string', 'string'), ('list', 'list'), ('integer', 'integer'), ('percentage', 'percentage')], default='list', max_length=255, verbose_name='value type'), + ), + ] diff --git a/apps/transfer/serializers/comments.py b/apps/transfer/serializers/comments.py index f15c1930..3b099239 100644 --- a/apps/transfer/serializers/comments.py +++ b/apps/transfer/serializers/comments.py @@ -19,7 +19,6 @@ class CommentSerializer(serializers.Serializer): 'mark': self.get_mark(data), 'content_object': self.get_content_object(data), 'user': self.get_account(data), - 'created': data.pop('created_at'), }) data.pop('establishment_id') data.pop('account_id')