Move widget to forms.py
This commit is contained in:
parent
af8403f7ce
commit
ad74254593
2 changed files with 14 additions and 16 deletions
|
|
@ -1,8 +1,21 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.forms.widgets import CheckboxSelectMultiple
|
||||||
|
|
||||||
from newsreader.news.collection.models import CollectionRule
|
from newsreader.news.collection.models import CollectionRule
|
||||||
from newsreader.news.core.models import Category
|
from newsreader.news.core.models import Category
|
||||||
from newsreader.news.core.widgets import RulesWidget
|
|
||||||
|
|
||||||
|
class RulesWidget(CheckboxSelectMultiple):
|
||||||
|
template_name = "news/core/widgets/rules.html"
|
||||||
|
option_template_name = "news/core/widgets/rule.html"
|
||||||
|
|
||||||
|
def create_option(self, *args, **kwargs):
|
||||||
|
option = super().create_option(*args, **kwargs)
|
||||||
|
instance = self.choices.queryset.get(pk=option["value"])
|
||||||
|
|
||||||
|
if self.category and instance.category:
|
||||||
|
option["selected"] = self.category.pk == instance.category.pk
|
||||||
|
return {**option, "instance": instance}
|
||||||
|
|
||||||
|
|
||||||
class CategoryForm(forms.ModelForm):
|
class CategoryForm(forms.ModelForm):
|
||||||
|
|
@ -16,7 +29,6 @@ class CategoryForm(forms.ModelForm):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.fields["rules"].queryset = CollectionRule.objects.filter(user=self.user)
|
self.fields["rules"].queryset = CollectionRule.objects.filter(user=self.user)
|
||||||
|
|
||||||
self.fields["rules"].widget.category = self.instance
|
self.fields["rules"].widget.category = self.instance
|
||||||
|
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
|
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
from django.forms.widgets import CheckboxSelectMultiple
|
|
||||||
|
|
||||||
|
|
||||||
class RulesWidget(CheckboxSelectMultiple):
|
|
||||||
template_name = "news/core/widgets/rules.html"
|
|
||||||
option_template_name = "news/core/widgets/rule.html"
|
|
||||||
|
|
||||||
def create_option(self, *args, **kwargs):
|
|
||||||
option = super().create_option(*args, **kwargs)
|
|
||||||
instance = self.choices.queryset.get(pk=option["value"])
|
|
||||||
|
|
||||||
if self.category and instance.category:
|
|
||||||
option["selected"] = self.category.pk == instance.category.pk
|
|
||||||
return {**option, "instance": instance}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue