Skip to content

Commit

Permalink
WIP 3
Browse files Browse the repository at this point in the history
  • Loading branch information
alanzirek committed Sep 19, 2024
1 parent a861065 commit 4058fef
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 31 deletions.
16 changes: 9 additions & 7 deletions sv/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from core.forms import DSFRForm, WithNextUrlMixin

from django.contrib.contenttypes.models import ContentType

from core.fields import MultiModelChoiceField
from django import forms

Expand Down Expand Up @@ -53,12 +52,15 @@ def __init__(self, *args, **kwargs):
content_type = ContentType.objects.get_for_model(FicheDetection)
self.fields["content_type"].initial = content_type
self.fields["object_id"].initial = fiche_detection_pk
self.fields["visibilite"].queryset = Visibilite.objects.get_visibilites_for_user(self.user)
self.fields["visibilite"].initial = (
VisibiliteFiche.objects.select_related("visibilite")
.get(content_type_id=content_type.id, object_id=fiche_detection_pk)
.visibilite
)

fiche_detection_visibilite = VisibiliteFiche.objects.get_visibilite_for_fiche_detection(fiche_detection_pk)
match fiche_detection_visibilite.libelle:
case Visibilite.BROUILLON:
self.fields["visibilite"].queryset = Visibilite.objects.filter(libelle=Visibilite.LOCAL)
self.fields["visibilite"].initial = Visibilite.objects.get(libelle=Visibilite.LOCAL)
case Visibilite.LOCAL | Visibilite.NATIONAL:
self.fields["visibilite"].queryset = Visibilite.objects.exclude(libelle=Visibilite.BROUILLON)
self.fields["visibilite"].initial = fiche_detection_visibilite

def save(self, commit=True):
content_type = self.cleaned_data["content_type"]
Expand Down
16 changes: 8 additions & 8 deletions sv/managers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType


class FicheDetectionManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_deleted=False)


class VisibiliteManager(models.Manager):
def get_visibilites_for_user(self, user):
from sv.models import Visibilite
class VisibiliteFicheManager(models.Manager):
def get_visibilite_for_fiche_detection(self, fiche_detection_id):
from sv.models import FicheDetection

libelles = (
Visibilite.VISIBILITES_ADMINISTRATION_CENTRALE
if user.agent.structure.is_mus_or_bsv
else Visibilite.VISIBILITES_SERVICES_DECONCENTRES
return (
self.select_related("visibilite")
.get(content_type=ContentType.objects.get_for_model(FicheDetection), object_id=fiche_detection_id)
.visibilite
)
return self.filter(libelle__in=libelles)
6 changes: 2 additions & 4 deletions sv/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from core.mixins import AllowsSoftDeleteMixin
from core.models import Document, Message, Contact, Structure, FinSuiviContact
from sv.managers import FicheDetectionManager, VisibiliteManager
from sv.managers import FicheDetectionManager, VisibiliteFicheManager


class NumeroFiche(models.Model):
Expand Down Expand Up @@ -384,15 +384,12 @@ class Visibilite(models.Model):
LOCAL: "Seul votre structure et l'administration centrale pourront consulter et modifier la fiche",
NATIONAL: "La fiche sera et modifiable par toutes les structures",
}
VISIBILITES_ADMINISTRATION_CENTRALE = [LOCAL, NATIONAL]
VISIBILITES_SERVICES_DECONCENTRES = [BROUILLON, LOCAL]

class Meta:
verbose_name = "Visibilité"
verbose_name_plural = "Visibilités"

libelle = models.CharField(max_length=50, verbose_name="Libellé")
objects = VisibiliteManager()

def __str__(self):
return self.libelle
Expand All @@ -413,6 +410,7 @@ class Meta:
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")
visibilite = models.ForeignKey(Visibilite, on_delete=models.PROTECT)
objects = VisibiliteFicheManager()

def __str__(self):
return f"{self.content_object} ({self.visibilite})"
Expand Down
2 changes: 1 addition & 1 deletion sv/templates/sv/fichedetection_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1 class="fiche-header__title">Fiche détection n° {{ fichedetection.numero }}
{% include "sv/_lienlibre_modal.html" %}
{% include "sv/_delete_fiche_modal.html" %}
{% if can_update_visibilite %}
{% include "sv/_fichedetection_edit_visibilite_modal.html" %}
{% include "sv/_fichedetection_update_visibilite_modal.html" %}
{% endif %}
</div>
</div>
Expand Down
25 changes: 14 additions & 11 deletions sv/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,14 @@ def _get_free_link_form(self):
def _can_update_visibilite(self, user):
"""Vérifie si l'utilisateur peut modifier la visibilité de la fiche de détection."""
fiche_detection = self.get_object()
visibilite_libelle = fiche_detection.visibilite.first().visibilite.libelle
return (
user.agent.structure == fiche_detection.createur
and visibilite_libelle in Visibilite.VISIBILITES_SERVICES_DECONCENTRES
) or (
user.agent.structure.is_mus_or_bsv and visibilite_libelle in Visibilite.VISIBILITES_ADMINISTRATION_CENTRALE
)
fiche_detection_visibilite = VisibiliteFiche.objects.get_visibilite_for_fiche_detection(fiche_detection.pk)
match fiche_detection_visibilite.libelle:
case Visibilite.BROUILLON:
return user.agent.structure == fiche_detection.createur
case Visibilite.LOCAL | Visibilite.NATIONAL:
return user.agent.structure.is_mus_or_bsv
case _:
return False

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand Down Expand Up @@ -612,14 +613,16 @@ class FicheDetectionVisibiliteUpdateView(View):
def post(self, request, *args, **kwargs):
fichedetection_id = kwargs["pk"]
form = self.form_class(request.POST, user=request.user, object_id=fichedetection_id)
if not form.has_changed():
return self.redirect_to_detail_view(fichedetection_id)

# Verifie si la visibilité n'a pas été modifiée pour éviter d'afficher un message d'erreur inutile
fiche_detection_visibilite = VisibiliteFiche.objects.get_visibilite_for_fiche_detection(fichedetection_id)
if fiche_detection_visibilite.libelle in [Visibilite.LOCAL, Visibilite.NATIONAL] and not form.has_changed():
return HttpResponseRedirect(reverse(self.redirect_url_name, args=[fichedetection_id]))

if form.is_valid():
form.save()
messages.success(request, "La visibilité de la fiche détection a bien été modifiée.")
else:
messages.error(request, "La visibilité de la fiche détection n'a pas pu être modifiée.")
return self.redirect_to_detail_view(fichedetection_id)

def redirect_to_detail_view(self, fichedetection_id):
return HttpResponseRedirect(reverse(self.redirect_url_name, args=[fichedetection_id]))

0 comments on commit 4058fef

Please sign in to comment.