Merge branch 'develop' into feature/gallery

This commit is contained in:
Anatoly 2019-08-27 17:12:24 +03:00
commit 0baeeef302
31 changed files with 417 additions and 40 deletions

View File

@ -1,3 +1,4 @@
"""Establishment admin conf."""
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,8 @@
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class EstablishmentConfig(AppConfig):
name = 'establishment'
verbose_name = _('Establishment')

View File

@ -0,0 +1,69 @@
# Generated by Django 2.2.4 on 2019-08-27 11:22
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import easy_thumbnails.fields
import utils.methods
class Migration(migrations.Migration):
initial = True
dependencies = [
('location', '0007_auto_20190826_1342'),
]
operations = [
migrations.CreateModel(
name='EstablishmentType',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(default=django.utils.timezone.now, editable=False, verbose_name='Date created')),
('modified', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
('name', models.CharField(max_length=255, unique=True, verbose_name='Name')),
('use_subtypes', models.BooleanField(default=True, verbose_name='Use subtypes')),
],
options={
'verbose_name': 'Establishment type',
'verbose_name_plural': 'Establishment types',
},
),
migrations.CreateModel(
name='EstablishmentSubType',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(default=django.utils.timezone.now, editable=False, verbose_name='Date created')),
('modified', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
('name', models.CharField(max_length=255, unique=True, verbose_name='Name')),
('establishment_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='establishment.EstablishmentType', verbose_name='Type')),
],
options={
'verbose_name': 'Establishment subtype',
'verbose_name_plural': 'Establishment subtypes',
},
),
migrations.CreateModel(
name='Establishment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(default=django.utils.timezone.now, editable=False, verbose_name='Date created')),
('modified', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
('image', easy_thumbnails.fields.ThumbnailerImageField(blank=True, default=None, null=True, upload_to=utils.methods.image_path, verbose_name='Image')),
('name', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=None, help_text='{"en":"some text"}', null=True, verbose_name='Name')),
('description', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=None, help_text='{"en":"some text"}', null=True, verbose_name='Description')),
('public_mark', models.PositiveIntegerField(blank=True, default=None, null=True, verbose_name='Public mark')),
('toque_number', models.PositiveIntegerField(blank=True, default=None, null=True, verbose_name='Toque number')),
('price_level', models.PositiveIntegerField(blank=True, default=None, null=True, verbose_name='Price level')),
('address', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.PROTECT, to='location.Address', verbose_name='Address')),
('establishment_subtypes', models.ManyToManyField(related_name='subtype_establishment', to='establishment.EstablishmentSubType', verbose_name='Subtype')),
('establishment_type', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='establishment', to='establishment.EstablishmentType', verbose_name='Type')),
],
options={
'verbose_name': 'Establishment',
'verbose_name_plural': 'Establishments',
},
),
]

View File

