refactored guide ad label photos

This commit is contained in:
Anatoly 2019-12-13 12:21:31 +03:00
parent 3bbd76a4f8
commit 86257153ac
5 changed files with 123 additions and 10 deletions

View File

@ -0,0 +1,34 @@
# Generated by Django 2.2.7 on 2019-12-13 08:59
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('gallery', '0006_merge_20191027_1758'),
('collection', '0023_advertorial'),
]
operations = [
migrations.CreateModel(
name='AdvertorialGallery',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('is_main', models.BooleanField(default=False, verbose_name='Is the main image')),
('advertorial', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='advertorial_gallery', to='collection.Advertorial', verbose_name='advertorial')),
('image', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='advertorial_gallery', to='gallery.Image', verbose_name='image')),
],
options={
'verbose_name': 'advertorial gallery',
'verbose_name_plural': 'advertorial galleries',
'unique_together': {('advertorial', 'image')},
},
),
migrations.AddField(
model_name='advertorial',
name='gallery',
field=models.ManyToManyField(through='collection.AdvertorialGallery', to='gallery.Image'),
),
]

View File

@ -10,6 +10,7 @@ from utils.models import ProjectBaseMixin, URLImageMixin
from utils.models import TJSONField
from utils.models import TranslatedFieldsMixin
from utils.querysets import RelatedObjectsCountMixin
from utils.models import IntermediateGalleryModelMixin, GalleryModelMixin
# Mixins
@ -210,7 +211,7 @@ class AdvertorialQuerySet(models.QuerySet):
"""QuerySet for model Advertorial."""
class Advertorial(ProjectBaseMixin):
class Advertorial(GalleryModelMixin, ProjectBaseMixin):
"""Guide advertorial model."""
number_of_pages = models.PositiveIntegerField(
verbose_name=_('number of pages'),
@ -222,6 +223,7 @@ class Advertorial(ProjectBaseMixin):
related_name='advertorial',
verbose_name=_('guide element'))
old_id = models.IntegerField(blank=True, null=True)
gallery = models.ManyToManyField('gallery.Image', through='AdvertorialGallery')
objects = AdvertorialQuerySet.as_manager()
@ -231,6 +233,24 @@ class Advertorial(ProjectBaseMixin):
verbose_name_plural = _('advertorials')
class AdvertorialGallery(IntermediateGalleryModelMixin):
"""Advertorial gallery model."""
advertorial = models.ForeignKey(Advertorial, null=True,
related_name='advertorial_gallery',
on_delete=models.CASCADE,
verbose_name=_('advertorial'))
image = models.ForeignKey('gallery.Image', null=True,
related_name='advertorial_gallery',
on_delete=models.CASCADE,
verbose_name=_('image'))
class Meta:
"""Meta class."""
verbose_name = _('advertorial gallery')
verbose_name_plural = _('advertorial galleries')
unique_together = (('advertorial', 'image'), )
class GuideFilterQuerySet(models.QuerySet):
"""QuerySet for model GuideFilter."""

View File

@ -1,15 +1,18 @@
from pprint import pprint
from tqdm import tqdm
from collection.models import GuideElementSection, GuideElementSectionCategory, \
GuideWineColorSection, GuideElementType, GuideElement, \
Guide, Advertorial, AdvertorialGallery
from establishment.models import Establishment
from review.models import Review
from gallery.models import Image
from location.models import WineRegion, City
from product.models import Product
from review.models import Review
from transfer.models import Guides, GuideFilters, GuideSections, GuideElements, \
GuideAds
GuideAds, LabelPhotos
from transfer.serializers.guide import GuideSerializer, GuideFilterSerializer
from collection.models import GuideElementSection, GuideElementSectionCategory, \
GuideWineColorSection, GuideElementType, GuideElement, \
Guide, Advertorial
def transfer_guide():
@ -285,6 +288,42 @@ def transfer_guide_element_advertorials():
print(f'COUNT OF CREATED OBJECTS: {len(objects_to_update)}')
def transfer_guide_element_advertorial_galleries():
"""Transfer galleries for Guide Advertorial model."""
def get_guide_element_advertorial(old_id: int):
if old_id:
qs = Advertorial.objects.filter(old_id=old_id)
legacy_qs = GuideAds.objects.filter(id=old_id)
if qs.exists() and legacy_qs.exists():
return qs.first()
elif legacy_qs.exists() and not qs.exists():
raise ValueError(f'Guide element advertorials was not transfer correctly - {old_id}.')
created_counter = 0
gallery_obj_exists_counter = 0
advertorial_galleries = LabelPhotos.objects.exclude(guide_ad__isnull=False) \
.values_list('guide_ad', 'attachment_suffix_url')
for guide_ad, attachment_suffix_url in tqdm(advertorial_galleries):
advertorial = get_guide_element_advertorial(guide_ad.id)
image, _ = Image.objects.get_or_create(image=attachment_suffix_url,
defaults={
'image': attachment_suffix_url,
'orientation': Image.HORIZONTAL,
'title': f'{advertorial.name} - '
f'{attachment_suffix_url}',
})
city_gallery, created = AdvertorialGallery.objects.get_or_create(image=image,
advertorial=advertorial,
is_main=True)
if created:
created_counter += 1
else:
gallery_obj_exists_counter += 1
print(f'Created: {created_counter}\n'
f'Already added: {gallery_obj_exists_counter}')
data_types = {
'guides': [
transfer_guide,
@ -306,6 +345,9 @@ data_types = {
],
'guide_element_advertorials': [
transfer_guide_element_advertorials
],
'guide_element_advertorial_galleries': [
],
'guide_complete': [
transfer_guide, # transfer guides from Guides
@ -315,5 +357,6 @@ data_types = {
transfer_guide_element_type, # partial transfer section types from GuideElements
transfer_guide_elements_bulk, # transfer result of GuideFilters from GuideElements
transfer_guide_element_advertorials, # transfer advertorials that linked to GuideElements
transfer_guide_element_advertorial_galleries, # transfer advertorial galleries
]
}

View File

@ -1222,3 +1222,19 @@ class Footers(MigrateMixin):
class Meta:
managed = False
db_table = 'footers'
class LabelPhotos(MigrateMixin):
using = 'legacy'
guide_ad = models.ForeignKey(GuideAds, models.DO_NOTHING, blank=True, null=True)
attachment_file_name = models.CharField(max_length=255)
attachment_content_type = models.CharField(max_length=255)
attachment_file_size = models.IntegerField()
attachment_updated_at = models.DateTimeField()
attachment_suffix_url = models.DateTimeField()
geometries = models.CharField(max_length=1024)
class Meta:
managed = False
db_table = 'label_photos'

View File

@ -361,6 +361,10 @@ class GMTokenGenerator(PasswordResetTokenGenerator):
class GalleryModelMixin(models.Model):
"""Mixin for models that has gallery."""
class Meta:
"""Meta class."""
abstract = True
@property
def crop_gallery(self):
if hasattr(self, 'gallery'):
@ -400,10 +404,6 @@ class GalleryModelMixin(models.Model):
)
return image_property
class Meta:
"""Meta class."""
abstract = True
class IntermediateGalleryModelQuerySet(models.QuerySet):
"""Extended QuerySet."""