Fixed bugs 2 #15
@ -92,10 +92,43 @@ class CategoryForm(forms.ModelForm):
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
self.fields["parent"].queryset = Category.objects.filter(
|
queryset = Category.objects.filter(owner=user)
|
||||||
owner=user,
|
|
||||||
|
if self.instance.pk:
|
||||||
|
queryset = queryset.exclude(
|
||||||
|
pk__in=self._self_and_descendant_ids(self.instance)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.fields["parent"].queryset = queryset
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _self_and_descendant_ids(category):
|
||||||
|
ids = [category.pk]
|
||||||
|
pending = [category.pk]
|
||||||
|
|
||||||
|
while pending:
|
||||||
|
children = list(
|
||||||
|
Category.objects.filter(parent_id__in=pending)
|
||||||
|
.exclude(pk__in=ids)
|
||||||
|
.values_list("pk", flat=True)
|
||||||
|
)
|
||||||
|
ids.extend(children)
|
||||||
|
pending = children
|
||||||
|
|
||||||
|
return ids
|
||||||
|
|
||||||
|
def clean_parent(self):
|
||||||
|
parent = self.cleaned_data.get("parent")
|
||||||
|
|
||||||
|
if parent and self.instance.pk:
|
||||||
|
if parent.pk in self._self_and_descendant_ids(self.instance):
|
||||||
|
raise forms.ValidationError(
|
||||||
|
"Una categoría no puede ser su propio padre ni depender "
|
||||||
|
"de una de sus subcategorías."
|
||||||
|
)
|
||||||
|
|
||||||
|
return parent
|
||||||
|
|
||||||
|
|
||||||
class GoalForm(forms.ModelForm):
|
class GoalForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user