From 3220219b165b80ac21115074fa8dfc5423d20379 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 3 Dec 2021 00:23:06 +0100 Subject: [PATCH] Setup rescue submission and edit --- nuitdelinfo_2021/settings.py | 9 ++++--- people/models.py | 3 +++ rescue/forms.py | 17 ++++++++++++ rescue/models.py | 13 ++++----- rescue/urls.py | 4 ++- rescue/views.py | 48 +++++++++++++++++++++++++++++++--- templates/article.html | 0 templates/article/article.html | 28 ++++++++++++++++++++ templates/article/edit.html | 10 +++++++ templates/article/submit.html | 10 +++++++ templates/people/edit.html | 10 +++---- 11 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 rescue/forms.py delete mode 100644 templates/article.html create mode 100644 templates/article/article.html create mode 100644 templates/article/edit.html create mode 100644 templates/article/submit.html diff --git a/nuitdelinfo_2021/settings.py b/nuitdelinfo_2021/settings.py index 4995664..2f0b5f7 100644 --- a/nuitdelinfo_2021/settings.py +++ b/nuitdelinfo_2021/settings.py @@ -80,9 +80,12 @@ WSGI_APPLICATION = 'nuitdelinfo_2021.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - } + 'ENGINE': 'django.db.backends.postgresql', + 'HOST': 'localhost', + 'USER': 'nuitdelinfo_2021', + 'NAME': 'nuitdelinfo_2021', + 'PASSWORD': 'nuitdelinfo_2021' + }, } diff --git a/people/models.py b/people/models.py index 59c349b..4a3d94a 100644 --- a/people/models.py +++ b/people/models.py @@ -22,3 +22,6 @@ class People(Model): validated = BooleanField(default=False) pending_edit_of = ForeignKey("self", on_delete=CASCADE, null=True, blank=True) + + def __str__(self): + return f"{self.first_name} {self.last_name}" diff --git a/rescue/forms.py b/rescue/forms.py new file mode 100644 index 0000000..3646b6a --- /dev/null +++ b/rescue/forms.py @@ -0,0 +1,17 @@ +from django.forms import ModelForm, DateInput + +from rescue.models import Rescue + + +class DateInput(DateInput): + input_type = "date" + + +class SubmitRescue(ModelForm): + class Meta: + model = Rescue + fields = ["name", "date", "location_long", "location_lat", "resume", "saved", "rescuers", "description", + "sources"] + widgets = { + "date": DateInput() + } diff --git a/rescue/models.py b/rescue/models.py index 184f618..5089e3b 100644 --- a/rescue/models.py +++ b/rescue/models.py @@ -1,20 +1,21 @@ -from django.db.models import Model, CharField, TextField, DateField, ManyToManyField, DecimalField +from django.db.models import Model, CharField, DateField, ManyToManyField, DecimalField +from django_quill.fields import QuillField from people.models import People class Rescue(Model): - name = CharField(max_length=70, null=False) - date = DateField(null=False) + name = CharField(max_length=70) + date = DateField() location_long = DecimalField(max_digits=9, decimal_places=6) location_lat = DecimalField(max_digits=9, decimal_places=6) - resume = CharField(max_length=125, null=False) + resume = CharField(max_length=125) saved = ManyToManyField(People, related_name="saved") rescuers = ManyToManyField(People, related_name="rescued") - description = TextField() + description = QuillField() - sources = TextField(null=False) + sources = QuillField() diff --git a/rescue/urls.py b/rescue/urls.py index 3b56c66..491a8a0 100644 --- a/rescue/urls.py +++ b/rescue/urls.py @@ -3,5 +3,7 @@ from django.urls import path from . import views urlpatterns = [ - path('', views.index, name='index'), + path('', views.index, name='index'), + path("submit/", views.submit, name="submit"), + path("edit//", views.edit, name="edit") ] diff --git a/rescue/views.py b/rescue/views.py index 39cdda2..da8792c 100644 --- a/rescue/views.py +++ b/rescue/views.py @@ -1,7 +1,49 @@ +from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest from django.shortcuts import render +from django.urls import reverse + +from rescue.forms import SubmitRescue +from rescue.models import Rescue -def index(request, article_id: int): - context = {} - return render(request, "article.html", context) +def get_rescue(rescue_id: int) -> Rescue: + try: + return Rescue.objects.get(pk=rescue_id) + except Rescue.DoesNotExist: + raise Http404("Rescue does not exist") + +def index(request, rescue_id: int): + return render(request, "article/article.html", {"rescue": get_rescue(rescue_id)}) + + +def submit(request): + if request.method == "POST": + form = SubmitRescue(request.POST) + if form.is_valid(): + rescue = form.save() + return HttpResponseRedirect(reverse(index, args=[rescue.pk])) + else: + form = SubmitRescue() + + return render(request, "article/submit.html", {"form": form}) + + +def edit(request, rescue_id: int): + edited_rescue = get_rescue(rescue_id) + if not edited_rescue.validated: + return HttpResponseBadRequest("This entry is not validated, you can't edit it") + + if request.method == "POST": + form = SubmitRescue(request.POST) + if form.is_valid(): + rescue = form.save() + rescue.pending_edit_of = edited_rescue + rescue.save() + + return HttpResponseRedirect(reverse(index, args=[rescue.pk])) + else: + edited_rescue.pk = None + form = SubmitRescue(instance=edited_rescue) + + return render(request, "article/edit.html", {"form": form, "edit_id": edited_rescue}) diff --git a/templates/article.html b/templates/article.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/article/article.html b/templates/article/article.html new file mode 100644 index 0000000..f222c24 --- /dev/null +++ b/templates/article/article.html @@ -0,0 +1,28 @@ +{% if not rescue.validated %} + {% if rescue.pending_edit_of %} +

This edit is not validated !

+ {% else %} +

This new entry is not validated !

+ {% endif %} +{% endif %} + +{{ rescue.name }}
+{{ rescue.date }}
+
+{{ rescue.location_long }} {{ rescue.location_lat }}
+
+{{ rescue.resume.html | safe }}
+{{ rescue.description.html | safe }}
+
+{{ rescue.testimonials.html | safe }}
+{{ rescue.sources.html | safe }} + +

Saved

+{% for people in rescue.saved.all %} + {{ people }} +{% endfor %} + +

Rescuers

+{% for people in rescue.rescuers.all %} + {{ people }} +{% endfor %} diff --git a/templates/article/edit.html b/templates/article/edit.html new file mode 100644 index 0000000..3685882 --- /dev/null +++ b/templates/article/edit.html @@ -0,0 +1,10 @@ + + {{ form.media }} + + +
+ {% csrf_token %} + {{ form }} + +
+ diff --git a/templates/article/submit.html b/templates/article/submit.html new file mode 100644 index 0000000..8bf0360 --- /dev/null +++ b/templates/article/submit.html @@ -0,0 +1,10 @@ + + {{ form.media }} + + +
+ {% csrf_token %} + {{ form }} + +
+ diff --git a/templates/people/edit.html b/templates/people/edit.html index 4e90bc0..2d13a50 100644 --- a/templates/people/edit.html +++ b/templates/people/edit.html @@ -2,9 +2,9 @@ {{ form.media }} -
- {% csrf_token %} - {{ form }} - -
+
+ {% csrf_token %} + {{ form }} + +