From b3bd2a714127dfa9ae513ea778dd6e0075823246 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 1 Nov 2019 10:48:46 +0300 Subject: [PATCH] add_establishment_social command --- .../commands/add_establishment_social.py | 56 +++++++++++++++++++ .../migrations/0050_socialnetwork_old_id.py | 18 ++++++ .../migrations/0051_auto_20191101_0732.py | 18 ++++++ apps/establishment/models.py | 3 +- apps/transfer/models.py | 1 + 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 apps/establishment/management/commands/add_establishment_social.py create mode 100644 apps/establishment/migrations/0050_socialnetwork_old_id.py create mode 100644 apps/establishment/migrations/0051_auto_20191101_0732.py diff --git a/apps/establishment/management/commands/add_establishment_social.py b/apps/establishment/management/commands/add_establishment_social.py new file mode 100644 index 00000000..647d8764 --- /dev/null +++ b/apps/establishment/management/commands/add_establishment_social.py @@ -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.')) diff --git a/apps/establishment/migrations/0050_socialnetwork_old_id.py b/apps/establishment/migrations/0050_socialnetwork_old_id.py new file mode 100644 index 00000000..0a057703 --- /dev/null +++ b/apps/establishment/migrations/0050_socialnetwork_old_id.py @@ -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'), + ), + ] diff --git a/apps/establishment/migrations/0051_auto_20191101_0732.py b/apps/establishment/migrations/0051_auto_20191101_0732.py new file mode 100644 index 00000000..ff834f46 --- /dev/null +++ b/apps/establishment/migrations/0051_auto_20191101_0732.py @@ -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'), + ), + ] diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 957333ac..3488e4e7 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -661,11 +661,12 @@ class Menu(TranslatedFieldsMixin, BaseAttributes): class SocialNetwork(models.Model): + old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None) establishment = models.ForeignKey( 'Establishment', verbose_name=_('establishment'), related_name='socials', on_delete=models.CASCADE) title = models.CharField(_('title'), max_length=255) - url = models.URLField(_('URL')) + url = models.URLField(_('URL'), max_length=255) class Meta: verbose_name = _('social network') diff --git a/apps/transfer/models.py b/apps/transfer/models.py index d81479a7..0ac0ee8a 100644 --- a/apps/transfer/models.py +++ b/apps/transfer/models.py @@ -553,6 +553,7 @@ class EstablishmentInfos(MigrateMixin): website = 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) + instagram = models.TextField(blank=True, null=True) lafourchette = models.CharField(max_length=255, blank=True, null=True) pub = models.IntegerField(blank=True, null=True) created_at = models.DateTimeField()