diff --git a/Dockerfile b/Dockerfile index ac63ed76..3564a933 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,6 @@ FROM python:3.7 ENV PYTHONUNBUFFERED 1 RUN apt-get update; apt-get --assume-yes install binutils libproj-dev gdal-bin gettext -RUN apt-get install python3-dev libmysqlclient-dev RUN mkdir /code WORKDIR /code ADD . /code/ diff --git a/apps/migrates/apps.py b/apps/migrates/apps.py deleted file mode 100644 index 60537525..00000000 --- a/apps/migrates/apps.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.apps import AppConfig - - -class MigratesConfig(AppConfig): - name = 'migrates' diff --git a/apps/migrates/__init__.py b/apps/transfer/__init__.py similarity index 100% rename from apps/migrates/__init__.py rename to apps/transfer/__init__.py diff --git a/apps/migrates/admin.py b/apps/transfer/admin.py similarity index 100% rename from apps/migrates/admin.py rename to apps/transfer/admin.py diff --git a/apps/transfer/apps.py b/apps/transfer/apps.py new file mode 100644 index 00000000..321ffb83 --- /dev/null +++ b/apps/transfer/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig +from django.utils.translation import gettext_lazy as _ + + +class TransferConfig(AppConfig): + name = 'transfer' + verbose_name = _('Transfer') diff --git a/apps/migrates/migrations/__init__.py b/apps/transfer/management/__init__.py similarity index 100% rename from apps/migrates/migrations/__init__.py rename to apps/transfer/management/__init__.py diff --git a/apps/transfer/management/commands/__init__.py b/apps/transfer/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/transfer/management/commands/transfer.py b/apps/transfer/management/commands/transfer.py new file mode 100644 index 00000000..66ed0a2c --- /dev/null +++ b/apps/transfer/management/commands/transfer.py @@ -0,0 +1,20 @@ +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = 'Transfer data between databases' + + def handle(self, *args, **options): + if options['short']: + import __hello__ + else: + import this + + def add_arguments(self, parser): + parser.add_argument( + '-s', + '--short', + action='store_true', + default=False, + help='Вывод короткого сообщения' + ) diff --git a/apps/transfer/migrations/__init__.py b/apps/transfer/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/transfer/mixins.py b/apps/transfer/mixins.py new file mode 100644 index 00000000..88cfe091 --- /dev/null +++ b/apps/transfer/mixins.py @@ -0,0 +1,24 @@ +from django.db import models + + +class SecondDbManager(models.Manager): + def get_queryset(self): + qs = super().get_queryset() + + # if `use_db` is set on model use that for choosing the DB + if hasattr(self.model, 'use_db'): + qs = qs.using(self.model.use_db) + + return qs + + +class MigrateMixin(models.Model): + """Mixin to data transfer from legacy database""" + use_db = 'legacy' + objects = SecondDbManager() + + def _parse_instance_fields(self): + print(getattr(self, "using", "Using not found")) + + class Meta: + abstract = True diff --git a/apps/migrates/models.py b/apps/transfer/models.py similarity index 95% rename from apps/migrates/models.py rename to apps/transfer/models.py index 7ce617c5..4a0931d8 100644 --- a/apps/migrates/models.py +++ b/apps/transfer/models.py @@ -7,8 +7,10 @@ # Feel free to rename the models, but don't rename db_table values or field names. from django.contrib.gis.db import models +from transfer.mixins import MigrateMixin -class Cities(models.Model): + +class Cities(MigrateMixin): using = 'legacy' name = models.CharField(max_length=255, blank=True, null=True) @@ -36,7 +38,7 @@ class Cities(models.Model): unique_together = (('name', 'region_code', 'country_code'),) -class CityNames(models.Model): +class CityNames(MigrateMixin): using = 'legacy' name = models.CharField(max_length=100, blank=True, null=True) @@ -51,7 +53,7 @@ class CityNames(models.Model): unique_together = (('city', 'name', 'locale'),) -class CityPhotos(models.Model): +class CityPhotos(MigrateMixin): using = 'legacy' city_id = models.IntegerField(blank=True, null=True) diff --git a/apps/migrates/tests.py b/apps/transfer/tests.py similarity index 100% rename from apps/migrates/tests.py rename to apps/transfer/tests.py diff --git a/apps/migrates/views.py b/apps/transfer/views.py similarity index 100% rename from apps/migrates/views.py rename to apps/transfer/views.py diff --git a/project/settings/base.py b/project/settings/base.py index 2fca1b46..98bede78 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -71,6 +71,7 @@ PROJECT_APPS = [ 'review.apps.ReviewConfig', 'comment.apps.CommentConfig', 'favorites.apps.FavoritesConfig', + 'transfer.apps.TransferConfig' ] EXTERNAL_APPS = [ @@ -91,7 +92,6 @@ EXTERNAL_APPS = [ 'rest_framework_simplejwt.token_blacklist', 'solo', 'phonenumber_field', - 'apps.migrates' ]