Esempio n. 1
0
class PublicBodyForm(forms.Form):
    name = forms.CharField(label=_("Name of Public Body"))
    description = forms.CharField(label=_("Short description"),
        widget=forms.Textarea, required=False)
    email = forms.EmailField(widget=forms.EmailInput,
        label=_("Email Address for Freedom of Information Requests"))
    url = forms.URLField(label=_("Homepage URL of Public Body"))
Esempio n. 2
0
class WallabagForm(ServiceForm):
    url = forms.URLField(label=_('Wallabag URL'),
                         help_text=_('Your Wallabag URL, e.g. '
                                     'https://www.framabag.org/u/username'))

    def check_wallabag(self):
        self.user.read_later_credentials = json.dumps({
            'wallabag_url':
            self.cleaned_data['url'],
        })
Esempio n. 3
0
class SubscriptionForm(forms.Form):
    subscribe = forms.BooleanField(label=_('Subscribe?'), required=False)
    name = forms.CharField(label=_('Name'), required=False)
    url = forms.URLField(label=_('URL'))
    category = forms.ChoiceField(label=_('Category'), required=False)

    def clean_url(self):
        url = self.cleaned_data['url']
        if (self.cleaned_data.get('subscribe', False)
                and self.user.feeds.filter(url=url).exists()):
            raise forms.ValidationError(
                _("You are already subscribed to this feed."))
        return url

    def clean_name(self):
        if (self.cleaned_data.get('subscribe', False)
                and not self.cleaned_data['name']):
            raise forms.ValidationError(_('This field is required.'))
        return self.cleaned_data['name']
Esempio n. 4
0
class NewNewsletterForm(BsForm):
    """Create a new newsletter"""
    subject = forms.CharField(
        label=_("Subject"),
        widget=forms.TextInput(
            attrs={'placeholder': _('Subject of the newsletter')}))
    template = forms.ChoiceField(label=_("Template"),
                                 choices=get_newsletter_templates(None, None))
    source_url = forms.URLField(label=_('Source URL'), required=False)
    content = forms.CharField(label=_('Content'),
                              required=False,
                              widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):
        super(NewNewsletterForm, self).__init__(*args, **kwargs)

        self.source_content = ""

        self.allow_url_sources = getattr(settings,
                                         'BALAFON_NEWSLETTER_SOURCES', ())
        if not self.allow_url_sources:
            self.fields['source_url'].widget = forms.HiddenInput()

    def clean_source_url(self):
        """
        clean source url: If an url is given download newsletter content from this source
        It can be used for people using a different tool for writing their newsletter
        """
        url = self.cleaned_data['source_url']
        if url:
            # Check in config if the url match with something allowed
            newsletter_sources = getattr(settings,
                                         'BALAFON_NEWSLETTER_SOURCES', ())
            for (regex, selector, post_processor) in newsletter_sources:
                if re.match(regex, url):
                    try:
                        # if so get the content
                        html = urlopen(url)
                        # and extract the selector content as initial content for the newsletter
                        soup = BeautifulSoup(html, "html.parser")
                        content = ''.join([
                            '{0}'.format(tag) for tag in soup.select(selector)
                        ])
                        if post_processor:
                            # import the post_processor function
                            module_name, processor_name = post_processor.rsplit(
                                '.', 1)
                            module = import_module(module_name)
                            post_processor_func = getattr(
                                module, processor_name)
                            # call the post_processor and update the content
                            content = post_processor_func(content)
                        self.source_content = content
                        return url
                    except Exception as msg:
                        raise ValidationError(msg)
            raise ValidationError(ugettext("The url is not allowed"))
        return ''

    def clean_content(self):
        """content validation"""
        url = self.cleaned_data['source_url']
        if url:
            return self.source_content
        return ''