Setup rescue submission and edit

This commit is contained in:
Ethanell 2021-12-03 00:23:06 +01:00
parent d6948e455a
commit 3220219b16
11 changed files with 134 additions and 18 deletions

View file

@ -80,9 +80,12 @@ WSGI_APPLICATION = 'nuitdelinfo_2021.wsgi.application'
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.postgresql',
'NAME': BASE_DIR / 'db.sqlite3', 'HOST': 'localhost',
} 'USER': 'nuitdelinfo_2021',
'NAME': 'nuitdelinfo_2021',
'PASSWORD': 'nuitdelinfo_2021'
},
} }

View file

@ -22,3 +22,6 @@ class People(Model):
validated = BooleanField(default=False) validated = BooleanField(default=False)
pending_edit_of = ForeignKey("self", on_delete=CASCADE, null=True, blank=True) pending_edit_of = ForeignKey("self", on_delete=CASCADE, null=True, blank=True)
def __str__(self):
return f"{self.first_name} {self.last_name}"

17
rescue/forms.py Normal file
View file

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

View file

@ -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 from people.models import People
class Rescue(Model): class Rescue(Model):
name = CharField(max_length=70, null=False) name = CharField(max_length=70)
date = DateField(null=False) date = DateField()
location_long = DecimalField(max_digits=9, decimal_places=6) location_long = DecimalField(max_digits=9, decimal_places=6)
location_lat = 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") saved = ManyToManyField(People, related_name="saved")
rescuers = ManyToManyField(People, related_name="rescued") rescuers = ManyToManyField(People, related_name="rescued")
description = TextField() description = QuillField()
sources = TextField(null=False) sources = QuillField()

View file

@ -3,5 +3,7 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('<int:article_id>', views.index, name='index'), path('<int:rescue_id>', views.index, name='index'),
path("submit/", views.submit, name="submit"),
path("edit/<int:rescue_id>/", views.edit, name="edit")
] ]

View file

@ -1,7 +1,49 @@
from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest
from django.shortcuts import render 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): def get_rescue(rescue_id: int) -> Rescue:
context = {} try:
return render(request, "article.html", context) 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})

View file

View file

@ -0,0 +1,28 @@
{% 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 %}
{{ rescue.name }} <br />
{{ rescue.date }} <br />
<br />
{{ rescue.location_long }} {{ rescue.location_lat }} <br />
<br />
{{ rescue.resume.html | safe }} <br />
{{ rescue.description.html | safe }} <br />
<br />
{{ rescue.testimonials.html | safe }} <br />
{{ rescue.sources.html | safe }}
<h3>Saved</h3>
{% for people in rescue.saved.all %}
<a href="/p/{{ people.pk }}">{{ people }}</a>
{% endfor %}
<h3>Rescuers</h3>
{% for people in rescue.rescuers.all %}
<a href="/p/{{ people.pk }}">{{ people }}</a>
{% endfor %}

View file

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

View file

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