refactor: Simplify Adoption Notice Forms
This commit is contained in:
@@ -22,67 +22,31 @@ class DateInput(forms.DateInput):
|
|||||||
input_type = 'date'
|
input_type = 'date'
|
||||||
|
|
||||||
|
|
||||||
class BulmaAdoptionNoticeForm(forms.ModelForm):
|
class AdoptionNoticeForm(forms.ModelForm):
|
||||||
template_name = "fellchensammlung/forms/form_snippets.html"
|
template_name = "fellchensammlung/forms/form_snippets.html"
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = AdoptionNotice
|
|
||||||
fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string",
|
|
||||||
"organization"]
|
|
||||||
|
|
||||||
|
|
||||||
class AdoptionNoticeForm(forms.ModelForm):
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
if 'in_adoption_notice_creation_flow' in kwargs:
|
|
||||||
in_flow = kwargs.pop('in_adoption_notice_creation_flow')
|
|
||||||
else:
|
|
||||||
in_flow = False
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.helper = FormHelper()
|
|
||||||
|
|
||||||
self.helper.form_id = 'form-adoption-notice'
|
|
||||||
self.helper.form_class = 'card'
|
|
||||||
self.helper.form_method = 'post'
|
|
||||||
|
|
||||||
if in_flow:
|
|
||||||
submit = Submit('save-and-add-another-animal', _('Speichern'))
|
|
||||||
|
|
||||||
else:
|
|
||||||
submit = Submit('submit', _('Speichern'))
|
|
||||||
|
|
||||||
self.helper.layout = Layout(
|
|
||||||
Fieldset(
|
|
||||||
_('Vermittlungsdetails'),
|
|
||||||
'name',
|
|
||||||
'species',
|
|
||||||
'num_animals',
|
|
||||||
'date_of_birth',
|
|
||||||
'sex',
|
|
||||||
'group_only',
|
|
||||||
'searching_since',
|
|
||||||
'location_string',
|
|
||||||
'organization',
|
|
||||||
'description',
|
|
||||||
'further_information',
|
|
||||||
),
|
|
||||||
submit)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = AdoptionNotice
|
|
||||||
fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string",
|
|
||||||
"organization"]
|
|
||||||
|
|
||||||
|
|
||||||
class AdoptionNoticeFormWithDateWidget(AdoptionNoticeForm):
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = AdoptionNotice
|
model = AdoptionNotice
|
||||||
fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string",
|
fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string",
|
||||||
"organization"]
|
"organization"]
|
||||||
widgets = {
|
widgets = {
|
||||||
'searching_since': DateInput(),
|
'searching_since': DateInput(format=('%Y-%m-%d')),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class AdoptionNoticeFormAutoAnimal(AdoptionNoticeForm):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(AdoptionNoticeFormAutoAnimal, self).__init__(*args, **kwargs)
|
||||||
|
self.fields["num_animals"] = forms.fields.IntegerField(min_value=1, max_value=30, label=_("Zahl der Tiere"))
|
||||||
|
animal_form = AnimalForm()
|
||||||
|
self.fields["species"] = animal_form.fields["species"]
|
||||||
|
self.fields["sex"] = animal_form.fields["sex"]
|
||||||
|
self.fields["date_of_birth"] = animal_form.fields["date_of_birth"]
|
||||||
|
self.fields["date_of_birth"].widget = DateInput(format=('%Y-%m-%d'))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AnimalForm(forms.ModelForm):
|
class AnimalForm(forms.ModelForm):
|
||||||
template_name = "fellchensammlung/forms/form_snippets.html"
|
template_name = "fellchensammlung/forms/form_snippets.html"
|
||||||
|
|
||||||
@@ -95,26 +59,6 @@ class AnimalForm(forms.ModelForm):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class AnimalFormWithDateWidget(AnimalForm):
|
|
||||||
class Meta:
|
|
||||||
model = Animal
|
|
||||||
fields = ["name", "date_of_birth", "species", "sex", "description"]
|
|
||||||
widgets = {
|
|
||||||
'date_of_birth': DateInput(),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class AdoptionNoticeFormWithDateWidgetAutoAnimal(AdoptionNoticeFormWithDateWidget):
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super(AdoptionNoticeFormWithDateWidgetAutoAnimal, self).__init__(*args, **kwargs)
|
|
||||||
self.fields["num_animals"] = forms.fields.IntegerField(min_value=1, max_value=30, label=_("Zahl der Tiere"))
|
|
||||||
animal_form = AnimalForm()
|
|
||||||
self.fields["species"] = animal_form.fields["species"]
|
|
||||||
self.fields["sex"] = animal_form.fields["sex"]
|
|
||||||
self.fields["date_of_birth"] = animal_form.fields["date_of_birth"]
|
|
||||||
self.fields["date_of_birth"].widget = DateInput()
|
|
||||||
|
|
||||||
|
|
||||||
class ImageForm(forms.ModelForm):
|
class ImageForm(forms.ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
if 'in_flow' in kwargs:
|
if 'in_flow' in kwargs:
|
||||||
@@ -179,7 +123,6 @@ class ModerationActionForm(forms.ModelForm):
|
|||||||
|
|
||||||
|
|
||||||
class CustomRegistrationForm(RegistrationForm):
|
class CustomRegistrationForm(RegistrationForm):
|
||||||
|
|
||||||
class Meta(RegistrationForm.Meta):
|
class Meta(RegistrationForm.Meta):
|
||||||
model = User
|
model = User
|
||||||
|
|
||||||
|
@@ -20,10 +20,8 @@ from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, Moderatio
|
|||||||
User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization, \
|
User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization, \
|
||||||
Species, Log, Timestamp, TrustLevel, SexChoicesWithAll, SearchSubscription, AdoptionNoticeNotification, \
|
Species, Log, Timestamp, TrustLevel, SexChoicesWithAll, SearchSubscription, AdoptionNoticeNotification, \
|
||||||
ImportantLocation
|
ImportantLocation
|
||||||
from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \
|
from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, \
|
||||||
CommentForm, ReportCommentForm, AnimalForm, \
|
CommentForm, ReportCommentForm, AnimalForm, AdoptionNoticeFormAutoAnimal
|
||||||
AdoptionNoticeSearchForm, AnimalFormWithDateWidget, AdoptionNoticeFormWithDateWidgetAutoAnimal, \
|
|
||||||
BulmaAdoptionNoticeForm
|
|
||||||
from .models import Language, Announcement
|
from .models import Language, Announcement
|
||||||
from .tools import i18n
|
from .tools import i18n
|
||||||
from .tools.geo import GeoAPI, zoom_level_for_radius
|
from .tools.geo import GeoAPI, zoom_level_for_radius
|
||||||
@@ -272,8 +270,8 @@ def search(request, templatename="fellchensammlung/search.html"):
|
|||||||
@login_required
|
@login_required
|
||||||
def add_adoption_notice(request):
|
def add_adoption_notice(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = AdoptionNoticeFormWithDateWidgetAutoAnimal(request.POST, request.FILES,
|
form = AdoptionNoticeFormAutoAnimal(request.POST, request.FILES,
|
||||||
in_adoption_notice_creation_flow=True)
|
in_adoption_notice_creation_flow=True)
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
an_instance = form.save(commit=False)
|
an_instance = form.save(commit=False)
|
||||||
@@ -307,18 +305,16 @@ def add_adoption_notice(request):
|
|||||||
|
|
||||||
return redirect(reverse("adoption-notice-detail", args=[an_instance.pk]))
|
return redirect(reverse("adoption-notice-detail", args=[an_instance.pk]))
|
||||||
else:
|
else:
|
||||||
form = AdoptionNoticeFormWithDateWidgetAutoAnimal(in_adoption_notice_creation_flow=True)
|
form = AdoptionNoticeFormAutoAnimal(in_adoption_notice_creation_flow=True)
|
||||||
return render(request, 'fellchensammlung/forms/form_add_adoption.html', {'form': form})
|
return render(request, 'fellchensammlung/forms/form_add_adoption.html', {'form': form})
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def add_adoption_notice_bulma(request):
|
def add_adoption_notice_bulma(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
print("dada")
|
form = AdoptionNoticeFormAutoAnimal(request.POST)
|
||||||
form = AdoptionNoticeFormWithDateWidgetAutoAnimal(request.POST)
|
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
print("dodo")
|
|
||||||
an_instance = form.save(commit=False)
|
an_instance = form.save(commit=False)
|
||||||
an_instance.owner = request.user
|
an_instance.owner = request.user
|
||||||
|
|
||||||
@@ -352,7 +348,7 @@ def add_adoption_notice_bulma(request):
|
|||||||
else:
|
else:
|
||||||
print(form.errors)
|
print(form.errors)
|
||||||
else:
|
else:
|
||||||
form = AdoptionNoticeFormWithDateWidgetAutoAnimal()
|
form = AdoptionNoticeFormAutoAnimal()
|
||||||
return render(request, 'fellchensammlung/forms/bulma-form-add-adoption.html', {'form': form})
|
return render(request, 'fellchensammlung/forms/bulma-form-add-adoption.html', {'form': form})
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from fellchensammlung.forms import AdoptionNoticeFormWithDateWidgetAutoAnimal
|
from fellchensammlung.forms import AdoptionNoticeFormAutoAnimal
|
||||||
from fellchensammlung.models import Species
|
from fellchensammlung.models import Species
|
||||||
from model_bakery import baker
|
from model_bakery import baker
|
||||||
|
|
||||||
@@ -21,5 +21,5 @@ class TestAdoptionNoticeFormWithDateWidgetAutoAnimal(TestCase):
|
|||||||
"description": "Blaaaa",
|
"description": "Blaaaa",
|
||||||
"further_information": "https://notfellchen.org",
|
"further_information": "https://notfellchen.org",
|
||||||
"save-and-add-another-animal": "Speichern"}
|
"save-and-add-another-animal": "Speichern"}
|
||||||
form = AdoptionNoticeFormWithDateWidgetAutoAnimal(data=form_data)
|
form = AdoptionNoticeFormAutoAnimal(data=form_data)
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
|
Reference in New Issue
Block a user