-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajoute une gestion basique des documents
Ce commit permet d'introduire une gestion basique des documents dans l'application. Les documents sont des objets qui portent les informations sur un fichier ajouté dans le processus de gestion des événements. Pour le moment la seule implémentation est faite dans les fiches détection, mais le but est que le concept puisse être ré-utilisé au travers de toute l'application. Pour la gestion des fichiers l'ajout de la bibliothéque django-storages a été nécessaire. A défaut d'avoir l'infra de prête j'ai utilisé minio en local pour simuler un bucket, j'ai ajouté les instructions pour que tout le monde puisse reproduire le processus si nécessaire. J'ai aussi ajouté pytest-env afin de pouvoir configurer simplement des variables d'env différentes pour les tests. Les fonctionnalités implémentées dans ce commit sont: - La gestion basique des documents (création du modèle, admin, etc.) - En tant qu'utilisateur je peut uploader un document avec les informations nécessaires. - En tant qu'utilisateur je peux supprimer (soft-delete) un document après confirmation. - En tant qu'utilisateur, je voit les documents liés a une fiche sur la fiche en question. Les points qui pourraient être améliorés sont: - Implémentation d'un tri pour les documents - Ajout de tous les types de documents - Lien entre un document et un utilisateur (qui a ajouté, qui a supprimé)
- Loading branch information
Showing
25 changed files
with
493 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,10 @@ SECRET_KEY=secret | |
|
||
# DB | ||
DATABASE_URL=psql://user:[email protected]:8458/database_name | ||
|
||
# Storage | ||
STORAGE_ENGINE="django.core.files.storage.FileSystemStorage" | ||
STORAGE_BUCKET_NAME="dev" | ||
STORAGE_ACCESS_KEY="XXX" | ||
STORAGE_SECRET_KEY="XXX" | ||
STORAGE_URL="XXX" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.contrib import admin | ||
from .models import Document | ||
|
||
|
||
admin.site.register(Document) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Generated by Django 5.0.3 on 2024-07-02 06:48 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("contenttypes", "0002_remove_content_type_name"), | ||
("core", "0005_delete_structurehierarchique"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Document", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("nom", models.CharField(max_length=256)), | ||
("description", models.TextField()), | ||
( | ||
"document_type", | ||
models.CharField( | ||
choices=[("autre", "Autre document")], max_length=100 | ||
), | ||
), | ||
("file", models.FileField(upload_to="")), | ||
( | ||
"date_creation", | ||
models.DateTimeField( | ||
auto_now_add=True, verbose_name="Date de création" | ||
), | ||
), | ||
("is_deleted", models.BooleanField(default=False)), | ||
("object_id", models.PositiveIntegerField()), | ||
( | ||
"content_type", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
to="contenttypes.contenttype", | ||
), | ||
), | ||
], | ||
options={ | ||
"indexes": [ | ||
models.Index( | ||
fields=["content_type", "object_id"], | ||
name="core_docume_content_343c2e_idx", | ||
) | ||
], | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from core.forms import DocumentUploadForm | ||
|
||
|
||
class WithDocumentUploadFormMixin: | ||
def get_object_linked_to_document(self): | ||
raise NotImplementedError | ||
|
||
def get_redirect_url_after_upload(self): | ||
raise NotImplementedError | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
obj = self.get_object_linked_to_document() | ||
context["document_form"] = DocumentUploadForm(obj=obj, next=obj.get_absolute_url()) | ||
return context | ||
|
||
|
||
class WithDocumentListInContextMixin: | ||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
context["document_list"] = self.get_object().documents.all() | ||
return context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<div class="fr-card"> | ||
<div class="fr-card__body"> | ||
<div class="fr-card__content document__details {% if document.is_deleted %}document__details--deleted{% endif %}"> | ||
<p class="fr-hint-text">Ajouté le {{ document.date_creation }}</p> | ||
<h6 class="fr-h6">{{ document.nom }}</h6> | ||
<p class="fr-card__dess fr-mb-1w">{{ document.description | truncatechars:150 | default:"Pas de description" }}</p> | ||
<p class="fr-tag fr-my-1w">{{document.document_type|title}}</p> | ||
|
||
{% if document.is_deleted %} | ||
Document supprimé | ||
{% else %} | ||
<div class="fr-btns-group--right document__actions"> | ||
<a href="{{ document.file.url }}" target="_blank" class="fr-icon-download-line fr-btn fr-btn--tertiary fr-mr-1w"></a> | ||
<a href="#" class="fr-icon-delete-line fr-btn fr-btn--tertiary" data-fr-opened="false" aria-controls="fr-modal-{{ document.pk }}"></a> | ||
</div> | ||
{% include "core/_modale_suppression_document.html" %} | ||
{% endif %} | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<div class="fr-btns-group--right fr-mb-2w"> | ||
<button class="fr-btn fr-btn--secondary fr-btn--icon-left fr-icon-add-circle-line" data-testid="documents-add" data-fr-opened="false" aria-controls="fr-modal-add-doc"> | ||
Ajouter un document | ||
</button> | ||
</div> | ||
|
||
{% include "core/_modale_ajout_document.html" %} | ||
<div class="fr-container--fluid"> | ||
<div class="fr-grid-row fr-grid-row--gutters"> | ||
{% for document in document_list %} | ||
<div class="fr-col-3 fr-col-xl-2"> | ||
{% include "core/_carte_resume_document.html" %} | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
<div class="bloc-commun-gestion"> | ||
<div class="fr-tabs fr-mt-2w"> | ||
<ul class="fr-tabs__list" role="tablist"> | ||
<li role="presentation"> | ||
<button id="tabpanel-404" class="fr-tabs__tab fr-icon-checkbox-line fr-tabs__tab--icon-left" tabindex="0" role="tab" aria-selected="true" aria-controls="tabpanel-404-panel">Fil de suivi</button> | ||
</li> | ||
<li role="presentation"> | ||
<button id="tabpanel-405" class="fr-tabs__tab fr-icon-checkbox-line fr-tabs__tab--icon-left" tabindex="-1" role="tab" aria-selected="false" aria-controls="tabpanel-405-panel">Contacts</button> | ||
</li> | ||
<li role="presentation"> | ||
<button id="tabpanel-406" class="fr-tabs__tab fr-icon-checkbox-line fr-tabs__tab--icon-left" tabindex="-1" role="tab" aria-selected="false" aria-controls="tabpanel-406-panel" data-testid="documents">Documents</button> | ||
</li> | ||
</ul> | ||
<div id="tabpanel-404-panel" class="fr-tabs__panel fr-tabs__panel--selected" role="tabpanel" aria-labelledby="tabpanel-404" tabindex="0"> | ||
</div> | ||
<div id="tabpanel-405-panel" class="fr-tabs__panel" role="tabpanel" aria-labelledby="tabpanel-405" tabindex="0"> | ||
</div> | ||
<div id="tabpanel-406-panel" class="fr-tabs__panel" role="tabpanel" aria-labelledby="tabpanel-406" tabindex="0"> | ||
{% include "core/_documents.html" %} | ||
</div> | ||
</div> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<dialog aria-labelledby="fr-modal-add-doc" id="fr-modal-add-doc" class="fr-modal" role="dialog"> | ||
<div class="fr-container fr-container--fluid fr-container-md"> | ||
<div class="fr-grid-row fr-grid-row--center"> | ||
<div class="fr-col-12 fr-col-md-8 fr-col-lg-6"> | ||
<div class="fr-modal__body"> | ||
<form method="post" action="{% url 'document-upload' %}" enctype="multipart/form-data"> | ||
{% csrf_token %} | ||
|
||
<div class="fr-modal__header"> | ||
<button class="fr-btn--close fr-btn" aria-controls="fr-modal-add-doc">Fermer</button> | ||
</div> | ||
<div class="fr-modal__content"><h1 class="fr-modal__title"><span | ||
class="fr-icon-arrow-right-line fr-icon--lg"></span>Ajouter un document</h1> | ||
|
||
{{ document_form.as_dsfr_div }} | ||
|
||
</div> | ||
<div class="fr-modal__footer"> | ||
<div class="fr-btns-group fr-btns-group--right fr-btns-group--inline-lg"> | ||
<button type="submit" class="fr-btn" data-testid="documents-send">Envoyer</button> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</dialog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<dialog aria-labelledby="fr-modal-{{ document.pk }}-title" id="fr-modal-{{ document.pk }}" class="fr-modal" role="dialog"> | ||
<div class="fr-container fr-container--fluid fr-container-md"> | ||
<div class="fr-grid-row fr-grid-row--center"> | ||
<div class="fr-col-12 fr-col-md-8 fr-col-lg-6"> | ||
<div class="fr-modal__body"> | ||
<div class="fr-modal__header"> | ||
<button class="fr-btn--close fr-btn" aria-controls="fr-modal-{{ document.pk }}">Fermer</button> | ||
</div> | ||
<div class="fr-modal__content"><h1 class="fr-modal__title"><span | ||
class="fr-icon-arrow-right-line fr-icon--lg"></span>Supprimer un document</h1> | ||
Voulez-vous supprimer le document {{ document.nom }} ? | ||
</div> | ||
|
||
<div class="fr-modal__footer"> | ||
<div class="fr-btns-group fr-btns-group--right fr-btns-group--inline-lg"> | ||
<button class="fr-btn fr-btn--secondary" aria-controls="fr-modal-{{ document.pk }}">Annuler</button> | ||
<form action="{% url 'document-delete' document.pk %}" method="POST"> | ||
{% csrf_token %} | ||
<input type="hidden" value="{{ document.pk }}" name="pk"> | ||
<input type="hidden" value="{{ redirect_url }}" name="next"> | ||
<button type="submit" class="fr-btn" data-testid="documents-delete-{{ document.pk }}">Supprimer le document</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</dialog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.urls import path | ||
from .views import DocumentUploadView, DocumentDeleteView | ||
|
||
urlpatterns = [ | ||
path( | ||
"document-upload/", | ||
DocumentUploadView.as_view(), | ||
name="document-upload", | ||
), | ||
path( | ||
"document-delete/<int:pk>/", | ||
DocumentDeleteView.as_view(), | ||
name="document-delete", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.shortcuts import get_object_or_404 | ||
from django.views import View | ||
from django.views.generic.edit import FormView | ||
from .forms import DocumentUploadForm | ||
from django.http import HttpResponseRedirect | ||
from django.utils.http import url_has_allowed_host_and_scheme | ||
|
||
from .models import Document | ||
|
||
|
||
class DocumentUploadView(FormView): | ||
form_class = DocumentUploadForm | ||
|
||
def _get_redirect(self): | ||
if url_has_allowed_host_and_scheme( | ||
url=self.request.POST.get("next"), | ||
allowed_hosts={self.request.get_host()}, | ||
require_https=self.request.is_secure(), | ||
): | ||
return HttpResponseRedirect(self.request.POST.get("next")) | ||
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) | ||
|
||
def post(self, request, *args, **kwargs): | ||
form = DocumentUploadForm(request.POST, request.FILES) | ||
if form.is_valid(): | ||
form.save() | ||
messages.success(request, "Le document a été ajouté avec succés.") | ||
return self._get_redirect() | ||
|
||
messages.error(request, "Une erreur s'est produite lors de l'ajout du document") | ||
return self._get_redirect() | ||
|
||
|
||
class DocumentDeleteView(View): | ||
def post(self, request, *args, **kwargs): | ||
document = get_object_or_404(Document, pk=kwargs.get("pk")) | ||
document.is_deleted = True | ||
document.save() | ||
messages.success(request, "Le document a été marqué comme supprimé.") | ||
return HttpResponseRedirect(request.POST.get("next")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = seves.settings | ||
python_files = test_*.py | ||
addopts = -nauto | ||
addopts = -nauto | ||
env = | ||
STORAGE_ENGINE=django.core.files.storage.FileSystemStorage | ||
STORAGE_BUCKET_NAME= | ||
STORAGE_ACCESS_KEY= | ||
STORAGE_SECRET_KEY= | ||
STORAGE_URL= |
Oops, something went wrong.