gault-millau/apps/transfer/management/commands/transfer.py
2019-11-14 10:23:18 +03:00

74 lines
3.0 KiB
Python

from django.core.management.base import BaseCommand
from transfer.utils import transfer_objects
class Command(BaseCommand):
help = 'Transfer data between databases'
"""Типы данных для трансфера
ВНИМАНИЕ: первые буквы типов данных должны быть уникальны!
"""
SHORT_DATA_TYPES = [
'dictionaries', # №2 - перенос стран, регионов, городов, адресов
'news', # перенос новостей (после №2)
'account', # №1 - перенос пользователей
'subscriber',
'recipe',
'partner',
'establishment', # №3 - перенос заведений
'gallery',
'commercial', # перенос рекламмы (очередность не важна)
'overlook', # №5 - перенос языков, отзывов
'tmp',
'menu',
'location_establishment', # №4 - добавление адресов для перенесенных заведений
'whirligig',
'identities',
]
LONG_DATA_TYPES = [
'update_country_flag',
'comment',
'inquiries', # №6 - перенос запросов оценок
'wine_characteristics',
'product',
'product_note',
'souvenir',
'establishment_note',
'assemblage',
]
def handle(self, *args, **options):
"""Находим включённую опцию путём пересечения множества типов данных и множества включённых опций"""
data_type = list(set(option for option in options.keys() if options[option]) & set(self.LONG_DATA_TYPES))
if len(data_type) != 1:
data_type = list(set(option for option in options.keys() if options[option]) & set(self.SHORT_DATA_TYPES))
if len(data_type) != 1:
print("You must set correct option!\r\nYou can get options list with \r\n\r\n\tmanage.py help transfer\r\n")
exit(1)
else:
data_type = data_type[0]
else:
data_type = data_type[0]
transfer_objects(data_type)
def add_arguments(self, parser):
for option_type in self.LONG_DATA_TYPES:
parser.add_argument(
f'--{option_type}',
action='store_true',
default=False,
help=f'Transfer {option_type} objects'
)
"""Добавляем опции к команде, основываясь на типах данных, определённых в DATA_TYPES"""
for option_type in self.SHORT_DATA_TYPES:
parser.add_argument(
f'-{option_type[:1]}',
f'--{option_type}',
action='store_true',
default=False,
help=f'Transfer {option_type} objects'
)