@ -0,0 +1,132 @@
"""Establishment models."""
from django.contrib.postgres.fields import JSONField
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _
from location.models import Address
from utils.models import ProjectBaseMixin, ImageMixin, LocaleManagerMixin
# todo: establishment type&subtypes check
class EstablishmentType(ProjectBaseMixin):
"""Establishment type model."""
name = models.CharField(_('Name'), max_length=255, unique=True)
use_subtypes = models.BooleanField(_('Use subtypes'), default=True)
class Meta:
"""Meta class."""
verbose_name = _('Establishment type')
verbose_name_plural = _('Establishment types')
def __str__(self):
"""__str__ method."""
return self.name
class EstablishmentSubTypeManager(models.Manager):
"""Extended manager for establishment subtype."""
def make(self, name, establishment_type):
obj = self.model(name=name, establishment_type=establishment_type)
obj.full_clean()
obj.save()
return obj
class EstablishmentSubType(ProjectBaseMixin):
"""Establishment type model."""
name = models.CharField(_('Name'), max_length=255, unique=True)
establishment_type = models.ForeignKey(EstablishmentType,
on_delete=models.CASCADE,
verbose_name=_('Type'))
objects = EstablishmentSubTypeManager()
class Meta:
"""Meta class."""
verbose_name = _('Establishment subtype')
verbose_name_plural = _('Establishment subtypes')
def __str__(self):
"""__str__ method."""
return self.name
def clean_fields(self, exclude=None):
if not self.establishment_type.use_subtypes:
raise ValidationError(_('Establishment type is not use subtypes.'))
class EstablishmentManager(LocaleManagerMixin):
"""Extended manager for establishment model."""
class Establishment(ProjectBaseMixin, ImageMixin):
"""Establishment model."""
name = JSONField(blank=True, null=True, default=None,
verbose_name=_('Name'), help_text='{"en":"some text"}')
description = JSONField(blank=True, null=True, default=None,
verbose_name=_('Description'),
help_text='{"en":"some text"}')
public_mark = models.PositiveIntegerField(blank=True, null=True,
default=None,
verbose_name=_('Public mark'),)
toque_number = models.PositiveIntegerField(blank=True, null=True,
default=None,
verbose_name=_('Toque number'),)
establishment_type = models.ForeignKey(EstablishmentType,
related_name='establishment',
on_delete=models.PROTECT,
verbose_name=_('Type'))
establishment_subtypes = models.ManyToManyField(EstablishmentSubType,
related_name='subtype_establishment',
verbose_name=_('Subtype'))
address = models.ForeignKey(Address, blank=True, null=True, default=None,
on_delete=models.PROTECT,
verbose_name=_('Address'))
price_level = models.PositiveIntegerField(blank=True, null=True,
default=None,
verbose_name=_('Price level'))
objects = EstablishmentManager()
class Meta:
"""Meta class."""
verbose_name = _('Establishment')
verbose_name_plural = _('Establishments')
# todo: recalculate toque_number
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(
subtype_establishment=self,
establishment_type=self.establishment_type,
establishment_type__use_subtypes=True)
def set_establishment_type(self, establishment_type):
self.establishment_type = establishment_type
self.establishment_subtypes.exclude(
establishement_type=establishment_type).delete()
def add_establishment_subtype(self, establishment_subtype):
if establishment_subtype.establishment_type != self.establishment_type:
raise ValidationError('Establishment type of subtype does not match')
self.establishment_subtypes.add(establishment_subtype)

View File

@ -0,0 +1,50 @@
"""Establishment serializers."""
from rest_framework import serializers
from establishment import models
from location.serializers import AddressSerializer
class EstablishmentTypeSerializer(serializers.ModelSerializer):
"""Serializer for EstablishmentType model."""
class Meta:
"""Meta class."""
model = models.EstablishmentType
fields = ('id', 'name',)
class EstablishmentSubTypeSerializer(serializers.ModelSerializer):
"""Serializer for EstablishmentSubType models."""
class Meta:
"""Meta class."""
model = models.EstablishmentSubType
fields = ('id', 'name')
class EstablishmentSerializer(serializers.ModelSerializer):
"""Serializer for Establishment model."""
name_trans = serializers.CharField()
description_trans = serializers.CharField()
type = EstablishmentTypeSerializer(source='establishment_type')
subtypes = EstablishmentSubTypeSerializer(many=True)
address = AddressSerializer()
class Meta:
"""Meta class."""
model = models.Establishment
fields = (
'id',
'name_trans',
'description_trans',
'public_mark',
'price_level',
'type',
'subtypes',
'image',
'address',
)

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,11 @@
"""Establishment url patterns."""
from django.urls import include, path
from establishment import views
app_name = 'establishment'
urlpatterns = [
path('', views.EstablishmentListView.as_view(), name='establishment-list'),
]

View File

@ -0,0 +1,7 @@
"""Establishment app web urlconf."""
from establishment.urls.common import urlpatterns as common_urlpatterns
urlpatterns = []
urlpatterns.extend(common_urlpatterns)

View File

@ -0,0 +1,16 @@
"""Establishment app views."""
from rest_framework import generics, permissions
from establishment import models, serializers
from utils.views import JWTGenericViewMixin
class EstablishmentListView(JWTGenericViewMixin, generics.ListAPIView):
"""Resource for getting a list of establishments."""
pagination_class = None
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.EstablishmentSerializer
def get_queryset(self):
return models.Establishment.objects.annotate_localized_fields(
locale=self.request.locale)

View File

@ -1,5 +0,0 @@
from django.apps import AppConfig
class EstablishmentsConfig(AppConfig):
name = 'establishments'

