recalculate price level preview version

This commit is contained in:
evgeniy-st 2019-08-27 16:53:05 +03:00
parent 6667047537
commit c04771ca58
4 changed files with 72 additions and 1 deletions

View File

@ -104,6 +104,16 @@ class Establishment(ProjectBaseMixin, ImageMixin):
def recalculate_toque_number(self):
self.toque_number = 4
def recalculate_price_level(self, low_price=None, high_price=None):
if low_price is None or high_price is None:
low_price, high_price = self.get_price_level()
# todo: calculate price level
self.price_level = 3
def get_price_level(self):
country = self.address.city.country
return country.low_price, country.high_price
@property
def subtypes(self):
return EstablishmentSubType.objects.filter(

View File

@ -0,0 +1,21 @@
"""Establishment app tasks."""
import logging
from celery import shared_task
from establishment import models
from location.models import Country
logger = logging.getLogger(__name__)
@shared_task
def recalculate_price_levels_by_country(country_id):
try:
country = Country.objects.get(pk=country_id)
except Country.DoesNotExist as ex:
logger.error(f'ESTABLISHMENT. Country does not exist. ID {country_id}')
else:
qs = models.Establishment.objects.filter(address__city__country=country)
for establishment in qs:
establishment.recalculate_price_level(low_price=country.low_price,
high_price=country.high_price)

View File

@ -0,0 +1,23 @@
# Generated by Django 2.2.4 on 2019-08-27 13:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('location', '0007_auto_20190826_1342'),
]
operations = [
migrations.AddField(
model_name='country',
name='high_price',
field=models.IntegerField(default=50, verbose_name='High price'),
),
migrations.AddField(
model_name='country',
name='low_price',
field=models.IntegerField(default=25, verbose_name='Low price'),
),
]

View File

@ -1,8 +1,12 @@
"""Location app models."""
from django.conf import settings
from django.db.models.signals import post_save
from django.db.transaction import on_commit
from django.dispatch import receiver
from django.contrib.gis.db import models
from django.contrib.postgres.fields import JSONField
from django.utils.translation import gettext_lazy as _
from establishment.tasks import recalculate_price_levels_by_country
from utils.models import ProjectBaseMixin, LocaleManagerMixin, SVGImageMixin
@ -16,6 +20,8 @@ class Country(SVGImageMixin, ProjectBaseMixin):
name = JSONField(null=True, blank=True, default=None,
verbose_name=_('Name'), help_text='{"en":"some text"}')
code = models.CharField(max_length=255, unique=True, verbose_name=_('Code'))
low_price = models.IntegerField(default=25, verbose_name=_('Low price'))
high_price = models.IntegerField(default=50, verbose_name=_('High price'))
objects = CountryManager()
@ -106,3 +112,14 @@ class Address(models.Model):
@property
def longitude(self):
return self.coordinates.x
# todo: Make recalculate price levels
@receiver(post_save, sender=Country)
def run_recalculate_price_levels(sender, instance, **kwargs):
if settings.USE_CELERY:
on_commit(lambda: recalculate_price_levels_by_country.delay(
country_id=instance.id))
else:
on_commit(lambda: recalculate_price_levels_by_country(
country_id=instance.id))