added collection gallery; create migrations for models - collection, gallery, news; fixed models - news, collections
This commit is contained in:
parent
0baeeef302
commit
0733323486
19
apps/collection/migrations/0006_auto_20190827_1205.py
Normal file
19
apps/collection/migrations/0006_auto_20190827_1205.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.2.4 on 2019-08-27 12:05
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('collection', '0005_auto_20190823_1401'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='collection',
|
||||
name='image',
|
||||
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='gallery.Image', verbose_name='Collection image'),
|
||||
),
|
||||
]
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
from django.contrib.postgres.fields import JSONField
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from utils.models import ProjectBaseMixin, ImageMixin
|
||||
|
|
@ -37,10 +38,17 @@ class CollectionQuerySet(models.QuerySet):
|
|||
"""Returned only published collection"""
|
||||
return self.filter(is_publish=True)
|
||||
|
||||
def valid(self):
|
||||
"""Returns valid offers"""
|
||||
now = timezone.now()
|
||||
return self.filter(start__lte=now, end__gte=now)
|
||||
|
||||
class Collection(ProjectBaseMixin, CollectionNameMixin,
|
||||
ImageMixin, CollectionDateMixin):
|
||||
|
||||
class Collection(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin):
|
||||
"""Collection model."""
|
||||
image = models.ForeignKey(
|
||||
'gallery.Image', blank=True, default=None, null=True,
|
||||
verbose_name=_('Collection image'), on_delete=models.CASCADE)
|
||||
is_publish = models.BooleanField(
|
||||
default=False, verbose_name=_('Publish status'))
|
||||
on_top = models.BooleanField(
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
from rest_framework import serializers
|
||||
|
||||
from collection import models
|
||||
from utils.serializers import ImageSerializerMixin
|
||||
|
||||
|
||||
class CollectionSerializer(serializers.ModelSerializer):
|
||||
class CollectionSerializer(ImageSerializerMixin, serializers.ModelSerializer):
|
||||
"""Collection serializer"""
|
||||
|
||||
class Meta:
|
||||
model = models.Collection
|
||||
fields = [
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ class CollectionListView(CollectionViewMixin, generics.ListAPIView):
|
|||
def get_queryset(self):
|
||||
"""Override get_queryset method"""
|
||||
return models.Collection.objects.published()\
|
||||
.valid()\
|
||||
.by_country_code(code=self.request.country_code)\
|
||||
.order_by('-on_top', '-created')
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from gallery.models import Gallery
|
||||
from gallery.models import Image
|
||||
|
||||
|
||||
@admin.register(Gallery)
|
||||
class GalleryModelAdmin(admin.ModelAdmin):
|
||||
"""Gallery model admin"""
|
||||
@admin.register(Image)
|
||||
class ImageModelAdmin(admin.ModelAdmin):
|
||||
"""Image model admin"""
|
||||
|
|
|
|||
30
apps/gallery/migrations/0001_initial.py
Normal file
30
apps/gallery/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Generated by Django 2.2.4 on 2019-08-27 12:04
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
import easy_thumbnails.fields
|
||||
import utils.methods
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Image',
|
||||
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(upload_to=utils.methods.image_path, verbose_name='Image')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Image',
|
||||
'verbose_name_plural': 'Images',
|
||||
},
|
||||
),
|
||||
]
|
||||
|
|
@ -1,16 +1,21 @@
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
from easy_thumbnails.fields import ThumbnailerImageField
|
||||
|
||||
from utils.methods import image_path
|
||||
from utils.models import ProjectBaseMixin, ImageMixin
|
||||
|
||||
|
||||
class Gallery(ProjectBaseMixin, ImageMixin):
|
||||
"""Gallery model."""
|
||||
class Image(ProjectBaseMixin, ImageMixin):
|
||||
"""Image model."""
|
||||
|
||||
image = ThumbnailerImageField(upload_to=image_path,
|
||||
verbose_name=_('Image'))
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
verbose_name = _('Gallery')
|
||||
verbose_name_plural = _('Galleries')
|
||||
verbose_name = _('Image')
|
||||
verbose_name_plural = _('Images')
|
||||
|
||||
def __str__(self):
|
||||
"""String representation"""
|
||||
return str(self.get_image_url)
|
||||
"""String representation of image instance"""
|
||||
return str(self.id)
|
||||
|
|
|
|||
|
|
@ -1,19 +1,24 @@
|
|||
"""Serializers for model Image"""
|
||||
from rest_framework import serializers
|
||||
|
||||
from . import models
|
||||
|
||||
|
||||
class GallerySerializer(serializers.ModelSerializer):
|
||||
"""Serializer for model Gallery."""
|
||||
class ImageSerializer(serializers.ModelSerializer):
|
||||
"""Serializer for model Image."""
|
||||
# REQUEST
|
||||
image = serializers.ImageField(write_only=True)
|
||||
# RESPONSE
|
||||
url = serializers.URLField(source='get_image_url')
|
||||
url = serializers.URLField(source='get_image_url',
|
||||
required=False)
|
||||
|
||||
class Meta:
|
||||
"""Meta class"""
|
||||
model = models.Gallery
|
||||
model = models.Image
|
||||
fields = (
|
||||
'id',
|
||||
'image',
|
||||
'url'
|
||||
'url',
|
||||
)
|
||||
read_only_fields = (
|
||||
'url',
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from . import views
|
|||
|
||||
app_name = 'gallery'
|
||||
|
||||
url_patterns = [
|
||||
urlpatterns = [
|
||||
path('upload/', views.GalleryUploadImage.as_view(),
|
||||
name='upload_image')
|
||||
]
|
||||
|
|
|
|||
|
|
@ -5,6 +5,6 @@ from . import models, serializers
|
|||
|
||||
class GalleryUploadImage(generics.CreateAPIView):
|
||||
"""Upload image to gallery"""
|
||||
model = models.Gallery
|
||||
queryset = models.Gallery.objects.all()
|
||||
serializer_class = serializers.GallerySerializer
|
||||
model = models.Image
|
||||
queryset = models.Image.objects.all()
|
||||
serializer_class = serializers.ImageSerializer
|
||||
|
|
|
|||
19
apps/news/migrations/0006_auto_20190827_1205.py
Normal file
19
apps/news/migrations/0006_auto_20190827_1205.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.2.4 on 2019-08-27 12:05
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('news', '0005_auto_20190823_1149'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='news',
|
||||
name='image',
|
||||
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='gallery.Image', verbose_name='News image'),
|
||||
),
|
||||
]
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
from django.contrib.postgres.fields import JSONField
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from utils.models import (ProjectBaseMixin, BaseAttributes,
|
||||
|
|
@ -36,8 +37,13 @@ class NewsQuerySet(models.QuerySet):
|
|||
"""Return only published news"""
|
||||
return self.filter(is_publish=True)
|
||||
|
||||
def valid(self):
|
||||
"""Returns valid offers"""
|
||||
now = timezone.now()
|
||||
return self.filter(start__lte=now, end__gte=now)
|
||||
|
||||
class News(ImageMixin, BaseAttributes):
|
||||
|
||||
class News(BaseAttributes):
|
||||
"""News model."""
|
||||
news_type = models.ForeignKey(
|
||||
NewsType, verbose_name=_('news type'), on_delete=models.CASCADE)
|
||||
|
|
@ -64,6 +70,9 @@ class News(ImageMixin, BaseAttributes):
|
|||
country = models.ForeignKey(
|
||||
'location.Country', blank=True, null=True,
|
||||
verbose_name=_('country'), on_delete=models.CASCADE)
|
||||
image = models.ForeignKey(
|
||||
'gallery.Image', blank=True, default=None, null=True,
|
||||
verbose_name=_('News image'), on_delete=models.CASCADE)
|
||||
is_highlighted = models.BooleanField(
|
||||
default=False, verbose_name=_('Is highlighted'))
|
||||
# TODO: metadata_keys - описание ключей для динамического построения полей метаданных
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ from rest_framework import serializers
|
|||
from location.models import Address
|
||||
from location.serializers import AddressSerializer
|
||||
from news import models
|
||||
from utils.serializers import ImageSerializerMixin
|
||||
|
||||
|
||||
class NewsTypeSerializer(serializers.ModelSerializer):
|
||||
|
|
@ -23,7 +24,7 @@ class NewsLocalizationMixinSerializer(serializers.ModelSerializer):
|
|||
description_trans = serializers.CharField(read_only=True)
|
||||
|
||||
|
||||
class NewsSerializer(NewsLocalizationMixinSerializer):
|
||||
class NewsSerializer(NewsLocalizationMixinSerializer, ImageSerializerMixin):
|
||||
"""News serializer."""
|
||||
address = AddressSerializer()
|
||||
|
||||
|
|
@ -50,7 +51,6 @@ class NewsCreateUpdateSerializer(NewsSerializer):
|
|||
title = serializers.JSONField()
|
||||
subtitle = serializers.JSONField()
|
||||
description = serializers.JSONField()
|
||||
image = serializers.ImageField(required=True)
|
||||
news_type = serializers.PrimaryKeyRelatedField(
|
||||
queryset=models.NewsType.objects.all(), write_only=True)
|
||||
address = serializers.PrimaryKeyRelatedField(
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ class NewsList(NewsViewMixin, JWTListAPIView):
|
|||
"""Override get_queryset method"""
|
||||
return News.objects.annotate_localized_fields(locale=self.request.locale)\
|
||||
.published()\
|
||||
.valid()\
|
||||
.by_country_code(code=self.request.country_code)\
|
||||
.order_by('-is_highlighted', '-created')
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
"""Utils app models."""
|
||||
|
||||
from os.path import exists
|
||||
|
||||
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
||||
from django.contrib.gis.db import models
|
||||
from django.contrib.postgres.fields import JSONField
|
||||
|
|
@ -74,7 +76,14 @@ class ImageMixin(models.Model):
|
|||
|
||||
def get_image_url(self, key=None):
|
||||
"""Get image thumbnail url."""
|
||||
return self.get_image(key).url if self.image else None
|
||||
return self.get_image(key).url if self.image and exists(self.image.path) else None
|
||||
|
||||
def get_full_image_url(self, request, thumbnail_key=None):
|
||||
"""Get full image url"""
|
||||
if self.image and exists(self.image.path):
|
||||
return request.build_absolute_uri(self.get_image(thumbnail_key).url)
|
||||
else:
|
||||
return None
|
||||
|
||||
def image_tag(self):
|
||||
"""Admin preview tag."""
|
||||
|
|
|
|||
15
apps/utils/serializers.py
Normal file
15
apps/utils/serializers.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
"""Serializer mixins"""
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class ImageSerializerMixin(serializers.Serializer):
|
||||
"""
|
||||
Image serializer mixin.
|
||||
Need to return an absolute path of cropped image instead of relative path
|
||||
"""
|
||||
# RESPONSE
|
||||
image = serializers.SerializerMethodField()
|
||||
|
||||
def get_image(self, obj):
|
||||
"""Get full image URL"""
|
||||
return obj.image.get_full_image_url(self.context.get('request'))
|
||||
|
|
@ -57,6 +57,7 @@ PROJECT_APPS = [
|
|||
'authorization.apps.AuthorizationConfig',
|
||||
'collection.apps.CollectionConfig',
|
||||
'establishment.apps.EstablishmentConfig',
|
||||
'gallery.apps.GalleryConfig',
|
||||
'location.apps.LocationConfig',
|
||||
'main.apps.MainConfig',
|
||||
'news.apps.NewsConfig',
|
||||
|
|
|
|||
|
|
@ -54,6 +54,8 @@ urlpatterns_auth = [
|
|||
]
|
||||
|
||||
api_urlpatterns = [
|
||||
path('gallery/', include(('gallery.urls', 'gallery'),
|
||||
namespace='gallery')),
|
||||
path('location/', include(('location.urls', 'location'),
|
||||
namespace='location')),
|
||||
path('main/', include(('main.urls', 'main'),
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user