gault-millau/apps/account/management/commands/add_image.py
Виктор Гладких 251d29c461 Add image url
2019-11-08 11:29:45 +03:00

41 lines
1.7 KiB
Python

from django.core.management.base import BaseCommand
from django.db import connections
from django.db.models import Q
from establishment.management.commands.add_position import namedtuplefetchall
from account.models import User
class Command(BaseCommand):
help = 'Update accounts image from old db to new db'
def account_sql(self):
with connections['legacy'].cursor() as cursor:
cursor.execute('''
select REPLACE(p.image_url, 'original.png', p.attachment_file_name) as image_url,
p.attachment_file_name,
p.account_id
from
(
select
REPLACE(
REPLACE(t.url, 'original.jpg', t.attachment_file_name),
'original.jpeg', t.attachment_file_name
) as image_url,
t.attachment_file_name,
t.account_id
from
(
select a.account_id, a.attachment_file_name,
LOWER(trim(CONCAT(u.url, a.attachment_suffix_url))) as url
from account_pictures a,
(select 'https://s3.eu-central-1.amazonaws.com/gm-test.com/media/' url) u
) t
) p
''')
return namedtuplefetchall(cursor)
def handle(self, *args, **kwargs):
for a in self.account_sql():
users = User.objects.filter(old_id=a.account_id)
users.update(image_url= a.image_url)
self.stdout.write(self.style.WARNING(f'Update accounts image url.'))