Switch application

Add application command
Update mixin
Add mixin to models
This commit is contained in:
littlewolf 2019-10-02 11:41:14 +03:00
parent e3d26b5dd1
commit 9dcdb7575e
14 changed files with 57 additions and 10 deletions

View File

@ -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/

View File

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

7
apps/transfer/apps.py Normal file
View File

@ -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')

View File

@ -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='Вывод короткого сообщения'
)

View File

24
apps/transfer/mixins.py Normal file
View File

@ -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

View File

@ -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)

View File

@ -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'
]