feat: add add-animal form
This commit is contained in:
parent
ad4757cb22
commit
76c24ba694
@ -43,11 +43,19 @@ class AdoptionNoticeForm(forms.ModelForm):
|
|||||||
|
|
||||||
class AnimalForm(forms.ModelForm):
|
class AnimalForm(forms.ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
if 'adding_to_adoption_notice' in kwargs:
|
||||||
|
adding = kwargs.pop('adding_to_adoption_notice')
|
||||||
|
else:
|
||||||
|
adding = False
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.helper = FormHelper()
|
self.helper = FormHelper()
|
||||||
self.helper.form_class = 'form-animal'
|
self.helper.form_class = 'form-animal'
|
||||||
self.helper.add_input(Submit('save-and-add-another-animal', _('Speichern und weiteres Tier hinzufügen')))
|
if adding:
|
||||||
self.helper.add_input(Submit('save-and-finish', _('Speichern und beenden')))
|
self.helper.add_input(Submit('save-and-add-another-animal', _('Speichern und weiteres Tier hinzufügen')))
|
||||||
|
self.helper.add_input(Submit('save-and-finish', _('Speichern und beenden')))
|
||||||
|
else:
|
||||||
|
self.helper.add_input(Submit('submit', _('Speichern')))
|
||||||
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Animal
|
model = Animal
|
||||||
|
@ -12,7 +12,7 @@ urlpatterns = [
|
|||||||
# ex: /animal/5/
|
# ex: /animal/5/
|
||||||
path("tier/<int:animal_id>/", views.animal_detail, name="animal-detail"),
|
path("tier/<int:animal_id>/", views.animal_detail, name="animal-detail"),
|
||||||
# ex: /animal/5/edit
|
# ex: /animal/5/edit
|
||||||
path("tier<int:animal_id>/edit", views.animal_edit, name="animal-edit"),
|
path("tier/<int:animal_id>/edit", views.animal_edit, name="animal-edit"),
|
||||||
# ex: /adoption_notice/7/
|
# ex: /adoption_notice/7/
|
||||||
path("vermittlung/<int:adoption_notice_id>/", views.adoption_notice_detail, name="adoption-notice-detail"),
|
path("vermittlung/<int:adoption_notice_id>/", views.adoption_notice_detail, name="adoption-notice-detail"),
|
||||||
# ex: /adoption_notice/7/edit
|
# ex: /adoption_notice/7/edit
|
||||||
|
@ -116,7 +116,7 @@ def adoption_notice_add_animal(request, adoption_notice_id):
|
|||||||
else:
|
else:
|
||||||
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html')
|
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html')
|
||||||
else:
|
else:
|
||||||
form = AnimalForm()
|
form = AnimalForm(adding_to_adoption_notice=True)
|
||||||
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html', {'form': form})
|
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html', {'form': form})
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@ -124,45 +124,17 @@ def animal_edit(request, animal_id):
|
|||||||
"""
|
"""
|
||||||
View implements the following methods
|
View implements the following methods
|
||||||
* Updating an Animal
|
* Updating an Animal
|
||||||
* Adding photos to an animal
|
|
||||||
"""
|
"""
|
||||||
|
animal = Animal.objects.get(pk=animal_id)
|
||||||
def delete_photo():
|
|
||||||
print("Photo deleted")
|
|
||||||
|
|
||||||
def save_photo():
|
|
||||||
print("Photo save")
|
|
||||||
|
|
||||||
def add_photo():
|
|
||||||
print("Photo added")
|
|
||||||
|
|
||||||
def save_animal():
|
|
||||||
print("Animal saved")
|
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = AnimalForm(request.POST, animal_id=animal_id, )
|
form = AnimalForm(request.POST, instance=animal)
|
||||||
for key in request.POST:
|
|
||||||
if key.startswith("delete_photo_"):
|
|
||||||
action = delete_photo
|
|
||||||
if key.startswith("save_photo_"):
|
|
||||||
action = save_photo
|
|
||||||
if key.startswith("add_photo"):
|
|
||||||
action = add_photo
|
|
||||||
if key.startswith("save_animal"):
|
|
||||||
action = save_animal
|
|
||||||
|
|
||||||
pk = key.split("_")[-1]
|
|
||||||
|
|
||||||
action(animal_id, pk, form_data=request.POST)
|
|
||||||
|
|
||||||
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
|
|
||||||
{'form': form})
|
|
||||||
|
|
||||||
|
if form.is_valid():
|
||||||
|
animal = form.save()
|
||||||
|
return redirect(reverse("animal-detail", args=[animal.pk], ))
|
||||||
else:
|
else:
|
||||||
form = AnimalForm(animal_id)
|
form = AnimalForm(instance=animal)
|
||||||
image_form = ImageForm(request.POST, request.FILES, prefix="image_")
|
return render(request, 'fellchensammlung/forms/form-adoption-notice.html', context={"form": form})
|
||||||
return render(request, 'fellchensammlung/forms/form_add_animal_to_adoption.html',
|
|
||||||
{'form': form})
|
|
||||||
|
|
||||||
|
|
||||||
def about(request):
|
def about(request):
|
||||||
|
Loading…
Reference in New Issue
Block a user