add_establishment_social command
This commit is contained in:
parent
4be8582e7c
commit
b3bd2a7141
|
|
@ -0,0 +1,56 @@
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
|
from establishment.models import Establishment, SocialNetwork
|
||||||
|
from transfer.models import EstablishmentInfos
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Add social links values from old db to new db'
|
||||||
|
|
||||||
|
def handle(self, *args, **kwargs):
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
queryset = EstablishmentInfos.objects.exclude(
|
||||||
|
establishment_id__isnull=True
|
||||||
|
).values_list('id', 'establishment_id', 'facebook', 'twitter', 'instagram')
|
||||||
|
|
||||||
|
for id, es_id, facebook, twitter, instagram in queryset:
|
||||||
|
try:
|
||||||
|
establishment = Establishment.objects.get(old_id=es_id)
|
||||||
|
except Establishment.DoesNotExist:
|
||||||
|
continue
|
||||||
|
except Establishment.MultipleObjectsReturned:
|
||||||
|
establishment = Establishment.objects.filter(old_id=es_id).first()
|
||||||
|
else:
|
||||||
|
if facebook:
|
||||||
|
if 'facebook.com/' not in facebook:
|
||||||
|
facebook = 'https://www.facebook.com/' + facebook
|
||||||
|
obj, _ = SocialNetwork.objects.get_or_create(
|
||||||
|
old_id=id,
|
||||||
|
establishment=establishment,
|
||||||
|
title='facebook',
|
||||||
|
url=facebook,
|
||||||
|
)
|
||||||
|
count += 1
|
||||||
|
if twitter:
|
||||||
|
if 'twitter.com/' not in twitter:
|
||||||
|
twitter = 'https://www.twitter.com/' + twitter
|
||||||
|
obj, _ = SocialNetwork.objects.get_or_create(
|
||||||
|
old_id=id,
|
||||||
|
establishment=establishment,
|
||||||
|
title='twitter',
|
||||||
|
url=twitter,
|
||||||
|
)
|
||||||
|
count += 1
|
||||||
|
if instagram:
|
||||||
|
if 'instagram.com/' not in instagram:
|
||||||
|
instagram = 'https://www.instagram.com/' + instagram
|
||||||
|
obj, _ = SocialNetwork.objects.get_or_create(
|
||||||
|
old_id=id,
|
||||||
|
establishment=establishment,
|
||||||
|
title='instagram',
|
||||||
|
url=instagram,
|
||||||
|
)
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
self.stdout.write(self.style.WARNING(f'Created/updated {count} objects.'))
|
||||||
18
apps/establishment/migrations/0050_socialnetwork_old_id.py
Normal file
18
apps/establishment/migrations/0050_socialnetwork_old_id.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 2.2.4 on 2019-11-01 07:25
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('establishment', '0049_auto_20191031_1616'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='socialnetwork',
|
||||||
|
name='old_id',
|
||||||
|
field=models.PositiveIntegerField(blank=True, default=None, null=True, verbose_name='old id'),
|
||||||
|
),
|
||||||
|
]
|
||||||
18
apps/establishment/migrations/0051_auto_20191101_0732.py
Normal file
18
apps/establishment/migrations/0051_auto_20191101_0732.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 2.2.4 on 2019-11-01 07:32
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('establishment', '0050_socialnetwork_old_id'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='socialnetwork',
|
||||||
|
name='url',
|
||||||
|
field=models.URLField(max_length=255, verbose_name='URL'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -661,11 +661,12 @@ class Menu(TranslatedFieldsMixin, BaseAttributes):
|
||||||
|
|
||||||
|
|
||||||
class SocialNetwork(models.Model):
|
class SocialNetwork(models.Model):
|
||||||
|
old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
|
||||||
establishment = models.ForeignKey(
|
establishment = models.ForeignKey(
|
||||||
'Establishment', verbose_name=_('establishment'),
|
'Establishment', verbose_name=_('establishment'),
|
||||||
related_name='socials', on_delete=models.CASCADE)
|
related_name='socials', on_delete=models.CASCADE)
|
||||||
title = models.CharField(_('title'), max_length=255)
|
title = models.CharField(_('title'), max_length=255)
|
||||||
url = models.URLField(_('URL'))
|
url = models.URLField(_('URL'), max_length=255)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _('social network')
|
verbose_name = _('social network')
|
||||||
|
|
|
||||||
|
|
@ -553,6 +553,7 @@ class EstablishmentInfos(MigrateMixin):
|
||||||
website = models.CharField(max_length=255, blank=True, null=True)
|
website = models.CharField(max_length=255, blank=True, null=True)
|
||||||
facebook = models.CharField(max_length=255, blank=True, null=True)
|
facebook = models.CharField(max_length=255, blank=True, null=True)
|
||||||
twitter = models.CharField(max_length=255, blank=True, null=True)
|
twitter = models.CharField(max_length=255, blank=True, null=True)
|
||||||
|
instagram = models.TextField(blank=True, null=True)
|
||||||
lafourchette = models.CharField(max_length=255, blank=True, null=True)
|
lafourchette = models.CharField(max_length=255, blank=True, null=True)
|
||||||
pub = models.IntegerField(blank=True, null=True)
|
pub = models.IntegerField(blank=True, null=True)
|
||||||
created_at = models.DateTimeField()
|
created_at = models.DateTimeField()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user