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.messages',
|
||||
'django.contrib.staticfiles',
|
||||
"django_quill"
|
||||
]
|
||||
|
||||
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):
|
||||
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)
|
||||
|
|
|
@ -3,5 +3,7 @@ from django.urls import path
|
|||
from . import views
|
||||
|
||||
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.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})
|
||||
|
|
|
@ -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
|
||||
|
|
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