#112 WIP: Restructure CompensationAction
* changes action_type from ForeignKey into M2M * adds migration * changes form widget * WIP: changes rendering on detail view of compensation * TEST NOT CHECKED YET!
This commit is contained in:
		
							parent
							
								
									1028c19f87
								
							
						
					
					
						commit
						2b66189590
					
				@ -405,7 +405,7 @@ class NewActionModalForm(BaseModalForm):
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    from compensation.models import UnitChoices
 | 
			
		||||
    action_type = forms.ModelChoiceField(
 | 
			
		||||
    action_type = forms.ModelMultipleChoiceField(
 | 
			
		||||
        label=_("Action Type"),
 | 
			
		||||
        label_suffix="",
 | 
			
		||||
        required=True,
 | 
			
		||||
@ -415,7 +415,7 @@ class NewActionModalForm(BaseModalForm):
 | 
			
		||||
            is_leaf=True,
 | 
			
		||||
            code_lists__in=[CODELIST_COMPENSATION_ACTION_ID],
 | 
			
		||||
        ),
 | 
			
		||||
        widget=autocomplete.ModelSelect2(
 | 
			
		||||
        widget=autocomplete.ModelSelect2Multiple(
 | 
			
		||||
            url="codes-compensation-action-autocomplete",
 | 
			
		||||
            attrs={
 | 
			
		||||
                "data-placeholder": _("Action"),
 | 
			
		||||
@ -496,7 +496,7 @@ class EditCompensationActionModalForm(NewActionModalForm):
 | 
			
		||||
        self.action = kwargs.pop("action", None)
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        form_data = {
 | 
			
		||||
            "action_type": self.action.action_type,
 | 
			
		||||
            "action_type": self.action.action_type.all(),
 | 
			
		||||
            "action_type_details": self.action.action_type_details.all(),
 | 
			
		||||
            "amount": self.action.amount,
 | 
			
		||||
            "unit": self.action.unit,
 | 
			
		||||
@ -506,7 +506,7 @@ class EditCompensationActionModalForm(NewActionModalForm):
 | 
			
		||||
 | 
			
		||||
    def save(self):
 | 
			
		||||
        action = self.action
 | 
			
		||||
        action.action_type = self.cleaned_data.get("action_type", None)
 | 
			
		||||
        action.action_type.set(self.cleaned_data.get("action_type", []))
 | 
			
		||||
        action.action_type_details.set(self.cleaned_data.get("action_type_details", []))
 | 
			
		||||
        action.amount = self.cleaned_data.get("amount", None)
 | 
			
		||||
        action.unit = self.cleaned_data.get("unit", None)
 | 
			
		||||
 | 
			
		||||
@ -8,17 +8,6 @@ Created on: 14.10.21
 | 
			
		||||
from django.db import models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CompensationActionManager(models.Manager):
 | 
			
		||||
    """ Holds default db fetch setting for this model type
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    def get_queryset(self):
 | 
			
		||||
        return super().get_queryset().select_related(
 | 
			
		||||
            "action_type",
 | 
			
		||||
            "action_type__parent"
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CompensationStateManager(models.Manager):
 | 
			
		||||
    """ Holds default db fetch setting for this model type
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										39
									
								
								compensation/migrations/0004_auto_20220210_1402.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								compensation/migrations/0004_auto_20220210_1402.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
			
		||||
# Generated by Django 3.1.3 on 2022-02-10 13:02
 | 
			
		||||
 | 
			
		||||
from django.db import migrations, models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def migrate_actions(apps, schema_editor):
 | 
			
		||||
    CompensationAction = apps.get_model('compensation', 'CompensationAction')
 | 
			
		||||
    actions = CompensationAction.objects.all()
 | 
			
		||||
 | 
			
		||||
    for action in actions:
 | 
			
		||||
        action_type = action.action_type or []
 | 
			
		||||
        action.action_type_tmp.set(action_type)
 | 
			
		||||
        action.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('codelist', '0001_initial'),
 | 
			
		||||
        ('compensation', '0003_auto_20220202_0846'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.AddField(
 | 
			
		||||
            model_name='compensationaction',
 | 
			
		||||
            name='action_type_tmp',
 | 
			
		||||
            field=models.ManyToManyField(blank=True, limit_choices_to={'code_lists__in': [1026], 'is_archived': False, 'is_selectable': True}, related_name='_compensationaction_action_type_+', to='codelist.KonovaCode'),
 | 
			
		||||
        ),
 | 
			
		||||
        migrations.RunPython(migrate_actions),
 | 
			
		||||
        migrations.RemoveField(
 | 
			
		||||
            model_name='compensationaction',
 | 
			
		||||
            name='action_type',
 | 
			
		||||
        ),
 | 
			
		||||
        migrations.RenameField(
 | 
			
		||||
            model_name='compensationaction',
 | 
			
		||||
            old_name='action_type_tmp',
 | 
			
		||||
            new_name='action_type',
 | 
			
		||||
        )
 | 
			
		||||
    ]
 | 
			
		||||
@ -10,9 +10,7 @@ from django.utils.translation import gettext_lazy as _
 | 
			
		||||
 | 
			
		||||
from codelist.models import KonovaCode
 | 
			
		||||
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_COMPENSATION_ACTION_DETAIL_ID
 | 
			
		||||
from compensation.managers import CompensationActionManager
 | 
			
		||||
from konova.models import BaseResource
 | 
			
		||||
from konova.utils.message_templates import COMPENSATION_ACTION_REMOVED
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class UnitChoices(models.TextChoices):
 | 
			
		||||
@ -31,10 +29,8 @@ class CompensationAction(BaseResource):
 | 
			
		||||
    """
 | 
			
		||||
    Compensations include actions like planting trees, refreshing rivers and so on.
 | 
			
		||||
    """
 | 
			
		||||
    action_type = models.ForeignKey(
 | 
			
		||||
    action_type = models.ManyToManyField(
 | 
			
		||||
        KonovaCode,
 | 
			
		||||
        on_delete=models.SET_NULL,
 | 
			
		||||
        null=True,
 | 
			
		||||
        blank=True,
 | 
			
		||||
        limit_choices_to={
 | 
			
		||||
            "code_lists__in": [CODELIST_COMPENSATION_ACTION_ID],
 | 
			
		||||
@ -57,10 +53,8 @@ class CompensationAction(BaseResource):
 | 
			
		||||
    unit = models.CharField(max_length=100, null=True, blank=True, choices=UnitChoices.choices)
 | 
			
		||||
    comment = models.TextField(blank=True, null=True, help_text="Additional comment")
 | 
			
		||||
 | 
			
		||||
    objects = CompensationActionManager()
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return f"{self.action_type} | {self.amount} {self.unit}"
 | 
			
		||||
        return f"{self.action_type.all()} | {self.amount} {self.unit}"
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def unit_humanize(self):
 | 
			
		||||
 | 
			
		||||
@ -104,12 +104,12 @@ class AbstractCompensation(BaseObject, GeoReferencedMixin):
 | 
			
		||||
        with transaction.atomic():
 | 
			
		||||
            user_action = UserActionLogEntry.get_created_action(user)
 | 
			
		||||
            comp_action = CompensationAction.objects.create(
 | 
			
		||||
                action_type=form_data["action_type"],
 | 
			
		||||
                amount=form_data["amount"],
 | 
			
		||||
                unit=form_data["unit"],
 | 
			
		||||
                comment=form_data["comment"],
 | 
			
		||||
                created=user_action,
 | 
			
		||||
            )
 | 
			
		||||
            comp_action.action_type.set(form_data.get("action_type", []))
 | 
			
		||||
            comp_action_details = form_data["action_type_details"]
 | 
			
		||||
            comp_action.action_type_details.set(comp_action_details)
 | 
			
		||||
            self.actions.add(comp_action)
 | 
			
		||||
 | 
			
		||||
@ -47,7 +47,11 @@
 | 
			
		||||
            {% for action in actions %}
 | 
			
		||||
            <tr>
 | 
			
		||||
                <td class="">
 | 
			
		||||
                    <span>{{ action.action_type }}</span>
 | 
			
		||||
                    <ul>
 | 
			
		||||
                        {% for type in action.action_type.all %}
 | 
			
		||||
                        <li> {{type}} </li>
 | 
			
		||||
                        {% endfor %}
 | 
			
		||||
                    </ul>
 | 
			
		||||
                    {% if action.action_type_details.count > 0 %}
 | 
			
		||||
                        <br>
 | 
			
		||||
                        {% for detail in action.action_type_details.all %}
 | 
			
		||||
 | 
			
		||||
@ -46,7 +46,11 @@
 | 
			
		||||
            {% for action in actions %}
 | 
			
		||||
            <tr>
 | 
			
		||||
                <td class="">
 | 
			
		||||
                    <span>{{ action.action_type }}</span>
 | 
			
		||||
                    <ul>
 | 
			
		||||
                        {% for type in action.action_type.all%}
 | 
			
		||||
                        <li> {{type.long_name}} </li>
 | 
			
		||||
                        {% endfor %}
 | 
			
		||||
                    </ul>
 | 
			
		||||
                    {% if action.action_type_details.count > 0 %}
 | 
			
		||||
                        <br>
 | 
			
		||||
                        {% for detail in action.action_type_details.all %}
 | 
			
		||||
 | 
			
		||||
@ -44,7 +44,11 @@
 | 
			
		||||
            {% for action in obj.actions.all %}
 | 
			
		||||
            <tr>
 | 
			
		||||
                <td class="">
 | 
			
		||||
                    <span>{{ action.action_type }}</span>
 | 
			
		||||
                    <ul>
 | 
			
		||||
                        {% for type in action.action_type.all%}
 | 
			
		||||
                        <li> {{type}} </li>
 | 
			
		||||
                        {% endfor %}
 | 
			
		||||
                    </ul>
 | 
			
		||||
                    {% if action.action_type_details.count > 0 %}
 | 
			
		||||
                        <br>
 | 
			
		||||
                        {% for detail in action.action_type_details.all %}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user