From 3e3d7a21dbeb981634c5025154479666cffec52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Wed, 6 Nov 2019 10:23:31 +0300 Subject: [PATCH] Add award types --- apps/main/management/__init__.py | 0 apps/main/management/commands/__init__.py | 0 .../management/commands/add_award_type.py | 34 +++++++++++++++++++ apps/main/migrations/0032_awardtype_old_id.py | 18 ++++++++++ apps/main/models.py | 1 + 5 files changed, 53 insertions(+) create mode 100644 apps/main/management/__init__.py create mode 100644 apps/main/management/commands/__init__.py create mode 100644 apps/main/management/commands/add_award_type.py create mode 100644 apps/main/migrations/0032_awardtype_old_id.py diff --git a/apps/main/management/__init__.py b/apps/main/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/main/management/commands/__init__.py b/apps/main/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/main/management/commands/add_award_type.py b/apps/main/management/commands/add_award_type.py new file mode 100644 index 00000000..9e24c78b --- /dev/null +++ b/apps/main/management/commands/add_award_type.py @@ -0,0 +1,34 @@ +from django.core.management.base import BaseCommand +from django.db import connections +from establishment.management.commands.add_position import namedtuplefetchall +from main.models import AwardType +from location.models import Country + + +class Command(BaseCommand): + help = '''Add employee position from old db to new db. + Run after migrate country code!''' + + def award_types_sql(self): + with connections['legacy'].cursor() as cursor: + cursor.execute(''' + SELECT + DISTINCT + at.id, TRIM(at.title) AS name, + s.country_code_2 AS country_code + FROM award_types as at + JOIN sites s on s.id = at.site_id + WHERE LENGTH(TRIM(at.title))>0 + ''') + return namedtuplefetchall(cursor) + + def handle(self, *args, **kwargs): + objects =[] + for a in self.award_types_sql(): + country = Country.objects.filter(code=a.country_code).first() + if country: + type = AwardType(name=a.name, old_id=a.id) + type.country = country + objects.append(type) + types = AwardType.objects.bulk_create(objects) + self.stdout.write(self.style.WARNING(f'Created establishment employee objects.')) \ No newline at end of file diff --git a/apps/main/migrations/0032_awardtype_old_id.py b/apps/main/migrations/0032_awardtype_old_id.py new file mode 100644 index 00000000..7a9e9913 --- /dev/null +++ b/apps/main/migrations/0032_awardtype_old_id.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.4 on 2019-11-06 07:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0031_auto_20191103_2037'), + ] + + operations = [ + migrations.AddField( + model_name='awardtype', + name='old_id', + field=models.IntegerField(blank=True, null=True), + ), + ] diff --git a/apps/main/models.py b/apps/main/models.py index 97fe53c1..fbbf3701 100644 --- a/apps/main/models.py +++ b/apps/main/models.py @@ -184,6 +184,7 @@ class AwardType(models.Model): country = models.ForeignKey( 'location.Country', verbose_name=_('country'), on_delete=models.CASCADE) name = models.CharField(_('name'), max_length=255) + old_id = models.IntegerField(null=True, blank=True) def __str__(self): return self.name