gault-millau/apps/transfer/management/commands/transfer.py
2019-10-03 16:37:57 +03:00

39 lines
1.1 KiB
Python

from django.core.management.base import BaseCommand
from django.apps import apps
from os.path import exists
from importlib.machinery import SourceFileLoader
from transfer.utils import transfer_objects
class Command(BaseCommand):
help = 'Transfer data between databases'
types = [
'dictionaries',
'keys'
]
def handle(self, *args, **options):
option = set(options.keys()) & set(self.types)
print(option)
if options['dictionaries']:
self._transfer_objects()
def add_arguments(self, parser):
for type in self.types:
parser.add_argument(
f'-{type[:1]}',
f'--{type}',
action='store_true',
default=False,
help=f'Transfer {type} objects'
)
def _transfer_objects(self):
for app in apps.get_app_configs():
if exists(f"{app.path}/transfer.py"):
card_module = SourceFileLoader("transfer", f"{app.path}/transfer.py").load_module()
transfer_objects(app, card_module.card)