gault-millau/apps/transfer/management/commands/transfer.py
2019-12-17 11:54:50 +03:00

89 lines
4.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', # №2 - рецепты
'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', # №5 - перенос характиристик вин
'product', # №5 - перенос продуктов
'product_note', # №6 - перенос заметок продуктов
'souvenir', # №5 - перенос продуктов типа - сувениры
'establishment_note', # №5 - перенос заметок заведений
'assemblage', # №6 - перенос тегов для типа продуктов - вино
'rating_count',
'product_review',
'newsletter_subscriber', # подписчики на рассылку - переносить после переноса пользователей №1
'purchased_plaques', # №6 - перенос купленных тарелок
'fill_city_gallery', # №3 - перенос галереи городов
'guides',
'guide_filters',
'guide_element_sections',
'guide_wine_color_sections',
'guide_element_types',
'guide_elements_bulk',
'guide_element_advertorials',
'guide_complete',
'languages', # №4 - перенос языков
]
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'
)