# Migration list 975->288
* adds migration for app codelist to migrate existing biotope type details codes from list 975 to 288 depending on their atomID * improves rendering of action and biotope type details on frontend for KOM, OEK and EMA * refactors KonovaCode str() rendering
This commit is contained in:
		
							parent
							
								
									94e9035e10
								
							
						
					
					
						commit
						df241747cf
					
				@ -109,3 +109,6 @@ class BiotopeExtraCodeAutocomplete(KonovaCodeAutocomplete):
 | 
			
		||||
 | 
			
		||||
    def get_result_label(self, result):
 | 
			
		||||
        return f"{result.long_name} ({result.short_name})"
 | 
			
		||||
 | 
			
		||||
    def get_selected_result_label(self, result):
 | 
			
		||||
        return f"{result.parent.short_name} > {result.long_name} ({result.short_name})"
 | 
			
		||||
							
								
								
									
										49
									
								
								codelist/migrations/0002_migrate_975_to_288.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								codelist/migrations/0002_migrate_975_to_288.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
			
		||||
# Generated by Django 5.0.7 on 2024-08-06 13:40
 | 
			
		||||
 | 
			
		||||
from django.db import migrations
 | 
			
		||||
from django.db.models import Q
 | 
			
		||||
 | 
			
		||||
from codelist.settings import CODELIST_BIOTOPES_EXTRA_CODES_FULL_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def migrate_975_to_288(apps, schema_editor):
 | 
			
		||||
    KonovaCodeList = apps.get_model('codelist', 'KonovaCodeList')
 | 
			
		||||
    CompensationState = apps.get_model('compensation', 'CompensationState')
 | 
			
		||||
 | 
			
		||||
    list_288 = KonovaCodeList.objects.get(
 | 
			
		||||
        id=CODELIST_BIOTOPES_EXTRA_CODES_FULL_ID
 | 
			
		||||
    ).codes.all()
 | 
			
		||||
 | 
			
		||||
    states_with_extra_code = CompensationState.objects.filter(
 | 
			
		||||
        ~Q(biotope_type_details=None)
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    print(f"... Found {states_with_extra_code.count()} biotope state entries")
 | 
			
		||||
    for state in states_with_extra_code:
 | 
			
		||||
        extra_codes_975 = state.biotope_type_details.filter(
 | 
			
		||||
            code_lists__in=[CODELIST_BIOTOPES_EXTRA_CODES_ID]
 | 
			
		||||
        )
 | 
			
		||||
        count_extra_codes_975 = extra_codes_975.count()
 | 
			
		||||
        if count_extra_codes_975 > 0:
 | 
			
		||||
            print(f"    --> Found {count_extra_codes_975} codes from list 975 in biotope entry {state.id}")
 | 
			
		||||
        extra_codes_288 = []
 | 
			
		||||
        for extra_code_975 in extra_codes_975:
 | 
			
		||||
            atom_id = extra_code_975.atom_id
 | 
			
		||||
            codes_from_288 = list_288.filter(
 | 
			
		||||
                atom_id=atom_id,
 | 
			
		||||
            )
 | 
			
		||||
            extra_codes_288 += codes_from_288
 | 
			
		||||
 | 
			
		||||
        state.biotope_type_details.set(extra_codes_288)
 | 
			
		||||
    print("    --> Migrated to list 288 for all biotope entries")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('codelist', '0001_initial'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.RunPython(migrate_975_to_288)
 | 
			
		||||
    ]
 | 
			
		||||
@ -25,13 +25,11 @@ class KonovaCode(models.Model):
 | 
			
		||||
    )
 | 
			
		||||
    short_name = models.CharField(
 | 
			
		||||
        max_length=500,
 | 
			
		||||
        null=True,
 | 
			
		||||
        blank=True,
 | 
			
		||||
        help_text="Short version of long name",
 | 
			
		||||
    )
 | 
			
		||||
    long_name = models.CharField(
 | 
			
		||||
        max_length=1000,
 | 
			
		||||
        null=True,
 | 
			
		||||
        blank=True,
 | 
			
		||||
        help_text="",
 | 
			
		||||
    )
 | 
			
		||||
@ -54,12 +52,17 @@ class KonovaCode(models.Model):
 | 
			
		||||
        long_name = self.long_name
 | 
			
		||||
        short_name = self.short_name
 | 
			
		||||
 | 
			
		||||
        both_names_exist = long_name and short_name
 | 
			
		||||
        both_names_exist = long_name is not None and short_name is not None
 | 
			
		||||
 | 
			
		||||
        if both_names_exist:
 | 
			
		||||
            if with_parent:
 | 
			
		||||
                if self.parent and self.parent.long_name:
 | 
			
		||||
            if with_parent and self.parent:
 | 
			
		||||
                parent_short_name_exists = self.parent.short_name is not None
 | 
			
		||||
                parent_long_name_exists = self.parent.long_name is not None
 | 
			
		||||
                if parent_long_name_exists:
 | 
			
		||||
                    ret_val += self.parent.long_name + " > "
 | 
			
		||||
                elif parent_short_name_exists:
 | 
			
		||||
                    ret_val += self.parent.short_name + " > "
 | 
			
		||||
 | 
			
		||||
            ret_val += long_name
 | 
			
		||||
 | 
			
		||||
            if short_name and short_name != long_name:
 | 
			
		||||
 | 
			
		||||
