2024-03-20 09:19:58 +01:00
|
|
|
from django import forms
|
2024-05-30 09:57:20 +02:00
|
|
|
|
2024-05-30 15:46:51 +02:00
|
|
|
from .models import AdoptionNotice, Animal, Image, ReportAdoptionNotice, ReportComment, ModerationAction, User, Species, \
|
|
|
|
Comment
|
2024-04-07 11:33:41 +02:00
|
|
|
from django_registration.forms import RegistrationForm
|
2024-04-20 08:06:20 +02:00
|
|
|
from crispy_forms.helper import FormHelper
|
2024-10-10 23:19:17 +02:00
|
|
|
from crispy_forms.layout import Submit, Layout, Fieldset, HTML, Row, Column, Field, Hidden
|
2024-04-20 08:06:20 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-05-10 13:54:16 +02:00
|
|
|
from notfellchen.settings import MEDIA_URL
|
|
|
|
|
2024-03-20 09:19:58 +01:00
|
|
|
|
2024-11-14 18:30:51 +01:00
|
|
|
def animal_validator(value: str):
|
|
|
|
value = value.lower()
|
|
|
|
animal_list = ["ratte", "farbratte", "katze", "hund", "kaninchen", "hase", "kuh", "fuchs", "cow", "rat", "cat",
|
|
|
|
"dog", "rabbit", "fox"]
|
|
|
|
if value not in animal_list:
|
|
|
|
raise forms.ValidationError(_("Dieses Tier kenne ich nicht. Probier ein anderes"))
|
|
|
|
|
|
|
|
|
2024-03-20 09:19:58 +01:00
|
|
|
class DateInput(forms.DateInput):
|
|
|
|
input_type = 'date'
|
|
|
|
|
|
|
|
|
|
|
|
class AdoptionNoticeForm(forms.ModelForm):
|
2024-04-20 08:06:20 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2024-05-31 09:58:55 +02:00
|
|
|
if 'in_adoption_notice_creation_flow' in kwargs:
|
|
|
|
in_flow = kwargs.pop('in_adoption_notice_creation_flow')
|
|
|
|
else:
|
|
|
|
in_flow = False
|
2024-04-20 08:06:20 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
2024-04-20 08:40:04 +02:00
|
|
|
|
|
|
|
self.helper.form_id = 'form-adoption-notice'
|
2024-04-20 08:06:20 +02:00
|
|
|
self.helper.form_class = 'card'
|
|
|
|
self.helper.form_method = 'post'
|
|
|
|
|
2024-05-31 09:58:55 +02:00
|
|
|
if in_flow:
|
2024-09-29 13:38:56 +02:00
|
|
|
submit = Submit('save-and-add-another-animal', _('Speichern'))
|
2024-05-31 09:58:55 +02:00
|
|
|
|
|
|
|
else:
|
2024-09-30 15:22:19 +02:00
|
|
|
submit = Submit('submit', _('Speichern'))
|
2024-05-31 09:58:55 +02:00
|
|
|
|
2024-04-20 08:40:16 +02:00
|
|
|
self.helper.layout = Layout(
|
|
|
|
Fieldset(
|
|
|
|
_('Vermittlungsdetails'),
|
|
|
|
'name',
|
2024-09-29 13:38:56 +02:00
|
|
|
'species',
|
|
|
|
'num_animals',
|
|
|
|
'date_of_birth',
|
|
|
|
'sex',
|
2024-04-20 08:40:16 +02:00
|
|
|
'group_only',
|
|
|
|
'searching_since',
|
2024-05-31 10:58:57 +02:00
|
|
|
'location_string',
|
2024-11-14 21:11:34 +01:00
|
|
|
'organization',
|
2024-04-20 08:40:16 +02:00
|
|
|
'description',
|
|
|
|
'further_information',
|
|
|
|
),
|
2024-05-31 09:58:55 +02:00
|
|
|
submit)
|
2024-04-20 08:06:20 +02:00
|
|
|
|
2024-03-20 09:19:58 +01:00
|
|
|
class Meta:
|
|
|
|
model = AdoptionNotice
|
2024-11-14 21:11:34 +01:00
|
|
|
fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string",
|
|
|
|
"organization"]
|
2024-05-30 10:48:57 +02:00
|
|
|
|
2024-03-20 09:19:58 +01:00
|
|
|
|
2024-06-08 12:31:22 +02:00
|
|
|
class AdoptionNoticeFormWithDateWidget(AdoptionNoticeForm):
|
|
|
|
class Meta:
|
|
|
|
model = AdoptionNotice
|
2024-11-14 21:11:34 +01:00
|
|
|
fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string", "organization"]
|
2024-06-08 12:31:22 +02:00
|
|
|
widgets = {
|
|
|
|
'searching_since': DateInput(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-30 15:49:32 +02:00
|
|
|
class AnimalForm(forms.ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2024-05-31 09:58:55 +02:00
|
|
|
if 'in_adoption_notice_creation_flow' in kwargs:
|
|
|
|
adding = kwargs.pop('in_adoption_notice_creation_flow')
|
2024-05-30 16:57:47 +02:00
|
|
|
else:
|
|
|
|
adding = False
|
2024-05-30 15:49:32 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
2024-08-04 14:31:23 +02:00
|
|
|
self.helper.form_class = 'form-animal card'
|
2024-05-30 16:57:47 +02:00
|
|
|
if adding:
|
|
|
|
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:
|
2024-08-04 14:31:23 +02:00
|
|
|
self.helper.add_input(Submit('submit', _('Speichern'), css_class="btn"))
|
2024-05-30 16:57:47 +02:00
|
|
|
|
2024-05-30 15:49:32 +02:00
|
|
|
class Meta:
|
|
|
|
model = Animal
|
|
|
|
fields = ["name", "date_of_birth", "species", "sex", "description"]
|
|
|
|
|
|
|
|
|
2024-06-08 12:31:22 +02:00
|
|
|
class AnimalFormWithDateWidget(AnimalForm):
|
|
|
|
class Meta:
|
|
|
|
model = Animal
|
|
|
|
fields = ["name", "date_of_birth", "species", "sex", "description"]
|
|
|
|
widgets = {
|
|
|
|
'date_of_birth': DateInput(),
|
|
|
|
}
|
|
|
|
|
2024-11-14 18:30:51 +01:00
|
|
|
|
2024-09-29 13:38:56 +02:00
|
|
|
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()
|
|
|
|
|
2024-06-08 12:31:22 +02:00
|
|
|
|
2024-03-20 13:40:00 +01:00
|
|
|
class ImageForm(forms.ModelForm):
|
2024-05-10 13:54:16 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2024-05-31 10:56:43 +02:00
|
|
|
if 'in_flow' in kwargs:
|
|
|
|
in_flow = kwargs.pop('in_flow')
|
|
|
|
else:
|
|
|
|
in_flow = False
|
2024-05-10 13:54:16 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_id = 'form-animal-photo'
|
|
|
|
self.helper.form_class = 'card'
|
|
|
|
self.helper.form_method = 'post'
|
2024-05-31 10:56:43 +02:00
|
|
|
if in_flow:
|
|
|
|
self.helper.add_input(Submit('save-and-add-another', _('Speichern und weiteres Foto hinzufügen')))
|
|
|
|
self.helper.add_input(Submit('submit', _('Speichern')))
|
|
|
|
else:
|
|
|
|
self.helper.add_input(Submit('submit', _('Submit')))
|
2024-05-10 13:54:16 +02:00
|
|
|
|
2024-03-20 13:40:00 +01:00
|
|
|
class Meta:
|
|
|
|
model = Image
|
2024-05-31 13:51:14 +02:00
|
|
|
fields = ('image', 'alt_text')
|
2024-03-22 12:45:50 +01:00
|
|
|
|
|
|
|
|
2024-05-30 15:46:51 +02:00
|
|
|
class ReportAdoptionNoticeForm(forms.ModelForm):
|
2024-03-22 12:45:50 +01:00
|
|
|
class Meta:
|
2024-05-30 15:46:51 +02:00
|
|
|
model = ReportAdoptionNotice
|
|
|
|
fields = ('reported_broken_rules', 'user_comment')
|
|
|
|
|
|
|
|
|
|
|
|
class ReportCommentForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = ReportComment
|
|
|
|
fields = ('reported_broken_rules', 'user_comment')
|
2024-03-22 12:45:50 +01:00
|
|
|
|
2024-05-30 14:39:28 +02:00
|
|
|
|
2024-05-30 14:37:15 +02:00
|
|
|
class CommentForm(forms.ModelForm):
|
2024-05-30 14:39:28 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_class = 'form-comments'
|
2024-10-10 23:19:17 +02:00
|
|
|
self.helper.add_input(Hidden('action', 'comment'))
|
2024-08-04 14:41:14 +02:00
|
|
|
self.helper.add_input(Submit('submit', _('Kommentieren'), css_class="btn2"))
|
2024-05-30 14:39:28 +02:00
|
|
|
|
2024-05-30 14:37:15 +02:00
|
|
|
class Meta:
|
|
|
|
model = Comment
|
|
|
|
fields = ('text',)
|
2024-03-22 12:45:50 +01:00
|
|
|
|
2024-05-30 14:39:28 +02:00
|
|
|
|
2024-03-22 12:45:50 +01:00
|
|
|
class ModerationActionForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = ModerationAction
|
|
|
|
fields = ('action', 'public_comment', 'private_comment')
|
2024-04-07 11:33:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CustomRegistrationForm(RegistrationForm):
|
2024-04-07 11:41:37 +02:00
|
|
|
class Meta(RegistrationForm.Meta):
|
|
|
|
model = User
|
2024-05-31 13:48:24 +02:00
|
|
|
|
2024-11-14 18:30:51 +01:00
|
|
|
captcha = forms.CharField(validators=[animal_validator], label=_("Nenne ein Tier (Captcha)"))
|
|
|
|
|
2024-08-03 09:40:53 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_id = 'form-registration'
|
|
|
|
self.helper.form_class = 'card'
|
|
|
|
|
|
|
|
self.helper.add_input(Submit('submit', _('Registrieren'), css_class="btn"))
|
|
|
|
|
2024-05-31 13:48:24 +02:00
|
|
|
|
|
|
|
def _get_distances():
|
2024-11-14 18:30:51 +01:00
|
|
|
return {i: i for i in [20, 50, 100, 200, 500]}
|
2024-05-31 13:48:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AdoptionNoticeSearchForm(forms.Form):
|
2024-10-29 17:49:54 +01:00
|
|
|
location = forms.CharField(max_length=20, label=_("Stadt"))
|
2024-05-31 13:48:24 +02:00
|
|
|
max_distance = forms.ChoiceField(choices=_get_distances, label=_("Max. Distanz"))
|