Add boats

This commit is contained in:
Ethanell 2021-12-03 04:52:09 +01:00
parent 8293b77752
commit 6f02c4b050
19 changed files with 197 additions and 8 deletions

0
boat/__init__.py Normal file
View file

3
boat/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
boat/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class BoatConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'boat'

13
boat/forms.py Normal file
View file

@ -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"]

View file

17
boat/models.py Normal file
View file

@ -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

3
boat/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
boat/urls.py Normal file
View file

@ -0,0 +1,11 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name='index'),
path("<int:boat_id>/", views.details, name='details'),
path("submit/", views.submit, name="submit"),
path("edit/<int:boat_id>/", views.edit, name="edit"),
path("ajax/search/<str:text>/", views.ajax_search, name="ajax_search")
]

65
boat/views.py Normal file
View file

@ -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)))

View file

@ -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',

View file

@ -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(

View file

@ -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)

View file

@ -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()

View file

@ -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()

View file

@ -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)

17
templates/boat/boat.html Normal file
View file

@ -0,0 +1,17 @@
{% if not rescue.validated %}
{% if rescue.pending_edit_of %}
<h1>This edit is not validated !</h1>
{% else %}
<h1>This new entry is not validated !</h1>
{% endif %}
{% endif %}
{{ boat.name }} <br />
{{ boat.model }} <br />
<br />
{{ boat.description.html | safe }} <br />
<h3>Rescues</h3>
{% for rescue in rescues.all %}
<a href="/a/{{ rescue.pk }}">{{ rescue }}</a>
{% endfor %}

25
templates/boat/boats.html Normal file
View file

@ -0,0 +1,25 @@
{% extends 'base.html' %}
{% load static %}
{% block content %}
<section>
<h1>Bateaux</h1>
<div class="flex-card">
{% for boat in boats %}
<div class="card">
<div class="inner">
<div class="img-container">
<img src="{% static 'images/sauvetage.png' %}">
</div>
<div class="content">
<p class="titre">{{ boat.name }}</p>
<a class="btn" href="/b/{{ boat.pk }}/">Voir le bateau</a>
</div>
</div>
</div>
{% endfor %}
</div>
<a class="btn btn--blue" href="">Charger plus de bateaux</a>
</section>
{% include 'quicksearch.html' %}
{% endblock %}

10
templates/boat/edit.html Normal file
View file

@ -0,0 +1,10 @@
<head>
{{ form.media }}
</head>
<body>
<form action="/b/edit/{{ edit_id }}/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
</body>

View file

@ -0,0 +1,10 @@
<head>
{{ form.media }}
</head>
<body>
<form action="/b/submit/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
</body>