@ -52,7 +52,7 @@
 | 
			
		||||
                    <hr>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
                    {% for detail in action.action_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.long_name }} > {{detail}}">{{ detail.parent.long_name }} > {{detail.long_name}}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No action type details' %}">{% trans 'No action type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -51,7 +51,7 @@
 | 
			
		||||
                    <span>{{ state.biotope_type.parent.long_name }} {% fa5_icon 'angle-right' %} {{ state.biotope_type.long_name }} ({{state.biotope_type.short_name}})</span>
 | 
			
		||||
                    <br>
 | 
			
		||||
                    {% for detail in state.biotope_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.short_name }} > {{detail}}">{{ detail.parent.short_name }} > {{ detail.long_name }}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No biotope type details' %}">{% trans 'No biotope type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -51,7 +51,7 @@
 | 
			
		||||
                    <span>{{ state.biotope_type.parent.long_name }} {% fa5_icon 'angle-right' %} {{ state.biotope_type.long_name }} ({{state.biotope_type.short_name}})</span>
 | 
			
		||||
                    <br>
 | 
			
		||||
                    {% for detail in state.biotope_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.short_name }} > {{detail}}">{{ detail.parent.short_name }} > {{ detail.long_name }}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No biotope type details' %}">{% trans 'No biotope type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -51,7 +51,7 @@
 | 
			
		||||
                    <hr>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
                    {% for detail in action.action_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.long_name }} > {{detail}}">{{ detail.parent.long_name }} > {{detail.long_name}}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No action type details' %}">{% trans 'No action type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -51,7 +51,7 @@
 | 
			
		||||
                    <span>{{ state.biotope_type.parent.long_name }} {% fa5_icon 'angle-right' %} {{ state.biotope_type.long_name }} ({{state.biotope_type.short_name}})</span>
 | 
			
		||||
                    <br>
 | 
			
		||||
                    {% for detail in state.biotope_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.short_name }} > {{detail}}">{{ detail.parent.short_name }} > {{ detail.long_name }}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No biotope type details' %}">{% trans 'No biotope type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -51,7 +51,7 @@
 | 
			
		||||
                    <span>{{ state.biotope_type.parent.long_name }} {% fa5_icon 'angle-right' %} {{ state.biotope_type.long_name }} ({{state.biotope_type.short_name}})</span>
 | 
			
		||||
                    <br>
 | 
			
		||||
                    {% for detail in state.biotope_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.short_name }} > {{detail}}">{{ detail.parent.short_name }} > {{ detail.long_name }}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No biotope type details' %}">{% trans 'No biotope type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -49,7 +49,7 @@
 | 
			
		||||
                    <hr>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
                    {% for detail in action.action_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.long_name }} > {{detail}}">{{ detail.parent.long_name }} > {{detail.long_name}}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No action type details' %}">{% trans 'No action type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -49,7 +49,7 @@
 | 
			
		||||
                    <span>{{ state.biotope_type.parent.long_name }} {% fa5_icon 'angle-right' %} {{ state.biotope_type.long_name }} ({{state.biotope_type.short_name}})</span>
 | 
			
		||||
                    <br>
 | 
			
		||||
                    {% for detail in state.biotope_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.short_name }} > {{detail}}">{{ detail.parent.short_name }} > {{ detail.long_name }}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No biotope type details' %}">{% trans 'No biotope type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
@ -49,7 +49,7 @@
 | 
			
		||||
                    <span>{{ state.biotope_type.parent.long_name }} {% fa5_icon 'angle-right' %} {{ state.biotope_type.long_name }} ({{state.biotope_type.short_name}})</span>
 | 
			
		||||
                    <br>
 | 
			
		||||
                    {% for detail in state.biotope_type_details.all %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{detail}}">{{detail.long_name}}</span>
 | 
			
		||||
                        <span class="badge badge-pill rlp-r" title="{{ detail.parent.short_name }} > {{detail}}">{{ detail.parent.short_name }} > {{ detail.long_name }}</span>
 | 
			
		||||
                    {% empty %}
 | 
			
		||||
                        <span class="badge badge-pill rlp-r-outline" title="{% trans 'No biotope type details' %}">{% trans 'No biotope type details' %}</span>
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user