Add confirmation form for Child delete.

This commit is contained in:
Christopher Charbonneau Wells 2017-10-31 20:49:10 -04:00
parent c565bfc59a
commit 492bcddeb4
3 changed files with 33 additions and 12 deletions

View File

@ -49,6 +49,25 @@ class ChildForm(forms.ModelForm):
} }
class ChildDeleteForm(forms.ModelForm):
confirm_name = forms.CharField(max_length=511)
class Meta:
model = Child
fields = []
def clean_confirm_name(self):
confirm_name = self.cleaned_data['confirm_name']
if confirm_name != str(self.instance):
raise forms.ValidationError('Name does not match child name.')
return confirm_name
def save(self, commit=True):
instance = self.instance
self.instance.delete()
return instance
class DiaperChangeForm(forms.ModelForm): class DiaperChangeForm(forms.ModelForm):
class Meta: class Meta:
model = DiaperChange model = DiaperChange

View File

@ -14,8 +14,15 @@
{% csrf_token %} {% csrf_token %}
<h1>Are you sure you want to delete <span class="text-info">{{ object }}</span>?</h1> <h1>Are you sure you want to delete <span class="text-info">{{ object }}</span>?</h1>
<div class="form-group"> <div class="form-group">
<label for="confirm_name">To confirm this action. Type the full name of the child below.</label> <label for="{{ form.confirm_name.id_for_label }}">To confirm this action. Type the full name of the child below.</label>
<input type="text" id="confirm_name" name="confirm_name" class="form-control" placeholder="{{ object }}" /> {% if form.confirm_name.errors %}
{{ form.confirm_name|add_class:"form-control is-invalid" }}
{% else %}
{{ form.confirm_name|add_class:"form-control" }}
{% endif %}
{% if form.confirm_name.errors %}
<div class="invalid-feedback">{{ form.confirm_name.errors.0 }}</div>
{% endif %}
</div> </div>
<input type="submit" value="Delete" class="btn btn-danger" /> <input type="submit" value="Delete" class="btn btn-danger" />
<a href="{% url 'child-list' %}" class="btn btn-default">Cancel</a> <a href="{% url 'child-list' %}" class="btn btn-default">Cancel</a>

View File

@ -2,7 +2,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.http import HttpResponseRedirect
from django.urls import reverse from django.urls import reverse
from django.views.generic.base import RedirectView from django.views.generic.base import RedirectView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
@ -10,8 +9,8 @@ from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from .models import Child, DiaperChange, Feeding, Note, Sleep, Timer, TummyTime from .models import Child, DiaperChange, Feeding, Note, Sleep, Timer, TummyTime
from .forms import (ChildForm, DiaperChangeForm, FeedingForm, SleepForm, from .forms import (ChildForm, ChildDeleteForm, DiaperChangeForm, FeedingForm,
TimerForm, TummyTimeForm) SleepForm, TimerForm, TummyTimeForm)
class ChildList(PermissionRequiredMixin, ListView): class ChildList(PermissionRequiredMixin, ListView):
@ -39,17 +38,13 @@ class ChildUpdate(PermissionRequiredMixin, UpdateView):
success_url = '/children' success_url = '/children'
class ChildDelete(PermissionRequiredMixin, DeleteView): class ChildDelete(PermissionRequiredMixin, UpdateView):
model = Child model = Child
form_class = ChildDeleteForm
template_name = 'core/child_confirm_delete.html'
permission_required = ('core.delete_child',) permission_required = ('core.delete_child',)
success_url = '/children' success_url = '/children'
def post(self, request, *args, **kwargs):
if str(self.get_object()) != self.request.POST.get('confirm_name'):
# TODO: Provide some error feedback.
return HttpResponseRedirect(self.request.path)
return self.delete(request, *args, **kwargs)
class DiaperChangeList(PermissionRequiredMixin, ListView): class DiaperChangeList(PermissionRequiredMixin, ListView):
model = DiaperChange model = DiaperChange