From 6f02c4b050ef45bddad1f0ecb6e508610f3cf014 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 3 Dec 2021 04:52:09 +0100 Subject: [PATCH] Add boats --- boat/__init__.py | 0 boat/admin.py | 3 ++ boat/apps.py | 6 +++ boat/forms.py | 13 ++++++ boat/migrations/__init__.py | 0 boat/models.py | 17 ++++++++ boat/tests.py | 3 ++ boat/urls.py | 11 +++++ boat/views.py | 65 ++++++++++++++++++++++++++++ nuitdelinfo_2021/settings.py.exemple | 1 + nuitdelinfo_2021/urls.py | 5 ++- people/views.py | 5 ++- rescue/forms.py | 2 +- rescue/models.py | 8 +++- rescue/views.py | 4 +- templates/boat/boat.html | 17 ++++++++ templates/boat/boats.html | 25 +++++++++++ templates/boat/edit.html | 10 +++++ templates/boat/submit.html | 10 +++++ 19 files changed, 197 insertions(+), 8 deletions(-) create mode 100644 boat/__init__.py create mode 100644 boat/admin.py create mode 100644 boat/apps.py create mode 100644 boat/forms.py create mode 100644 boat/migrations/__init__.py create mode 100644 boat/models.py create mode 100644 boat/tests.py create mode 100644 boat/urls.py create mode 100644 boat/views.py create mode 100644 templates/boat/boat.html create mode 100644 templates/boat/boats.html create mode 100644 templates/boat/edit.html create mode 100644 templates/boat/submit.html diff --git a/boat/__init__.py b/boat/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/boat/admin.py b/boat/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/boat/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/boat/apps.py b/boat/apps.py new file mode 100644 index 0000000..d015290 --- /dev/null +++ b/boat/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BoatConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'boat' diff --git a/boat/forms.py b/boat/forms.py new file mode 100644 index 0000000..f4254a8 --- /dev/null +++ b/boat/forms.py @@ -0,0 +1,13 @@ +from django.forms import ModelForm, DateInput + +from boat.models import Boat + + +class DateInput(DateInput): + input_type = "date" + + +class SubmitBoat(ModelForm): + class Meta: + model = Boat + fields = ["name", "model", "description"] diff --git a/boat/migrations/__init__.py b/boat/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/boat/models.py b/boat/models.py new file mode 100644 index 0000000..0921c87 --- /dev/null +++ b/boat/models.py @@ -0,0 +1,17 @@ +from django.db.models import Model, CharField, BooleanField, ForeignKey, CASCADE +from django_quill.fields import QuillField + +from people.models import People + + +class Boat(Model): + name = CharField(max_length=80) + model = CharField(max_length=80) + + description = QuillField() + + validated = BooleanField(default=False) + pending_edit_of = ForeignKey(People, on_delete=CASCADE, null=True, blank=True) + + def __str__(self): + return self.name diff --git a/boat/tests.py b/boat/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/boat/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/boat/urls.py b/boat/urls.py new file mode 100644 index 0000000..a35eaa2 --- /dev/null +++ b/boat/urls.py @@ -0,0 +1,11 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path("", views.index, name='index'), + path("/", views.details, name='details'), + path("submit/", views.submit, name="submit"), + path("edit//", views.edit, name="edit"), + path("ajax/search//", views.ajax_search, name="ajax_search") +] diff --git a/boat/views.py b/boat/views.py new file mode 100644 index 0000000..bb6ef29 --- /dev/null +++ b/boat/views.py @@ -0,0 +1,65 @@ +from django.contrib.auth.decorators import login_required +from django.db.models import Q +from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest, JsonResponse +from django.shortcuts import render +from django.urls import reverse + +from boat.forms import SubmitBoat +from boat.models import Boat + + +def get_boat(boat_id: int) -> Boat: + try: + return Boat.objects.get(pk=boat_id) + except Boat.DoesNotExist: + raise Http404("Boat does not exist") + + +def index(request): + return render(request, "boat/boats.html", { + "boats": Boat.objects.all() + }) + + +def details(request, boat_id: int): + return render(request, "boat/boat.html", { + "boat": get_boat(boat_id) + }) + + +@login_required +def submit(request): + if request.method == "POST": + form = SubmitBoat(request.POST) + if form.is_valid(): + boat = form.save() + return HttpResponseRedirect(reverse(details, args=[boat.pk])) + else: + form = SubmitBoat() + + return render(request, "boat/submit.html", {"form": form}) + + +@login_required +def edit(request, boat_id: int): + edited_boat = get_boat(boat_id) + if not edited_boat.validated: + return HttpResponseBadRequest("This entry is not validated, you can't edit it") + + if request.method == "POST": + form = SubmitBoat(request.POST) + if form.is_valid(): + boat = form.save() + boat.pending_edit_of = edited_boat + boat.save() + + return HttpResponseRedirect(reverse(details, args=[boat.pk])) + else: + edited_boat.pk = None + form = SubmitBoat(instance=edited_boat) + + return render(request, "boat/edit.html", {"form": form, "edit_id": boat_id}) + + +def ajax_search(request, text: str): + return JsonResponse(Boat.objects.filter(Q(name__icontains=text) | Q(description__icontains=text))) diff --git a/nuitdelinfo_2021/settings.py.exemple b/nuitdelinfo_2021/settings.py.exemple index b54ae13..2ed6653 100644 --- a/nuitdelinfo_2021/settings.py.exemple +++ b/nuitdelinfo_2021/settings.py.exemple @@ -34,6 +34,7 @@ INSTALLED_APPS = [ "core.apps.AppConfig", "rescue.apps.RescueConfig", "people.apps.PeopleConfig", + "boat.apps.BoatConfig", "error.apps.ErrorConfig", 'django.contrib.admin', 'django.contrib.auth', diff --git a/nuitdelinfo_2021/urls.py b/nuitdelinfo_2021/urls.py index 01253c1..65a41b9 100644 --- a/nuitdelinfo_2021/urls.py +++ b/nuitdelinfo_2021/urls.py @@ -22,8 +22,9 @@ from nuitdelinfo_2021.forms import MyCustomUserForm urlpatterns = [ path('', views.index, name="index"), - path("a/", include("rescue.urls")), - path("p/", include("people.urls")), + path("a/", include("rescue.urls"), name="rescue"), + path("p/", include("people.urls"), name="people"), + path("b/", include("boat.urls"), name="boat"), path('admin/', admin.site.urls), path('accounts/register/', RegistrationView.as_view( diff --git a/people/views.py b/people/views.py index 827d59e..36c7b6e 100644 --- a/people/views.py +++ b/people/views.py @@ -26,13 +26,14 @@ def details(request, people_id: int): "people": get_people(people_id) }) + @login_required def submit(request): if request.method == "POST": form = SubmitPeople(request.POST) if form.is_valid(): people = form.save() - return HttpResponseRedirect(reverse(index, args=[people.pk])) + return HttpResponseRedirect(reverse(details, args=[people.pk])) else: form = SubmitPeople() @@ -52,7 +53,7 @@ def edit(request, people_id: int): people.pending_edit_of = edited_people people.save() - return HttpResponseRedirect(reverse(index, args=[people.pk])) + return HttpResponseRedirect(reverse(details, args=[people.pk])) else: edited_people.pk = None form = SubmitPeople(instance=edited_people) diff --git a/rescue/forms.py b/rescue/forms.py index 3646b6a..f6089a2 100644 --- a/rescue/forms.py +++ b/rescue/forms.py @@ -10,7 +10,7 @@ class DateInput(DateInput): class SubmitRescue(ModelForm): class Meta: model = Rescue - fields = ["name", "date", "location_long", "location_lat", "resume", "saved", "rescuers", "description", + fields = ["name", "date", "location_long", "location_lat", "resume", "saved", "rescuers", "boats", "description", "sources"] widgets = { "date": DateInput() diff --git a/rescue/models.py b/rescue/models.py index 3b0e67a..ee991d9 100644 --- a/rescue/models.py +++ b/rescue/models.py @@ -1,6 +1,8 @@ -from django.db.models import Model, CharField, DateField, ManyToManyField, DecimalField +from django.db.models import Model, CharField, DateField, ManyToManyField, DecimalField, BooleanField, ForeignKey, \ + CASCADE from django_quill.fields import QuillField +from boat.models import Boat from people.models import People @@ -15,6 +17,10 @@ class Rescue(Model): saved = ManyToManyField(People, related_name="saved") rescuers = ManyToManyField(People, related_name="rescued") + boats = ManyToManyField(Boat) + + validated = BooleanField(default=False) + pending_edit_of = ForeignKey(People, on_delete=CASCADE, null=True, blank=True) description = QuillField() diff --git a/rescue/views.py b/rescue/views.py index 9c329cb..8e26770 100644 --- a/rescue/views.py +++ b/rescue/views.py @@ -29,7 +29,7 @@ def submit(request): form = SubmitRescue(request.POST) if form.is_valid(): rescue = form.save() - return HttpResponseRedirect(reverse(index, args=[rescue.pk])) + return HttpResponseRedirect(reverse(details, args=[rescue.pk])) else: form = SubmitRescue() @@ -49,7 +49,7 @@ def edit(request, rescue_id: int): rescue.pending_edit_of = edited_rescue rescue.save() - return HttpResponseRedirect(reverse(index, args=[rescue.pk])) + return HttpResponseRedirect(reverse(details, args=[rescue.pk])) else: edited_rescue.pk = None form = SubmitRescue(instance=edited_rescue) diff --git a/templates/boat/boat.html b/templates/boat/boat.html new file mode 100644 index 0000000..02ff03f --- /dev/null +++ b/templates/boat/boat.html @@ -0,0 +1,17 @@ +{% if not rescue.validated %} + {% if rescue.pending_edit_of %} +

This edit is not validated !

+ {% else %} +

This new entry is not validated !

+ {% endif %} +{% endif %} + +{{ boat.name }}
+{{ boat.model }}
+
+{{ boat.description.html | safe }}
+ +

Rescues

+{% for rescue in rescues.all %} + {{ rescue }} +{% endfor %} diff --git a/templates/boat/boats.html b/templates/boat/boats.html new file mode 100644 index 0000000..d81bb5e --- /dev/null +++ b/templates/boat/boats.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} +{% load static %} +{% block content %} +
+

Bateaux

+
+ {% for boat in boats %} +
+
+
+ +
+
+

{{ boat.name }}

+ Voir le bateau +
+
+ +
+ {% endfor %} +
+ Charger plus de bateaux +
+ {% include 'quicksearch.html' %} +{% endblock %} \ No newline at end of file diff --git a/templates/boat/edit.html b/templates/boat/edit.html new file mode 100644 index 0000000..493a533 --- /dev/null +++ b/templates/boat/edit.html @@ -0,0 +1,10 @@ + + {{ form.media }} + + +
+ {% csrf_token %} + {{ form }} + +
+ diff --git a/templates/boat/submit.html b/templates/boat/submit.html new file mode 100644 index 0000000..8b0a4ec --- /dev/null +++ b/templates/boat/submit.html @@ -0,0 +1,10 @@ + + {{ form.media }} + + +
+ {% csrf_token %} + {{ form }} + +
+