From d6948e455ac93238fe3326d4420c3ad4a0aad456 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 2 Dec 2021 23:46:42 +0100 Subject: [PATCH] Setup people submission and edit --- nuitdelinfo_2021/settings.py | 1 + people/forms.py | 18 ++++++++++++++ people/models.py | 30 +++++++++++++---------- people/urls.py | 4 +++- people/views.py | 46 ++++++++++++++++++++++++++++++++---- requirements.txt | 1 + templates/people.html | 0 templates/people/edit.html | 10 ++++++++ templates/people/people.html | 23 ++++++++++++++++++ templates/people/submit.html | 10 ++++++++ 10 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 people/forms.py delete mode 100644 templates/people.html create mode 100644 templates/people/edit.html create mode 100644 templates/people/people.html create mode 100644 templates/people/submit.html diff --git a/nuitdelinfo_2021/settings.py b/nuitdelinfo_2021/settings.py index 9a45ba3..4995664 100644 --- a/nuitdelinfo_2021/settings.py +++ b/nuitdelinfo_2021/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + "django_quill" ] MIDDLEWARE = [ diff --git a/people/forms.py b/people/forms.py new file mode 100644 index 0000000..257c336 --- /dev/null +++ b/people/forms.py @@ -0,0 +1,18 @@ +from django.forms import ModelForm, DateInput + +from people.models import People + + +class DateInput(DateInput): + input_type = "date" + + +class SubmitPeople(ModelForm): + class Meta: + model = People + fields = ["first_name", "last_name", "title", "description", "birth", "death", "history", "genealogy", "awards", + "pro_life", "testimonials", "sources"] + widgets = { + "birth": DateInput(), + "death": DateInput() + } diff --git a/people/models.py b/people/models.py index be8906a..59c349b 100644 --- a/people/models.py +++ b/people/models.py @@ -1,20 +1,24 @@ -from django.db.models import Model, CharField, DateField, TextField +from django.db.models import Model, CharField, DateField, BooleanField, ForeignKey, CASCADE +from django_quill.fields import QuillField class People(Model): - first_name = CharField(max_length=40, null=False) - last_name = CharField(max_length=60, null=False) + first_name = CharField(max_length=40) + last_name = CharField(max_length=60) - title = CharField(max_length=70) - description = TextField(null=False) + title = CharField(max_length=70, null=True, blank=True) + description = QuillField() - birth = DateField(null=False) - death = DateField() + birth = DateField() + death = DateField(null=True, blank=True) - history = TextField() - genealogy = TextField() - awards = TextField() - pro_life = TextField() + history = QuillField(null=True, blank=True) + genealogy = QuillField(null=True, blank=True) + awards = QuillField(null=True, blank=True) + pro_life = QuillField(null=True, blank=True) - testimonials = TextField() - sources = TextField(null=False) + testimonials = QuillField(null=True, blank=True) + sources = QuillField() + + validated = BooleanField(default=False) + pending_edit_of = ForeignKey("self", on_delete=CASCADE, null=True, blank=True) diff --git a/people/urls.py b/people/urls.py index 96de495..ae7518b 100644 --- a/people/urls.py +++ b/people/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/people/views.py b/people/views.py index 53dc36f..b29af5f 100644 --- a/people/views.py +++ b/people/views.py @@ -1,15 +1,51 @@ -from django.http import Http404 +from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest from django.shortcuts import render +from django.urls import reverse +from people.forms import SubmitPeople from people.models import People -def index(request, people_id: int): +def get_people(people_id: int) -> People: try: - people = People.objects.get(pk=people_id) + return People.objects.get(pk=people_id) except People.DoesNotExist: raise Http404("People does not exist") - return render(request, "people.html", { - "people": people + +def index(request, people_id: int): + return render(request, "people/people.html", { + "people": get_people(people_id) }) + + +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])) + else: + form = SubmitPeople() + + return render(request, "people/submit.html", {"form": form}) + + +def edit(request, people_id: int): + edited_people = get_people(people_id) + if not edited_people.validated: + return HttpResponseBadRequest("This entry is not validated, you can't edit it") + + if request.method == "POST": + form = SubmitPeople(request.POST) + if form.is_valid(): + people = form.save() + people.pending_edit_of = edited_people + people.save() + + return HttpResponseRedirect(reverse(index, args=[people.pk])) + else: + edited_people.pk = None + form = SubmitPeople(instance=edited_people) + + return render(request, "people/edit.html", {"form": form, "edit_id": people_id}) diff --git a/requirements.txt b/requirements.txt index c496a00..32348c3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ asgiref==3.4.1 Django==3.2.9 +django-quill-editor==0.1.22 pytz==2021.3 sqlparse==0.4.2 diff --git a/templates/people.html b/templates/people.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/people/edit.html b/templates/people/edit.html new file mode 100644 index 0000000..4e90bc0 --- /dev/null +++ b/templates/people/edit.html @@ -0,0 +1,10 @@ + + {{ form.media }} + + +
+ {% csrf_token %} + {{ form }} + +
+ diff --git a/templates/people/people.html b/templates/people/people.html new file mode 100644 index 0000000..96de6fe --- /dev/null +++ b/templates/people/people.html @@ -0,0 +1,23 @@ +{% if not people.validated %} + {% if people.pending_edit_of %} +

This edit is not validated !

+ {% else %} +

This new entry is not validated !

+ {% endif %} +{% endif %} + +{{ people.first_name }} {{ people.last_name }}
+{{ people.title }}
+
+{{ people.description.html | safe }}
+
+{{ people.birth }}
+{{ people.death }}
+
+{{ people.history.html | safe }}
+{{ people.genealogy.html | safe }}
+{{ people.awards.html | safe }}
+{{ people.pro_life.html | safe }}
+
+{{ people.testimonials.html | safe }}
+{{ people.sources.html | safe }} diff --git a/templates/people/submit.html b/templates/people/submit.html new file mode 100644 index 0000000..f0036e2 --- /dev/null +++ b/templates/people/submit.html @@ -0,0 +1,10 @@ + + {{ form.media }} + + +
+ {% csrf_token %} + {{ form }} + +
+