Skip to content

Commit

Permalink
feat(Structures): Nouveau modèle SiaeActivity (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Jun 13, 2024
1 parent 8d5c5e7 commit e458851
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 16 deletions.
103 changes: 91 additions & 12 deletions lemarche/siaes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

from lemarche.conversations.models import Conversation
from lemarche.labels.models import Label
from lemarche.networks.models import Network
from lemarche.notes.models import Note
from lemarche.siaes import constants as siae_constants
from lemarche.siaes.models import (
Siae,
SiaeActivity,
SiaeClientReference,
SiaeGroup,
SiaeImage,
Expand Down Expand Up @@ -66,6 +68,26 @@ def queryset(self, request, queryset):
return queryset


class SiaeActivityInline(admin.TabularInline):
model = SiaeActivity
fields = [
"sector_group",
"sectors",
"presta_type",
"location",
"geo_range",
"geo_range_custom_distance",
"created_at",
]
show_change_link = True
can_delete = False
extra = 0
max_num = 0

def has_change_permission(self, request, obj=None):
return False


class SiaeLabelInline(admin.TabularInline):
model = SiaeLabel
fields = ["label", "label_with_link", "created_at", "updated_at"]
Expand Down Expand Up @@ -107,12 +129,11 @@ def user_with_link(self, siae_user):

class ConversationsInline(admin.TabularInline):
model = Conversation
can_delete = False

fields = ["id", "title_with_link", "nb_message_with_link", "kind", "created_at"]

# autocomplete_fields = ["user"]
readonly_fields = ["id", "title_with_link", "nb_message_with_link", "kind", "created_at"]
show_change_link = True
can_delete = False
extra = 0
max_num = 0

Expand Down Expand Up @@ -144,6 +165,7 @@ class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin, SimpleHistoryAdmin)
"tender_detail_display_count_annotated_with_link",
"tender_detail_contact_click_count_annotated_with_link",
"tender_detail_not_interested_count_annotated_with_link",
"activity_count_with_link",
"offer_count_with_link",
"label_count_with_link",
"client_reference_count_with_link",
Expand Down Expand Up @@ -175,6 +197,7 @@ class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin, SimpleHistoryAdmin)
"sector_count_with_link",
"network_count_with_link",
"group_count_with_link",
"activity_count_with_link",
"offer_count_with_link",
"label_count_with_link",
"client_reference_count_with_link",
Expand Down Expand Up @@ -253,10 +276,7 @@ class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin, SimpleHistoryAdmin)
"description",
"sectors",
"sector_count_with_link",
"networks",
"network_count_with_link",
"groups",
"group_count_with_link",
"activity_count_with_link",
"offer_count_with_link",
"label_count_with_link",
"client_reference_count_with_link",
Expand All @@ -265,6 +285,7 @@ class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin, SimpleHistoryAdmin)
)
},
),
SiaeActivityInline,
SiaeLabelInline,
(
"Périmètre d'intervention",
Expand All @@ -289,6 +310,17 @@ class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin, SimpleHistoryAdmin)
)
},
),
(
"Réseaux & Groupements",
{
"fields": (
"networks",
"network_count_with_link",
"groups",
"group_count_with_link",
)
},
),
SiaeNoteInline,
ConversationsInline,
SiaeUserInline,
Expand Down Expand Up @@ -488,7 +520,7 @@ def user_count_with_link(self, siae):
url = reverse("admin:users_user_changelist") + f"?siaes__in={siae.id}"
return format_html(f'<a href="{url}">{siae.user_count}</a>')

user_count_with_link.short_description = "Utilisateurs"
user_count_with_link.short_description = User._meta.verbose_name_plural
user_count_with_link.admin_order_field = "user_count"

def sector_count_with_link(self, siae):
Expand All @@ -502,21 +534,28 @@ def network_count_with_link(self, siae):
url = reverse("admin:networks_network_changelist") + f"?siaes__in={siae.id}"
return format_html(f'<a href="{url}">{siae.network_count}</a>')

network_count_with_link.short_description = "Réseaux"
network_count_with_link.short_description = Network._meta.verbose_name_plural
network_count_with_link.admin_order_field = "network_count"

def group_count_with_link(self, siae):
url = reverse("admin:siaes_siaegroup_changelist") + f"?siaes__in={siae.id}"
return format_html(f'<a href="{url}">{siae.group_count}</a>')

group_count_with_link.short_description = "Groupements"
group_count_with_link.short_description = SiaeGroup._meta.verbose_name_plural
group_count_with_link.admin_order_field = "group_count"

def activity_count_with_link(self, siae):
url = reverse("admin:siaes_siaeactivity_changelist") + f"?siae__id__exact={siae.id}"
return format_html(f'<a href="{url}">{siae.offer_count}</a>')

activity_count_with_link.short_description = SiaeActivity._meta.verbose_name_plural
activity_count_with_link.admin_order_field = "offer_count"

def offer_count_with_link(self, siae):
url = reverse("admin:siaes_siaeoffer_changelist") + f"?siae__id__exact={siae.id}"
return format_html(f'<a href="{url}">{siae.offer_count}</a>')

offer_count_with_link.short_description = "Prestations"
offer_count_with_link.short_description = SiaeOffer._meta.verbose_name_plural
offer_count_with_link.admin_order_field = "offer_count"

def label_count_with_link(self, siae):
Expand All @@ -537,7 +576,7 @@ def image_count_with_link(self, siae):
url = reverse("admin:siaes_siaeimage_changelist") + f"?siae__id__exact={siae.id}"
return format_html(f'<a href="{url}">{siae.image_count}</a>')

image_count_with_link.short_description = "Images"
image_count_with_link.short_description = SiaeImage._meta.verbose_name_plural
image_count_with_link.admin_order_field = "image_count"

def coords_display(self, siae):
Expand Down Expand Up @@ -678,6 +717,46 @@ def has_delete_permission(self, request, obj=None):
return False


@admin.register(SiaeActivity, site=admin_site)
class SiaeActivityAdmin(admin.ModelAdmin):
list_display = ["id", "siae_with_link", "sector_group", "created_at"]
list_filter = ["sectors"]
search_fields = ["id", "siae__id", "siae__name"]
search_help_text = "Cherche sur les champs : ID, Structure (ID, Nom)"

autocomplete_fields = ["siae", "sectors", "location"]
readonly_fields = ["created_at", "updated_at"]

fieldsets = (
(
"Structure",
{
"fields": ("siae",),
},
),
(
"Prestation",
{
"fields": ("sector_group", "sectors", "presta_type"),
},
),
(
"Localisation et périmètre d'intervention",
{
"fields": ("location", "geo_range", "geo_range_custom_distance"),
},
),
("Dates", {"fields": ("created_at", "updated_at")}),
)

def siae_with_link(self, siae_offer):
url = reverse("admin:siaes_siae_change", args=[siae_offer.siae_id])
return format_html(f'<a href="{url}">{siae_offer.siae}</a>')

siae_with_link.short_description = Siae._meta.verbose_name
siae_with_link.admin_order_field = "siae"


@admin.register(SiaeOffer, site=admin_site)
class SiaeOfferAdmin(admin.ModelAdmin):
list_display = ["id", "name", "siae_with_link", "source", "created_at"]
Expand Down
22 changes: 21 additions & 1 deletion lemarche/siaes/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
from factory.django import DjangoModelFactory

from lemarche.siaes import constants as siae_constants
from lemarche.siaes.models import Siae, SiaeClientReference, SiaeGroup, SiaeImage, SiaeLabelOld, SiaeOffer
from lemarche.siaes.models import (
Siae,
SiaeActivity,
SiaeClientReference,
SiaeGroup,
SiaeImage,
SiaeLabelOld,
SiaeOffer,
)


class SiaeGroupFactory(DjangoModelFactory):
Expand Down Expand Up @@ -57,6 +65,18 @@ def networks(self, create, extracted, **kwargs):
self.networks.add(*extracted)


class SiaeActivityFactory(DjangoModelFactory):
class Meta:
model = SiaeActivity

presta_type = factory.List([factory.fuzzy.FuzzyChoice([key for (key, value) in siae_constants.PRESTA_CHOICES])])

@factory.post_generation
def sectors(self, create, extracted, **kwargs):
if extracted:
self.sectors.add(*extracted)


class SiaeOfferFactory(DjangoModelFactory):
class Meta:
model = SiaeOffer
Expand Down
112 changes: 112 additions & 0 deletions lemarche/siaes/migrations/0076_siaeactivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Generated by Django 4.2.13 on 2024-06-10 15:00

import django.db.models.deletion
import django.utils.timezone
from django.db import migrations, models

import lemarche.utils.fields


class Migration(migrations.Migration):
dependencies = [
("sectors", "0003_sector_sectorgroup_ordering"),
("perimeters", "0005_alter_perimeter_post_codes"),
("siaes", "0075_historicalsiae"),
]

operations = [
migrations.CreateModel(
name="SiaeActivity",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
(
"presta_type",
lemarche.utils.fields.ChoiceArrayField(
base_field=models.CharField(
choices=[
("DISP", "Mise à disposition - Interim"),
("PREST", "Prestation de service"),
("BUILD", "Fabrication et commercialisation de biens"),
],
max_length=20,
),
blank=True,
db_index=True,
null=True,
size=None,
verbose_name="Type de prestation",
),
),
(
"geo_range",
models.CharField(
blank=True,
choices=[
("COUNTRY", "France entière"),
("REGION", "Région"),
("DEPARTMENT", "Département"),
("CUSTOM", "Distance en kilomètres"),
],
db_index=True,
max_length=20,
verbose_name="Périmètre d'intervention",
),
),
(
"geo_range_custom_distance",
models.IntegerField(
blank=True, null=True, verbose_name="Distance en kilomètres (périmètre d'intervention)"
),
),
(
"created_at",
models.DateTimeField(default=django.utils.timezone.now, verbose_name="Date de création"),
),
("updated_at", models.DateTimeField(auto_now=True, verbose_name="Date de modification")),
(
"sectors",
models.ManyToManyField(
blank=True,
related_name="siae_activities",
to="sectors.sector",
verbose_name="Activités",
),
),
(
"siae",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="activities",
to="siaes.siae",
verbose_name="Structure",
),
),
(
"sector_group",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="siae_activities",
to="sectors.sectorgroup",
verbose_name="Secteur d'activité",
),
),
(
"location",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="siae_activities",
to="perimeters.perimeter",
verbose_name="Localisation",
),
),
],
options={
"verbose_name": "Activité",
"verbose_name_plural": "Activités",
"ordering": ["-created_at"],
},
),
]
Loading

0 comments on commit e458851

Please sign in to comment.