From d24c0cf4071c74e762ceab7a7305ce88df1d5e2e Mon Sep 17 00:00:00 2001 From: Anatoly Date: Wed, 21 Aug 2019 09:58:45 +0300 Subject: [PATCH] added app collection, added new models --- apps/collection/__init__.py | 0 apps/collection/admin.py | 19 ++++++ apps/collection/apps.py | 7 ++ apps/collection/migrations/__init__.py | 0 apps/collection/models.py | 92 ++++++++++++++++++++++++++ apps/collection/tests.py | 3 + apps/collection/views.py | 3 + project/settings/base.py | 1 + 8 files changed, 125 insertions(+) create mode 100644 apps/collection/__init__.py create mode 100644 apps/collection/admin.py create mode 100644 apps/collection/apps.py create mode 100644 apps/collection/migrations/__init__.py create mode 100644 apps/collection/models.py create mode 100644 apps/collection/tests.py create mode 100644 apps/collection/views.py diff --git a/apps/collection/__init__.py b/apps/collection/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/collection/admin.py b/apps/collection/admin.py new file mode 100644 index 00000000..90a32564 --- /dev/null +++ b/apps/collection/admin.py @@ -0,0 +1,19 @@ +from django.contrib.gis import admin + +from collection import models + + +@admin.register(models.Collection) +class CollectionAdmin(admin.ModelAdmin): + """Collection admin.""" + + +@admin.register(models.CollectionItem) +class CollectionItemAdmin(admin.ModelAdmin): + """CollectionItem admin.""" + + +@admin.register(models.Guide) +class GuideAdmin(admin.ModelAdmin): + """Guide admin.""" + diff --git a/apps/collection/apps.py b/apps/collection/apps.py new file mode 100644 index 00000000..49f489c8 --- /dev/null +++ b/apps/collection/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig +from django.utils.translation import gettext_lazy as _ + + +class CollectionConfig(AppConfig): + name = 'collection' + verbose_name = _('collection') diff --git a/apps/collection/migrations/__init__.py b/apps/collection/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/collection/models.py b/apps/collection/models.py new file mode 100644 index 00000000..b02d57f9 --- /dev/null +++ b/apps/collection/models.py @@ -0,0 +1,92 @@ +from django.db import models +from django.utils.translation import gettext_lazy as _ +from django.contrib.postgres.fields import JSONField +from utils.models import ProjectBaseMixin + + +# Mixins +class CollectionNameMixin(models.Model): + """CollectionName mixin""" + name = models.CharField(_('name'), max_length=250) + + class Meta: + """Meta class""" + abstract = True + + +class CollectionDateMixin(models.Model): + """CollectionDate mixin""" + start = models.DateTimeField(_('start')) + end = models.DateTimeField(_('end')) + + class Meta: + """Meta class""" + abstract = True + + +# Models +class CollectionQuerySet(models.QuerySet): + """QuerySet for model Collection""" + + def by_country(self, country_id): + """Filter collection by country id.""" + return self.filter(country=country_id) + + +class Collection(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin): + """Collection model.""" + filters = JSONField( + _('filters'), null=True, blank=True, + default=None, help_text='{"key":"value"}') + selectors = JSONField( + _('selectors'), null=True, blank=True, + default=None, help_text='{"key":"value"}') + targets = JSONField( + _('targets'), null=True, blank=True, + default=None, help_text='{"key":"value"}') + country = models.ForeignKey( + 'location.Country', verbose_name=_('country'), on_delete=models.CASCADE) + + objects = CollectionQuerySet.as_manager() + + +class CollectionItemQuerySet(models.QuerySet): + """QuerySet for model CollectionItem.""" + + def by_collection(self, collection_id): + """Filter by collection id""" + return self.filter(collection=collection_id) + + +class CollectionItem(ProjectBaseMixin): + """CollectionItem model.""" + collection = models.ForeignKey( + Collection, verbose_name=_('collection'), on_delete=models.CASCADE) + item_type = models.IntegerField(verbose_name=_('item type identifier')) + item_ids = JSONField( + _('item_ids'), null=True, blank=True, + default=None, help_text='{"key":"value"}') + + objects = CollectionItemQuerySet.as_manager() + + +class GuideQuerySet(models.QuerySet): + """QuerySet for Guide.""" + + def by_collection_id(self, collection_id): + """Filter by collection id""" + return self.filter(collection=collection_id) + + +class Guide(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin): + """Guide model.""" + guide_fk = models.ForeignKey( + 'self', verbose_name=_('guide'), on_delete=models.CASCADE) + advertorials = JSONField( + _('advertorials'), null=True, blank=True, + default=None, help_text='{"key":"value"}') + collection = models.ForeignKey( + Collection, verbose_name=_('collection'), on_delete=models.CASCADE) + + objects = GuideQuerySet.as_manager() + diff --git a/apps/collection/tests.py b/apps/collection/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/apps/collection/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/apps/collection/views.py b/apps/collection/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/apps/collection/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/project/settings/base.py b/project/settings/base.py index 97c0ee04..3f5fcd37 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -58,6 +58,7 @@ PROJECT_APPS = [ 'main.apps.MainConfig', 'news.apps.NewsConfig', 'translation.apps.TranslationConfig', + 'collection.apps.CollectionConfig', ] EXTERNAL_APPS = [