add_establishment_mark command
This commit is contained in:
parent
7be3c2b93f
commit
0d399cc44e
|
|
@ -0,0 +1,46 @@
|
|||
from pprint import pprint
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Q
|
||||
|
||||
from establishment.models import Establishment
|
||||
from transfer.models import Reviews
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Add description values from old db to new db'
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
count = 0
|
||||
valid_data = {}
|
||||
|
||||
queryset = Reviews.objects.exclude(
|
||||
Q(establishment_id__isnull=True) |
|
||||
Q(mark__isnull=True)
|
||||
).filter(aasm_state='published').values_list('establishment_id', 'mark', 'updated_at')
|
||||
|
||||
print(queryset.count())
|
||||
|
||||
for es_id, new_mark, new_date in queryset:
|
||||
try:
|
||||
mark, date = valid_data[es_id]
|
||||
except KeyError:
|
||||
valid_data[es_id] = (new_mark, new_date)
|
||||
else:
|
||||
if new_date > date:
|
||||
valid_data[es_id] = (new_mark, new_date)
|
||||
|
||||
for key, value in valid_data.items():
|
||||
try:
|
||||
establishment = Establishment.objects.get(old_id=key)
|
||||
except Establishment.DoesNotExist:
|
||||
continue
|
||||
except Establishment.MultipleObjectsReturned:
|
||||
establishment = Establishment.objects.filter(old_id=key).first()
|
||||
else:
|
||||
establishment.public_mark = int(value[0])
|
||||
establishment.save()
|
||||
count += 1
|
||||
break
|
||||
|
||||
self.stdout.write(self.style.WARNING(f'Updated {count} objects.'))
|
||||
Loading…
Reference in New Issue
Block a user