Commit 0bfcf316 authored by Dorian Lesbre's avatar Dorian Lesbre
Browse files

Fix typing errors introduced by new mypy version

parent 4fab358c
Pipeline #1821 passed with stage
in 1 minute and 17 seconds
from typing import Any, Mapping, Optional, Union
from typing import Any, Optional, TypeAlias, Union, cast
from django import forms
from django.contrib.auth.models import Group, User
......@@ -42,10 +42,11 @@ class FolderForm(forms.ModelForm):
def __init__(self, instance=None, **kwargs) -> None:
"""Override init to set smart values for parent.choices"""
super().__init__(instance=instance, **kwargs)
iterator = sorted(self.fields["parent"].choices, key=self.sort_key)
parent = cast(Any, self.fields["parent"])
iterator = sorted(parent.choices, key=self.sort_key)
if self.instance:
iterator = list(filter(self.filter, iterator))
self.fields["parent"].choices = iterator
parent.choices = iterator
class GalleryForm(forms.ModelForm):
......@@ -103,7 +104,8 @@ class GroupPermissionForm(forms.Form):
gallery.can_edit_groups.add(group)
GroupPermissionFormset = forms.formset_factory(
GroupFormsetType: TypeAlias = "forms.BaseFormSet[GroupPermissionForm]"
GroupPermissionFormset: type[GroupFormsetType] = forms.formset_factory(
GroupPermissionForm, extra=0, can_order=False, can_delete=False
)
......@@ -122,7 +124,7 @@ class UserPermissionForm(forms.Form):
user_obj: User
def clean(self) -> Mapping[str, Any]:
def clean(self) -> dict[str, Any]:
"""Checks that the text field user actually designates a user
and sets user_obj"""
username = self.cleaned_data.get("user")
......@@ -160,7 +162,8 @@ class UserPermissionForm(forms.Form):
gallery.can_edit_users.add(self.user_obj)
UserPermissionFormset = forms.formset_factory(
UserFormsetType: TypeAlias = "forms.BaseFormSet[UserPermissionForm]"
UserPermissionFormset: type[UserFormsetType] = forms.formset_factory(
UserPermissionForm, extra=2, can_order=False, can_delete=True
)
......
......@@ -16,8 +16,10 @@ from config.constants import KEY_CHARS, KEY_LENGTH, KEY_QUERY_PARAM_NAME, KEY_VA
from .forms import (
FolderForm,
GalleryForm,
GroupFormsetType,
GroupPermissionFormset,
PhotoUploadForm,
UserFormsetType,
UserPermissionFormset,
)
from .graphics import create_photo
......@@ -236,16 +238,12 @@ class GalleryFormView(CanAccessMixin, View):
soit vide, soit avec le gallerie à modifier, soit avec les données POST reçues"""
raise NotImplementedError()
def get_group_formset(
self, post: Optional[Mapping[str, Any]]
) -> GroupPermissionFormset:
def get_group_formset(self, post: Optional[Mapping[str, Any]]) -> GroupFormsetType:
"""Initialize le formset de permissions de groupes
Il doit contenir tout les groupes."""
raise NotImplementedError()
def get_user_formset(
self, post: Optional[Mapping[str, Any]]
) -> UserPermissionFormset:
def get_user_formset(self, post: Optional[Mapping[str, Any]]) -> UserFormsetType:
"""Initialize le formset de permissions d'utilisateurs
Il doit contenir tout les groupes."""
return UserPermissionFormset(post, prefix=self.user_form_prefix)
......@@ -311,7 +309,7 @@ class GalleryCreate(GalleryFormView, FolderAccessMixin):
return GalleryForm(post)
return GalleryForm(initial={"parent": self.obj})
def get_group_formset(self, post) -> GroupPermissionFormset:
def get_group_formset(self, post) -> GroupFormsetType:
"""Special initial values for groups"""
if post is not None:
return GroupPermissionFormset(post, prefix=self.group_form_prefix)
......@@ -354,9 +352,7 @@ class GalleryEdit(GalleryFormView, GalleryAccessMixin):
def get_form(self, post: Optional[Mapping[str, Any]]) -> GalleryForm:
return GalleryForm(post, instance=self.obj)
def get_group_formset(
self, post: Optional[Mapping[str, Any]]
) -> GroupPermissionFormset:
def get_group_formset(self, post: Optional[Mapping[str, Any]]) -> GroupFormsetType:
gallery = self.obj
return GroupPermissionFormset(
post,
......@@ -373,9 +369,7 @@ class GalleryEdit(GalleryFormView, GalleryAccessMixin):
prefix=self.group_form_prefix,
)
def get_user_formset(
self, post: Optional[Mapping[str, Any]]
) -> UserPermissionFormset:
def get_user_formset(self, post: Optional[Mapping[str, Any]]) -> UserFormsetType:
if post is not None:
return UserPermissionFormset(post, prefix=self.user_form_prefix)
gallery = self.obj
......@@ -414,7 +408,7 @@ class PhotoUploadView(GalleryAccessMixin, FormView):
failed: list[str] = []
for file in files:
photo = create_photo(file, self.obj, file.name)
if photo is None:
if photo is None and file.name is not None:
failed.append(file.name)
else:
created += 1
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment