Add image url

This commit is contained in:
Виктор Гладких 2019-11-08 11:29:45 +03:00
parent 1c8737a798
commit 251d29c461
3 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,41 @@
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.'))

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.4 on 2019-11-08 08:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0018_user_old_id'),
]
operations = [
migrations.AlterField(
model_name='user',
name='image_url',
field=models.URLField(blank=True, default=None, max_length=500, null=True, verbose_name='Image URL path'),
),
]

View File

@ -89,7 +89,8 @@ class UserQuerySet(models.QuerySet):
class User(AbstractUser): class User(AbstractUser):
"""Base user model.""" """Base user model."""
image_url = models.URLField(verbose_name=_('Image URL path'), image_url = models.URLField(verbose_name=_('Image URL path'),
blank=True, null=True, default=None) blank=True, null=True, default=None,
max_length=500)
cropped_image_url = models.URLField(verbose_name=_('Cropped image URL path'), cropped_image_url = models.URLField(verbose_name=_('Cropped image URL path'),
blank=True, null=True, default=None) blank=True, null=True, default=None)
email = models.EmailField(_('email address'), unique=True, email = models.EmailField(_('email address'), unique=True,