update method to remove city duplicates

This commit is contained in:
Anatoly 2019-12-12 11:17:59 +03:00
parent 3dfd674aa4
commit 28bf782731
5 changed files with 71 additions and 7 deletions

View File

@ -0,0 +1,14 @@
# Generated by Django 2.2.7 on 2019-12-12 07:52
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('gallery', '0007_auto_20191110_1329'),
('gallery', '0007_auto_20191211_1528'),
]
operations = [
]

View File

@ -154,6 +154,29 @@ class City(GalleryModelMixin):
def __str__(self):
return self.name
@property
def _related_objects(self) -> list:
"""Return list of related objects."""
related_objects = []
for related_object in self._meta.related_objects:
related_objects.append(related_object)
return related_objects
@property
def _related_instances(self) -> set:
"""Return list of related instances."""
related_instances = []
related_names = [related_object.related_name
if related_object.related_name
else f'{related_object.name}_set'
for related_object in self._related_objects]
for related_object in related_names:
instances = getattr(self, f'{related_object}')
if instances.exists():
for instance in instances.all():
related_instances.append(instance)
return set(related_instances)
class CityGallery(IntermediateGalleryModelMixin):
"""Gallery for model City."""

View File

@ -489,22 +489,29 @@ def fix_location_models():
except FileNotFoundError:
ruby_data = get_ruby_data()
print('add_correct_location_models')
add_correct_location_models(ruby_data)
print('fix_location_address')
fix_location_address()
print('fix_location_collection')
fix_location_collection()
print('fix_award_type')
fix_award_type()
print('fix_role')
fix_role()
print('fix_news')
fix_news()
print('fix_reviews')
fix_reviews()
print('fix_tag_category')
fix_tag_category()
print('fix_chosen_tag')
fix_chosen_tag()
def remove_old_records():
clean_old_records(City, {"mysql_id__isnull": True})
clean_old_records(Region, {"mysql_ids__isnull": True})
clean_old_records(City, {"mysql_id__isnull": True})
def transfer_city_gallery():
@ -716,7 +723,6 @@ data_types = {
"remove_old_locations": [
remove_old_records
],
"fill_city_gallery": [
transfer_city_gallery
],

View File

View File

@ -23,6 +23,7 @@ def transfer_objects(data_type):
def clean_old_records(model, conditions):
error_file = open(f"{settings.PROJECT_ROOT}/apps/transfer/clear.error.txt", "w")
non_existed_correct_city_ids = []
try:
old_records = model.objects.filter(**conditions)
@ -31,7 +32,27 @@ def clean_old_records(model, conditions):
return
for old_record in old_records:
try:
old_record.delete()
except Exception as e:
error_file.write(f"Cannot delete object {old_record}: {e}")
correct_record_qs = model.objects.exclude(id=old_record.id) \
.exclude(**conditions) \
.filter(name=old_record.name)
if correct_record_qs.exists():
correct_record = correct_record_qs.first()
for rel_instance in old_record._related_instances:
field_name = [related_field.name
for related_field in rel_instance._meta.fields
if related_field.related_model == correct_record._meta.model][0]
# rebinding correct dependency instances
if getattr(rel_instance, field_name) != correct_record:
setattr(rel_instance, field_name, correct_record)
rel_instance.save()
else:
non_existed_correct_city_ids.append(old_record.id)
if non_existed_correct_city_ids:
print(f"Non existed correct city ids: {non_existed_correct_city_ids}")
return
# delete old records
counter = old_records.count()
old_records.delete()
print(f'Deleted {counter} objects.')