Created
August 18, 2019 05:04
-
-
Save ilya-muhortov/c80cfd3bfe745c5f3c973bf1989ef5cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Generated by Django 1.9.13 on 2019-08-18 11:25 | |
from __future__ import unicode_literals | |
from django.db import migrations | |
def migrate_code(apps, schema_editor): | |
PatientInsurance = apps.get_model('patient', 'PatientInsurance') | |
NSI_05 = apps.get_model('directory', 'NSI_05') | |
F_002 = apps.get_model('directory', 'F_002') | |
for item in NSI_05.objects.filter(code__gt=250000): | |
if not F_002.objects.filter(code=item.code).exists(): | |
F_002.objects.create( | |
id=item.code, | |
name=item.name, | |
code=item.code, | |
priority=item.priority, | |
full_name=item.full_name | |
) | |
for patient in PatientInsurance.objects.filter(insurance__isnull=False): | |
if str(patient.insurance.code).startswith('25'): | |
patient.insurance_f002 = F_002.objects.filter( | |
code=patient.insurance.code | |
).first() | |
else: | |
insurance_company = None | |
if patient.insurance.kpp: | |
insurance_company = F_002.objects.filter( | |
kpp=patient.insurance.kpp | |
).first() | |
if insurance_company: | |
if insurance_company.name != patient.insurance.name: | |
insurance_company = None | |
if insurance_company is None: | |
patient.insurance_f002 = F_002.objects.filter( | |
code=patient.insurance.code | |
).first() | |
patient.insurance_f002 = insurance_company | |
patient.save() | |
def reverse_code_pass(apps, schema_editor): | |
return | |
class Migration(migrations.Migration): | |
dependencies = [ | |
('patient', '0102_patientinsurance_insurance_f002'), | |
] | |
operations = [ | |
migrations.RunPython(migrate_code, reverse_code=reverse_code_pass) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment