Created by
Eugene Morozov
| from django.conf import settings
from django.utils import translation
from django.core.management.base import BaseCommand
from portal.location_funnel.models import LocationFunnel
def print_locations_csv(loc1, loc2):
if settings.LAOS_FLAVOUR:
print(f'{loc1.level_2},{loc1.level_3},{loc2.level_2},{loc2.level_3}')
else:
print(f'{loc1.level_1},{loc1.level_2},{loc1.level_3},{loc2.level_1},{loc2.level_2},{loc2.level_3}')
class Command(BaseCommand):
@translation.override('en')
def handle(self, **options):
level_3s = set(LocationFunnel.objects.values_list('level_3', flat=True).distinct())
level_2s = set(LocationFunnel.objects.values_list('level_2', flat=True).distinct())
for level_2 in level_2s:
duplicates = LocationFunnel.objects.filter(level_1=level_2).exclude(level_1='')
original = LocationFunnel.objects.filter(level_2=level_2).first()
for dup in duplicates:
print_locations_csv(dup, original)
for level_3 in level_3s:
duplicates = LocationFunnel.objects.filter(level_2=level_3)
original = LocationFunnel.objects.filter(level_3=level_3).first()
for dup in duplicates:
print_locations_csv(dup, original)
duplicates = LocationFunnel.objects.filter(level_1=level_3)
for dup in duplicates:
print_locations_csv(dup, original)
sublevels_to_check = (
LocationFunnel.objects
.filter(level_3=level_3)
.values_list('level_2', flat=True)
.distinct()
)
for level_2 in sublevels_to_check:
duplicates = LocationFunnel.objects.exclude(level_3=level_3).filter(level_2=level_2)
original = LocationFunnel.objects.filter(level_3=level_3, level_2=level_2).first()
if duplicates.exists():
for dup in duplicates:
print_locations_csv(dup, original)
if not settings.LAOS_FLAVOUR:
sublevels_to_check = (
LocationFunnel.objects
.filter(level_3=level_3)
.exclude(level_1='')
.values_list('level_1', flat=True)
.distinct()
)
for level_1 in sublevels_to_check:
duplicates = LocationFunnel.objects.exclude(level_3=level_3).filter(level_1=level_1)
original = LocationFunnel.objects.filter(level_3=level_3, level_1=level_1).first()
if duplicates.exists():
for dup in duplicates:
print_locations_csv(dup, original)
if not settings.LAOS_FLAVOUR:
for level_2 in level_2s:
sublevels_to_check = (
LocationFunnel.objects
.filter(level_2=level_2)
.exclude(level_1='')
.values_list('level_1', flat=True)
.distinct()
)
for level_1 in sublevels_to_check:
duplicates = LocationFunnel.objects.exclude(level_2=level_2).filter(level_1=level_1)
original = LocationFunnel.objects.filter(level_2=level_2, level_1=level_1).first()
for dup in duplicates:
print_locations_csv(dup, original)
|