View File

@ -1,14 +0,0 @@
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.utils.translation import gettext_lazy as _
from utils.models import ProjectBaseMixin, ImageMixin
class Establishment(ProjectBaseMixin):
"""Establishment model."""
name = models.CharField(max_length=100)
description = JSONField()
country = models.ForeignKey('location.Country', on_delete=models.CASCADE)

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()
@ -88,6 +94,8 @@ class Address(models.Model):
_('Coordinates'), blank=True, null=True, default=None)
class Meta:
"""Meta class."""
verbose_name_plural = _('Address')
verbose_name = _('Address')
@ -104,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))

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.4 on 2019-08-26 14:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('main', '0006_auto_20190822_1118'),
]
operations = [
migrations.AddField(
model_name='sitefeature',
name='main',
field=models.BooleanField(default=False, verbose_name='Main'),
),
]

View File

@ -133,6 +133,10 @@ class SiteSettings(ProjectBaseMixin):
return self.feature_set.filter(sitefeature__site_settings=self,
sitefeature__published=True)
@property
def published_sitefeatures(self):
return self.sitefeature_set.filter(published=True)
@property
def site_url(self):
return methods.site_url(schema=settings.SCHEMA_URI,
@ -168,6 +172,7 @@ class SiteFeature(ProjectBaseMixin):
site_settings = models.ForeignKey(SiteSettings, on_delete=models.CASCADE)
feature = models.ForeignKey(Feature, on_delete=models.PROTECT)
published = models.BooleanField(default=False, verbose_name=_('Published'))
main = models.BooleanField(default=False, verbose_name=_('Main'))
objects = SiteFeatureQuerySet.as_manager()

View File

@ -16,11 +16,24 @@ class FeatureSerializer(serializers.ModelSerializer):
)
class SiteFeatureSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(source='feature.id')
slug = serializers.CharField(source='feature.slug')
class Meta:
"""Meta class."""
model = models.SiteFeature
fields = ('main',
'id',
'slug')
class SiteSettingsSerializer(serializers.ModelSerializer):
"""Site settings serializer."""
published_features = FeatureSerializer(many=True, allow_null=None)
published_features = SiteFeatureSerializer(source='published_sitefeatures',
many=True, allow_null=True)
#todo: remove this
country_code = serializers.CharField(source='subdomain', read_only=True)
@ -42,16 +55,16 @@ class SiteSettingsSerializer(serializers.ModelSerializer):
)
class SiteFeatureSerializer(serializers.ModelSerializer):
"""Site feature serializer."""
class Meta:
"""Meta class."""
model = models.SiteFeature
fields = (
'id',
'published',
'site_settings',
'feature',
)
# class SiteFeatureSerializer(serializers.ModelSerializer):
# """Site feature serializer."""
#
# class Meta:
# """Meta class."""
#
# model = models.SiteFeature
# fields = (
# 'id',
# 'published',
# 'site_settings',
# 'feature',
# )

View File

@ -52,15 +52,16 @@ CONTRIB_APPS = [
PROJECT_APPS = [
'advertisement.apps.AdvertisementConfig',
'account.apps.AccountConfig',
'authorization.apps.AuthorizationConfig',
'collection.apps.CollectionConfig',
'establishment.apps.EstablishmentConfig',
'location.apps.LocationConfig',
'main.apps.MainConfig',
'news.apps.NewsConfig',
'translation.apps.TranslationConfig',
'collection.apps.CollectionConfig',
'partner.apps.PartnerConfig',
'advertisement.apps.AdvertisementConfig',
'translation.apps.TranslationConfig',
]
EXTERNAL_APPS = [

View File

@ -19,8 +19,9 @@ app_name = 'web'
urlpatterns = [
path('account/', include('account.urls.web')),
path('news/', include('news.urls.web')),
path('advertisement/', include('advertisement.urls.web')),
path('collection/', include('collection.urls.web')),
path('establishments/', include('establishment.urls.web')),
path('news/', include('news.urls.web')),
path('partner/', include('partner.urls.web')),
path('advertisement/', include('advertisement.urls.web'))
]

View File

@ -1,2 +1,3 @@
-r base.txt
ipdb
ipython