Merge branch 'feature/affilations/award_type' into 'develop'
Feature/affilations/award type See merge request gm/gm-backend!104
This commit is contained in:
commit
b5789a1a5e
|
|
@ -54,4 +54,4 @@ class Command(BaseCommand):
|
|||
objects.append(est_empl)
|
||||
|
||||
ee = EstablishmentEmployee.objects.bulk_create(objects)
|
||||
self.stdout.write(self.style.WARNING(f'Created establishment employee objects.'))
|
||||
self.stdout.write(self.style.WARNING(f'Created employee position objects.'))
|
||||
|
|
|
|||
0
apps/main/management/__init__.py
Normal file
0
apps/main/management/__init__.py
Normal file
0
apps/main/management/commands/__init__.py
Normal file
0
apps/main/management/commands/__init__.py
Normal file
40
apps/main/management/commands/add_award.py
Normal file
40
apps/main/management/commands/add_award.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.db import connections
|
||||
from establishment.management.commands.add_position import namedtuplefetchall
|
||||
from main.models import Award, AwardType
|
||||
from establishment.models import Employee
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '''Add award from old db to new db.
|
||||
Run after command add_award_type!'''
|
||||
|
||||
def award_sql(self):
|
||||
with connections['legacy'].cursor() as cursor:
|
||||
cursor.execute('''
|
||||
select
|
||||
DISTINCT
|
||||
a.id, a.profile_id, a.title,
|
||||
a.`year` as vintage_year, a.state,
|
||||
t.id as award_type
|
||||
from awards as a
|
||||
join award_types t on t.id = a.award_type_id
|
||||
join profiles p on p.id = a.profile_id
|
||||
''')
|
||||
return namedtuplefetchall(cursor)
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
objects =[]
|
||||
for a in self.award_sql():
|
||||
profile = Employee.objects.filter(old_id=a.profile_id).first()
|
||||
type = AwardType.objects.filter(old_id=a.award_type).first()
|
||||
state = Award.PUBLISHED if a.state == 'published' else Award.WAITING
|
||||
if profile and type:
|
||||
award = Award(award_type=type, vintage_year=a.vintage_year,
|
||||
title={"en-GB": a.title}, state=state,
|
||||
content_object=profile, old_id=a.id)
|
||||
objects.append(award)
|
||||
awards = Award.objects.bulk_create(objects)
|
||||
self.stdout.write(self.style.WARNING(f'Created awards objects.'))
|
||||
|
||||
|
||||
34
apps/main/management/commands/add_award_type.py
Normal file
34
apps/main/management/commands/add_award_type.py
Normal file
|
|
@ -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 award types 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 award types objects.'))
|
||||
18
apps/main/migrations/0032_awardtype_old_id.py
Normal file
18
apps/main/migrations/0032_awardtype_old_id.py
Normal file
|
|
@ -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),
|
||||
),
|
||||
]
|
||||
23
apps/main/migrations/0033_auto_20191106_0744.py
Normal file
23
apps/main/migrations/0033_auto_20191106_0744.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 2.2.4 on 2019-11-06 07:44
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('main', '0032_awardtype_old_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='award',
|
||||
name='old_id',
|
||||
field=models.IntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='award',
|
||||
name='state',
|
||||
field=models.PositiveSmallIntegerField(choices=[(0, 'waiting'), (1, 'published')], default=0, verbose_name='State'),
|
||||
),
|
||||
]
|
||||
|
|
@ -164,6 +164,14 @@ class SiteFeature(ProjectBaseMixin):
|
|||
|
||||
class Award(TranslatedFieldsMixin, URLImageMixin, models.Model):
|
||||
"""Award model."""
|
||||
WAITING = 0
|
||||
PUBLISHED = 1
|
||||
|
||||
STATE_CHOICES = (
|
||||
(WAITING,'waiting'),
|
||||
(PUBLISHED, 'published')
|
||||
)
|
||||
|
||||
award_type = models.ForeignKey('main.AwardType', on_delete=models.CASCADE)
|
||||
title = TJSONField(
|
||||
_('title'), null=True, blank=True,
|
||||
|
|
@ -174,6 +182,11 @@ class Award(TranslatedFieldsMixin, URLImageMixin, models.Model):
|
|||
object_id = models.PositiveIntegerField()
|
||||
content_object = generic.GenericForeignKey('content_type', 'object_id')
|
||||
|
||||
state = models.PositiveSmallIntegerField(default=WAITING, choices=STATE_CHOICES,
|
||||
verbose_name=_('State'))
|
||||
|
||||
old_id = models.IntegerField(null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
title = 'None'
|
||||
lang = TranslationSettings.get_solo().default_language
|
||||
|
|
@ -187,6 +200,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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user