Skip to content

Commit

Permalink
Ajout d'un export spécifique à Koumoul (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienheureux authored Dec 17, 2024
1 parent 5c9d77e commit 91f23b7
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion qfdmd/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from django_extensions.db.fields import ImproperlyConfigured
from import_export import resources
from import_export.admin import ImportExportModelAdmin

Expand All @@ -10,9 +11,75 @@ class Meta:
model = Lien


class KoumoulModelResource(resources.ModelResource):
"""Base class to map Django model fields to Koumoul columns for export."""

koumoul_mapping: dict[str, str] = {}

def get_export_fields(self):
"""Update column names based on koumoul_mapping."""
fields = super().get_export_fields()
for field in fields:
field.column_name = self.koumoul_mapping.get(
field.attribute, field.column_name
)
return fields

def __init__(self, *args, **kwargs):
"""Validate koumoul_mapping and ensure field names are valid."""
super().__init__(*args, **kwargs)

if not self.koumoul_mapping:
error_message = (
"The mapping between Django model fields and Koumoul fields"
" is not defined."
)
raise ImproperlyConfigured(error_message)

model_fields = {field.name for field in self._meta.model._meta.get_fields()}
invalid_fields = [
field for field in self.koumoul_mapping if field not in model_fields
]

if invalid_fields:
error_message = (
"The following fields in koumoul_mapping are not part"
f" of the model: {', '.join(invalid_fields)}"
)
raise ImproperlyConfigured(error_message)

# Remove fields not in koumoul_mapping
self.fields = {
field: self.fields[field]
for field in self.fields
if field in self.koumoul_mapping
}


class KoumoulProduitResource(KoumoulModelResource):
koumoul_mapping = {
"id": "ID",
"nom": "Nom",
"synonymes_existants": "Synonymes_existants",
"code": "Code",
"bdd": "Bdd",
"comment_les_eviter": "Comment_les_eviter_?",
"qu_est_ce_que_j_en_fais": "Qu'est-ce_que_j'en_fais_?",
"que_va_t_il_devenir": "Que_va-t-il_devenir_?",
"nom_eco_organisme": "nom_eco_organisme",
"filieres_rep": "filieres_REP",
"slug": "Slug",
}

class Meta:
model = Produit
name = "Import/export des champs Koumoul uniquement"


class ProduitResource(resources.ModelResource):
class Meta:
model = Produit
name = "Import/export de tous les champs Produit"


class SynonymeResource(resources.ModelResource):
Expand Down Expand Up @@ -42,7 +109,7 @@ class SuggestionAdmin(admin.ModelAdmin):

@admin.register(Produit)
class ProduitAdmin(ImportExportModelAdmin, admin.ModelAdmin):
resource_class = ProduitResource
resource_classes = [ProduitResource, KoumoulProduitResource]
list_display = ("nom", "id", "synonymes_existants")
search_fields = ["nom__unaccent", "id", "synonymes_existants__unaccent"]
# ajout des filtres de recherche sur bdd et code
Expand Down

0 comments on commit 91f23b7

Please sign in to comment.