Setup people submission and edit
This commit is contained in:
parent
691afa7298
commit
d6948e455a
10 changed files with 124 additions and 19 deletions
|
@ -40,6 +40,7 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
"django_quill"
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
18
people/forms.py
Normal file
18
people/forms.py
Normal file
|
@ -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()
|
||||||
|
}
|
|
@ -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):
|
class People(Model):
|
||||||
first_name = CharField(max_length=40, null=False)
|
first_name = CharField(max_length=40)
|
||||||
last_name = CharField(max_length=60, null=False)
|
last_name = CharField(max_length=60)
|
||||||
|
|
||||||
title = CharField(max_length=70)
|
title = CharField(max_length=70, null=True, blank=True)
|
||||||
description = TextField(null=False)
|
description = QuillField()
|
||||||
|
|
||||||
birth = DateField(null=False)
|
birth = DateField()
|
||||||
death = DateField()
|
death = DateField(null=True, blank=True)
|
||||||
|
|
||||||
history = TextField()
|
history = QuillField(null=True, blank=True)
|
||||||
genealogy = TextField()
|
genealogy = QuillField(null=True, blank=True)
|
||||||
awards = TextField()
|
awards = QuillField(null=True, blank=True)
|
||||||
pro_life = TextField()
|
pro_life = QuillField(null=True, blank=True)
|
||||||
|
|
||||||
testimonials = TextField()
|
testimonials = QuillField(null=True, blank=True)
|
||||||
sources = TextField(null=False)
|
sources = QuillField()
|
||||||
|
|
||||||
|
validated = BooleanField(default=False)
|
||||||
|
pending_edit_of = ForeignKey("self", on_delete=CASCADE, null=True, blank=True)
|
||||||
|
|
|
@ -3,5 +3,7 @@ from django.urls import path
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('<int:people_id>/', views.index, name='index'),
|
path("<int:people_id>/", views.index, name='index'),
|
||||||
|
path("submit/", views.submit, name="submit"),
|
||||||
|
path("edit/<int:people_id>/", views.edit, name="edit")
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,15 +1,51 @@
|
||||||
from django.http import Http404
|
from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from people.forms import SubmitPeople
|
||||||
from people.models import People
|
from people.models import People
|
||||||
|
|
||||||
|
|
||||||
def index(request, people_id: int):
|
def get_people(people_id: int) -> People:
|
||||||
try:
|
try:
|
||||||
people = People.objects.get(pk=people_id)
|
return People.objects.get(pk=people_id)
|
||||||
except People.DoesNotExist:
|
except People.DoesNotExist:
|
||||||
raise Http404("People does not exist")
|
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})
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
asgiref==3.4.1
|
asgiref==3.4.1
|
||||||
Django==3.2.9
|
Django==3.2.9
|
||||||
|
django-quill-editor==0.1.22
|
||||||
pytz==2021.3
|
pytz==2021.3
|
||||||
sqlparse==0.4.2
|
sqlparse==0.4.2
|
||||||
|
|
10
templates/people/edit.html
Normal file
10
templates/people/edit.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<head>
|
||||||
|
{{ form.media }}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="/p/edit/{{ edit_id }}/" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form }}
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
23
templates/people/people.html
Normal file
23
templates/people/people.html
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
{% if not people.validated %}
|
||||||
|
{% if people.pending_edit_of %}
|
||||||
|
<h1>This edit is not validated !</h1>
|
||||||
|
{% else %}
|
||||||
|
<h1>This new entry is not validated !</h1>
|
||||||
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{{ people.first_name }} {{ people.last_name }} <br />
|
||||||
|
{{ people.title }} <br />
|
||||||
|
<br />
|
||||||
|
{{ people.description.html | safe }} <br />
|
||||||
|
<br />
|
||||||
|
{{ people.birth }} <br />
|
||||||
|
{{ people.death }} <br />
|
||||||
|
<br />
|
||||||
|
{{ people.history.html | safe }} <br />
|
||||||
|
{{ people.genealogy.html | safe }} <br />
|
||||||
|
{{ people.awards.html | safe }} <br />
|
||||||
|
{{ people.pro_life.html | safe }} <br />
|
||||||
|
<br />
|
||||||
|
{{ people.testimonials.html | safe }} <br />
|
||||||
|
{{ people.sources.html | safe }}
|
10
templates/people/submit.html
Normal file
10
templates/people/submit.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<head>
|
||||||
|
{{ form.media }}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="/p/submit/" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form }}
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
Loading…
Reference in a new issue