format files with black

This commit is contained in:
Aarni Halinen
2022-01-13 22:10:24 +02:00
parent a0f062c697
commit 11efcdd579
178 changed files with 3763 additions and 2324 deletions
+51 -26
View File
@@ -6,12 +6,16 @@ from kaehmy.models import PresetRole, CustomRole, Application, Comment, KaehmyBa
class CheckboxSelectMultiple(forms.widgets.CheckboxSelectMultiple):
option_template_name = 'checkbox_option.html'
option_template_name = "checkbox_option.html"
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
dic = super(CheckboxSelectMultiple, self).create_option(name, value, label, selected, index, subindex, attrs)
def create_option(
self, name, value, label, selected, index, subindex=None, attrs=None
):
dic = super(CheckboxSelectMultiple, self).create_option(
name, value, label, selected, index, subindex, attrs
)
description = PresetRole.objects.get(id=value).description
dic['description'] = description
dic["description"] = description
return dic
def __init__(self, *args, **kwargs):
@@ -25,30 +29,46 @@ class ApplicationForm(forms.ModelForm):
"""Meta for class Application."""
model = Application
fields = ['name', 'email', 'phone_number', 'year',
'preset_roles', 'custom_roles', 'custom_role_name',
'custom_role_is_board', 'text']
fields = [
"name",
"email",
"phone_number",
"year",
"preset_roles",
"custom_roles",
"custom_role_name",
"custom_role_is_board",
"text",
]
def __init__(self, *args, **kwargs):
super(ApplicationForm, self).__init__(*args, **kwargs)
self.fields["email"].label = _('Email (not public)')
self.fields["phone_number"].label = _('Phone number (not public)')
self.fields["email"].label = _("Email (not public)")
self.fields["phone_number"].label = _("Phone number (not public)")
custom_roles_exist = CustomRole.objects.all().exists()
self.fields["custom_roles"].widget = forms.widgets.CheckboxSelectMultiple() if custom_roles_exist else forms.HiddenInput()
self.fields["custom_roles"].widget = (
forms.widgets.CheckboxSelectMultiple()
if custom_roles_exist
else forms.HiddenInput()
)
self.fields["custom_roles"].help_text = ""
self.fields["custom_roles"].label = _('Custom roles')
self.fields["custom_roles"].label = _("Custom roles")
self.fields["custom_roles"].queryset = CustomRole.objects.all()
for cat_id, category in KaehmyBaseRole.CATEGORIES:
key = 'preset_roles_{}'.format(cat_id)
qset = PresetRole.objects.filter(category=cat_id).order_by('category', '-is_board')
key = "preset_roles_{}".format(cat_id)
qset = PresetRole.objects.filter(category=cat_id).order_by(
"category", "-is_board"
)
self.fields[key] = forms.ModelMultipleChoiceField(qset)
self.fields[key].widget = CheckboxSelectMultiple(attrs={
'title': _('Preset roles'),
'name': 'preset_roles',
})
self.fields[key].widget = CheckboxSelectMultiple(
attrs={
"title": _("Preset roles"),
"name": "preset_roles",
}
)
self.fields[key].help_text = ""
self.fields[key].queryset = qset
self.fields[key].label = _(category)
@@ -57,33 +77,38 @@ class ApplicationForm(forms.ModelForm):
def clean(self):
cleaned_data = super(ApplicationForm, self).clean()
for key in cleaned_data.keys():
if 'preset_roles_' in key:
cleaned_data['preset_roles'] = cleaned_data['preset_roles'] | cleaned_data[key]
if "preset_roles_" in key:
cleaned_data["preset_roles"] = (
cleaned_data["preset_roles"] | cleaned_data[key]
)
return cleaned_data
def clean_phone_number(self):
"""Clean phone number field."""
number = self.cleaned_data.get('phone_number')
number = self.cleaned_data.get("phone_number")
if number.isdigit():
return number
else:
raise ValidationError(_('Invalid phone number'))
raise ValidationError(_("Invalid phone number"))
def clean_custom_role_name(self):
"""Check that no other custom role with same name exists."""
custom_name = self.cleaned_data.get('custom_role_name')
custom_name = self.cleaned_data.get("custom_role_name")
if not CustomRole.objects.filter(name=custom_name).exists():
return custom_name
else:
raise ValidationError(_('Custom role with the same name already exists.'))
raise ValidationError(_("Custom role with the same name already exists."))
def non_role_fields(self):
return [self.fields[k] for k in self.fields.keys() if k not in ["preset_roles", "custom_roles"]]
return [
self.fields[k]
for k in self.fields.keys()
if k not in ["preset_roles", "custom_roles"]
]
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name', 'email', 'message', 'parent']
fields = ["name", "email", "message", "parent"]