Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPR] Add contractor to intervention model (#3820) #3928

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CHANGELOG
- Add missing translations for fields on ``Courses`` and ``Sites`` in APIv2 (#3569)
- Allow Apidae Trek parser to handle traces not in utf-8
- Add workforce cost into intervention model (#3824)
- Add contractor to intervention model (#3820)

2.101.5 (2024-01-11)
--------------------
Expand Down
14 changes: 11 additions & 3 deletions geotrek/maintenance/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.conf import settings
from django.contrib.gis.geos import GeometryCollection
from django.utils.translation import gettext_lazy as _
from django_filters import ChoiceFilter, MultipleChoiceFilter, DateFromToRangeFilter
from django_filters import ChoiceFilter, MultipleChoiceFilter, DateFromToRangeFilter, ModelMultipleChoiceFilter

from mapentity.filters import PolygonFilter, PythonPolygonFilter

Expand All @@ -15,7 +15,7 @@
ZoningFilterSet)
from geotrek.zoning.models import City, District, RestrictedArea, RestrictedAreaType

from .models import Intervention, Project
from .models import Intervention, Project, Contractor


class BboxInterventionFilterMixin:
Expand Down Expand Up @@ -153,7 +153,7 @@ class InterventionFilterSet(AltimetryInterventionFilterSet, ZoningFilterSet, Str
class Meta(StructureRelatedFilterSet.Meta):
model = Intervention
fields = StructureRelatedFilterSet.Meta.fields + [
'status', 'type', 'stake', 'subcontracting', 'project', 'on',
'status', 'type', 'stake', 'subcontracting', 'project', 'contractors', 'on',
]

def filter_year(self, qs, name, values):
Expand All @@ -176,6 +176,7 @@ class ProjectFilterSet(StructureRelatedFilterSet):
district = ProjectIntersectionFilterDistrict(label=_('District'), lookup_expr='intersects', required=False)
area_type = ProjectIntersectionFilterRestrictedAreaType(label=_('Restricted area type'), lookup_expr='intersects', required=False)
area = ProjectIntersectionFilterRestrictedArea(label=_('Restricted area'), lookup_expr='intersects', required=False)
contractors = ModelMultipleChoiceFilter(label=_("Intervention contractors"), queryset=Contractor.objects.all(), method='filter_contractors')

class Meta(StructureRelatedFilterSet.Meta):
model = Project
Expand All @@ -184,6 +185,13 @@ class Meta(StructureRelatedFilterSet.Meta):
'project_manager', 'founders'
]

def filter_contractors(self, qs, name, values):
q = Q()
if values:
q |= Q(contractors__in=values)
q |= Q(interventions__contractors__in=values)
return qs.filter(q)

def filter_year(self, qs, name, values):
q = Q()
for value in values:
Expand Down
7 changes: 3 additions & 4 deletions geotrek/maintenance/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class InterventionForm(CommonForm):
'height',
'stake',
'project',
'contractors',
'access',
'description',
'material_cost',
Expand All @@ -105,7 +106,7 @@ class Meta(CommonForm.Meta):
model = Intervention
fields = CommonForm.Meta.fields + \
['structure', 'name', 'begin_date', 'end_date', 'status', 'disorders', 'type', 'description', 'subcontracting', 'length', 'width',
'height', 'stake', 'project', 'access', 'material_cost', 'heliport_cost', 'contractor_cost', 'workforce_cost', 'topology']
'height', 'stake', 'project', 'contractors', 'access', 'material_cost', 'heliport_cost', 'contractor_cost', 'workforce_cost', 'topology']

def __init__(self, *args, target_type=None, target_id=None, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -205,9 +206,7 @@ class ProjectForm(CommonForm):
'constraint',
'global_cost',
'comments',

css_class="span6"),
Div('project_owner',
'project_owner',
'project_manager',
'contractors',
Fieldset(_("Fundings")),
Expand Down
9 changes: 6 additions & 3 deletions geotrek/maintenance/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ msgstr ""
msgid "Workforce cost"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "3D Length"
msgstr ""

Expand Down Expand Up @@ -272,9 +275,6 @@ msgstr ""
msgid "Domain"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "Project owner"
msgstr ""

Expand All @@ -287,6 +287,9 @@ msgstr ""
msgid "Projects"
msgstr ""

msgid "Intervention contractors"
msgstr ""

msgid "Period"
msgstr ""

Expand Down
9 changes: 6 additions & 3 deletions geotrek/maintenance/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ msgstr ""
msgid "Subcontract cost"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "3D Length"
msgstr ""

Expand Down Expand Up @@ -269,9 +272,6 @@ msgstr ""
msgid "Domain"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "Project owner"
msgstr ""

Expand All @@ -284,6 +284,9 @@ msgstr ""
msgid "Projects"
msgstr ""

msgid "Intervention contractors"
msgstr ""

msgid "Period"
msgstr ""

Expand Down
9 changes: 6 additions & 3 deletions geotrek/maintenance/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ msgstr ""
msgid "Subcontract cost"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "3D Length"
msgstr ""

Expand Down Expand Up @@ -269,9 +272,6 @@ msgstr ""
msgid "Domain"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "Project owner"
msgstr ""

Expand All @@ -284,6 +284,9 @@ msgstr ""
msgid "Projects"
msgstr ""

msgid "Intervention contractors"
msgstr ""

msgid "Period"
msgstr ""

Expand Down
9 changes: 6 additions & 3 deletions geotrek/maintenance/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ msgstr "Coût héliportage"
msgid "Subcontract cost"
msgstr "Coût sous-traitance"

msgid "Contractors"
msgstr "Prestataires"

msgid "3D Length"
msgstr "Longueur 3D"

Expand Down Expand Up @@ -271,9 +274,6 @@ msgstr "Commentaires"
msgid "Domain"
msgstr "Domaine"

msgid "Contractors"
msgstr "Prestataires"

msgid "Project owner"
msgstr "Maître d'ouvrage"

Expand All @@ -286,6 +286,9 @@ msgstr "Financeurs"
msgid "Projects"
msgstr "Chantiers"

msgid "Intervention contractors"
msgstr "Prestataires des interventions"

msgid "Period"
msgstr "Période"

Expand Down
9 changes: 6 additions & 3 deletions geotrek/maintenance/locale/it/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ msgstr ""
msgid "Subcontract cost"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "3D Length"
msgstr ""

Expand Down Expand Up @@ -269,9 +272,6 @@ msgstr ""
msgid "Domain"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "Project owner"
msgstr ""

Expand All @@ -284,6 +284,9 @@ msgstr ""
msgid "Projects"
msgstr ""

msgid "Intervention contractors"
msgstr ""

msgid "Period"
msgstr ""

Expand Down
9 changes: 6 additions & 3 deletions geotrek/maintenance/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ msgstr ""
msgid "Subcontract cost"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "3D Length"
msgstr ""

Expand Down Expand Up @@ -269,9 +272,6 @@ msgstr ""
msgid "Domain"
msgstr ""

msgid "Contractors"
msgstr ""

msgid "Project owner"
msgstr ""

Expand All @@ -284,6 +284,9 @@ msgstr ""
msgid "Projects"
msgstr ""

msgid "Intervention contractors"
msgstr ""

msgid "Period"
msgstr ""

Expand Down
18 changes: 18 additions & 0 deletions geotrek/maintenance/migrations/0028_intervention_contractors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.23 on 2024-02-05 09:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('maintenance', '0027_intervention_workforce_cost'),
]

operations = [
migrations.AddField(
model_name='intervention',
name='contractors',
field=models.ManyToManyField(blank=True, related_name='interventions', to='maintenance.Contractor', verbose_name='Contractors'),
),
]
13 changes: 10 additions & 3 deletions geotrek/maintenance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from geotrek.zoning.mixins import ZoningPropertiesMixin

from mapentity.models import DuplicateMixin

from django.contrib.postgres.aggregates import ArrayAgg

if 'geotrek.signage' in settings.INSTALLED_APPS:
from geotrek.signage.models import Blade
Expand Down Expand Up @@ -51,6 +51,8 @@ class Intervention(ZoningPropertiesMixin, AddPropertyMixin, GeotrekMapEntityMixi
heliport_cost = models.FloatField(default=0.0, blank=True, null=True, verbose_name=_("Heliport cost"))
contractor_cost = models.FloatField(default=0.0, blank=True, null=True, verbose_name=_("Contractor cost"))
workforce_cost = models.FloatField(default=0.0, blank=True, null=True, verbose_name=_("Workforce cost"))
contractors = models.ManyToManyField('Contractor', related_name="interventions", blank=True,
verbose_name=_("Contractors"))

# AltimetryMixin for denormalized fields from related topology, updated via trigger.
length = models.FloatField(editable=True, default=0.0, null=True, blank=True, verbose_name=_("3D Length"))
Expand Down Expand Up @@ -551,8 +553,13 @@ def interventions_csv_display(self):
return [str(i) for i in self.interventions.existing()]

@property
def contractors_display(self):
return [str(c) for c in self.contractors.all()]
def intervention_contractors(self):
return self.interventions.aggregate(
intervention_contractors=ArrayAgg('contractors__contractor', distinct=True))['intervention_contractors']

@classproperty
def intervention_contractors_verbose_name(cls):
return _("Intervention contractors")

@property
def founders_display(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
{% else %} <span class="none">{% trans "None" %}</span>
{% endif %}
</td>
</tr>
<tr>
<th>{{ intervention|verbose:"contractors" }}</th>
<td>
{% valuelist intervention.contractors.all %}
</td>
</tr>
<tr>
<th>{% trans "Related object" %}</th>
Expand Down
6 changes: 6 additions & 0 deletions geotrek/maintenance/templates/maintenance/project_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
{% valuelist project.contractors.all %}
</td>
</tr>
<tr>
<th>{{ project|verbose:"intervention_contractors" }}</th>
<td>
{% valuelist project.intervention_contractors %}
</td>
</tr>
<tr>
<th>{% trans "Fundings" %}</th>
<td>
Expand Down
25 changes: 21 additions & 4 deletions geotrek/maintenance/templates/maintenance/sql/post_20_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SELECT a.id,
j.nb_days AS "Cost",
(a.material_cost+a.heliport_cost+a.contractor_cost+a.workforce_cost)::float AS "Total cost",
h.name AS "Project",
m.contractor AS "Contractors",
CASE
WHEN k.app_label = 'core' THEN 'Tronçons'
WHEN k.app_label = 'infrastructure' THEN 'Aménagements'
Expand All @@ -39,6 +40,13 @@ SELECT a.id,
a.date_update AS "Update date",
a.geom_3d
FROM maintenance_intervention a
LEFT JOIN
(SELECT array_to_string(array_agg(a.contractor), ', '::text, '_'::text) contractor,
intervention_id
FROM maintenance_contractor a
JOIN maintenance_intervention_contractors b ON a.id = b.contractor_id
JOIN maintenance_intervention c ON b.intervention_id = c.id
GROUP BY intervention_id) m ON a.id = m.intervention_id
LEFT JOIN maintenance_interventiontype b ON a.type_id = b.id
LEFT JOIN maintenance_interventionstatus c ON a.status_id = c.id
LEFT JOIN core_stake d ON a.stake_id = d.id
Expand Down Expand Up @@ -100,6 +108,7 @@ SELECT a.id,
h.organism AS "Project owner",
j.organism AS "Project manager",
k.contractor AS "Contractors",
l.contractor AS "Intervention contractors",
i.financement AS "Fundings",
a.comments AS "Comments",
a.date_insert AS "Insertion date",
Expand Down Expand Up @@ -128,11 +137,19 @@ LEFT JOIN
FROM financements
GROUP BY project_id) i ON a.id = i.project_id
LEFT JOIN
(SELECT a.contractor,
b.contractor_id,
(SELECT array_to_string(array_agg(a.contractor), ', '::text, '_'::text) contractor,
project_id
FROM maintenance_contractor a
JOIN maintenance_project_contractors b ON a.id = b.contractor_id
JOIN maintenance_project c ON b.project_id = c.id) k ON a.id = k.project_id
WHERE a.deleted IS FALSE
JOIN maintenance_project c ON b.project_id = c.id
GROUP BY project_id) k ON a.id = k.project_id
LEFT JOIN maintenance_intervention m ON a.id = m.project_id
LEFT JOIN
(SELECT array_to_string(array_agg(a.contractor), ', '::text, '_'::text) contractor,
intervention_id
FROM maintenance_contractor a
JOIN maintenance_intervention_contractors b ON a.id = b.contractor_id
JOIN maintenance_intervention c ON b.intervention_id = c.id
GROUP BY intervention_id) l ON m.id = l.intervention_id
WHERE a.deleted IS FALSE
;
Loading
Loading