from django import forms from apps.core.urlutils import http_url_or_empty from .models import Subscription from .opml import MAX_UPLOAD_BYTES class AddFeedForm(forms.Form): feed_url = forms.URLField(label="Feed URL", max_length=1000, assume_scheme="https") def clean_feed_url(self): # URLField also accepts ftp:// and ftps://; feeds are only ever # fetched over http(s). url = http_url_or_empty(self.cleaned_data["feed_url"]) if not url: raise forms.ValidationError("Enter a feed URL starting with http:// or https://.") return url tag_names = forms.CharField( label="Tags", max_length=500, required=False, help_text="Comma-separated. Leave blank to tag it later.", ) class EditSubscriptionForm(forms.ModelForm): class Meta: model = Subscription fields = ["custom_title"] labels = {"custom_title": "Title (leave blank to use the feed's own title)"} def __init__(self, *args, user=None, **kwargs): super().__init__(*args, **kwargs) self._user = user class OPMLImportForm(forms.Form): opml_file = forms.FileField(label="OPML file") def clean_opml_file(self): uploaded = self.cleaned_data["opml_file"] # Checked on the upload's reported size before anything reads it # into memory (R2-S5); the reverse proxy's client_max_body_size is # a second, coarser limit. if uploaded.size > MAX_UPLOAD_BYTES: raise forms.ValidationError( f"That file is too large (limit {MAX_UPLOAD_BYTES // (1024 * 1024)} MB)." ) return uploaded