54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
import pytz
|
|
|
|
from newsreader.news.collection.models import CollectionRule
|
|
from newsreader.news.core.models import Category
|
|
|
|
|
|
class CollectionRuleForm(forms.ModelForm):
|
|
category = forms.ModelChoiceField(required=False, queryset=Category.objects.all())
|
|
timezone = forms.ChoiceField(
|
|
widget=forms.Select(attrs={"size": len(pytz.all_timezones)}),
|
|
choices=((timezone, timezone) for timezone in pytz.all_timezones),
|
|
help_text=_("The timezone which the feed uses"),
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.user = kwargs.pop("user")
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if self.user:
|
|
self.fields["category"].queryset = Category.objects.filter(user=self.user)
|
|
|
|
def save(self, commit=True):
|
|
instance = super().save(commit=False)
|
|
instance.user = self.user
|
|
|
|
if commit:
|
|
instance.save()
|
|
self.save_m2m()
|
|
|
|
return instance
|
|
|
|
class Meta:
|
|
model = CollectionRule
|
|
fields = ("name", "url", "timezone", "favicon", "category")
|
|
|
|
|
|
class CollectionRuleBulkForm(forms.Form):
|
|
rules = forms.ModelMultipleChoiceField(queryset=CollectionRule.objects.none())
|
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
self.user = user
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["rules"].queryset = CollectionRule.objects.filter(user=user)
|
|
|
|
|
|
class OPMLImportForm(forms.Form):
|
|
file = forms.FileField(allow_empty_file=False)
|
|
skip_existing = forms.BooleanField(initial=False, required=False)
|