Merge branch 'develop' into feature/add_tag_product

This commit is contained in:
Виктор Гладких 2019-11-12 18:38:25 +03:00
commit c5ac47953c
9 changed files with 144 additions and 0 deletions

View File

View File

@ -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.'))

View File

@ -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'),
),
]

View File

@ -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()

View File

@ -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)

View File

@ -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,

View File

@ -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'),
),
]

View File

@ -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({