File: forms.py

package info (click to toggle)
django-bleach 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 324 kB
  • sloc: python: 993; makefile: 137; sh: 16
file content (71 lines) | stat: -rwxr-xr-x 1,741 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from django import forms

from bleach.css_sanitizer import CSSSanitizer

from django_bleach.forms import BleachField
from testproject.constants import (
    ALLOWED_ATTRIBUTES,
    ALLOWED_CSS_PROPERTIES,
    ALLOWED_PROTOCOLS,
    ALLOWED_STYLES,
    ALLOWED_TAGS,
)
from testproject.models import Person


class CustomBleachWidget(forms.Textarea):

    def __init__(self, attrs=None):
        default_attrs = {'rows': 15, 'cols': 60}
        default_attrs.update(attrs or {})
        super().__init__(attrs=default_attrs)


class BleachForm(forms.Form):
    """ Form for testing BleachField """
    no_tags = BleachField(
        max_length=100,
        strip_tags=True,
        allowed_tags=[]
    )

    no_strip = BleachField(
        max_length=100,
        allowed_tags=None,
        allowed_attributes=None
    )

    bleach_strip = BleachField(
        max_length=100,
        strip_comments=True,
        strip_tags=True,
        allowed_tags=ALLOWED_TAGS
    )
    bleach_attrs = BleachField(
        max_length=100,
        strip_tags=False,
        allowed_tags=ALLOWED_TAGS,
        allowed_protocols=ALLOWED_PROTOCOLS,
        allowed_attributes=ALLOWED_ATTRIBUTES
    )
    bleach_styles = BleachField(
        max_length=100,
        strip_tags=False,
        allowed_attributes=['style'],
        allowed_tags=ALLOWED_TAGS,
        allowed_styles=ALLOWED_STYLES
    )
    bleach_css_sanitizer = BleachField(
        max_length=100,
        strip_tags=False,
        allowed_attributes=['style'],
        allowed_tags=ALLOWED_TAGS,
        css_sanitizer=CSSSanitizer(allowed_css_properties=ALLOWED_CSS_PROPERTIES)
    )


class PersonForm(forms.ModelForm):

    class Meta:
        model = Person
        fields = '__all__'