class ProblemsForm(forms.ModelForm): answer = BleachField(label=("Correct Answer"), widget=forms.TextInput(attrs={'size': '80'})) distractor1 = BleachField(label=("Distrator"), required=False, widget=forms.TextInput(attrs={'size': '80'})) distractor2 = BleachField(label=("Distrator"), required=False, widget=forms.TextInput(attrs={'size': '80'})) distractor3 = BleachField(label=("Distrator"), required=False, widget=forms.TextInput(attrs={'size': '80'})) def __init__(self, *args, **kwargs): super(ProblemsForm, self).__init__(*args, **kwargs) self.fields['concepts'].queryset = Concepts.objects.filter(graph=g) self.fields['concepts'].help_text = "Most exercises apply only to a single concept. However, you may list multiple concepts if you wish the exercise to appear in more than one exercise set." # fill out answer and distractors if this problem has them if self.instance.id: pset = Responses.objects.filter(problem=self.instance) i = 1 for p in pset: f = 'answer' if p.distract and i < 4: f = 'distractor%d' % i i += 1 self.fields[f].initial = p.response class Meta: model = Problems labels = { 'qtype': ("Type"), } widgets = { 'question': TinyMCE(attrs={'cols': 80, 'rows': 20}), }
class CustomForm(forms.Form): # Define form inside function with overridden settings so # get_default_widget() sees the modified setting. 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 )
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 )
def test_empty(self): """ Test that the empty_value arg is returned for any input empty value """ for requested_empty_value in ('', None): field = BleachField(empty_value=requested_empty_value) for empty_value in field.empty_values: self.assertEqual(field.to_python(empty_value), requested_empty_value)
class BleachForm(forms.Form): """ Form for testing BleachField """ no_tags = BleachField(max_length=100, strip_tags=True, allowed_tags=[]) bleach_strip = BleachField(max_length=100, strip_tags=True, allowed_tags=ALLOWED_TAGS) bleach_attrs = BleachField(max_length=100, strip_tags=False, allowed_tags=ALLOWED_TAGS, allowed_attributes=ALLOWED_ATTRIBUTES)
def __init__(self, *args, **kwargs): sizes = kwargs.pop('sizes', []) self.shop = kwargs.pop('shop', None) super(ProductCreationForm, self).__init__(*args, **kwargs) self.fields['sizeVariation'] = forms.ChoiceField( label='Size type', choices=SIZE_TYPES_AND_EMPTY, required=True, initial=self.get_value_if_in_edit_mode('sizeVariation', '0')) self.fields['price'] \ = forms.DecimalField(decimal_places=2, max_digits=12, localize=True, initial=self.get_value_if_in_edit_mode('price', None), min_value=Decimal('0.01')) self.helper = FormHelper() self.helper.form_show_labels = False self.helper.layout = Layout( Div(Fieldset( 'General', Field('title', placeholder='Title'), Field('description', placeholder='Description', style="padding-bottom: 10px"), Field('category', placeholder='Choose a Category'), HTML( '<b>Include shipping costs into the price</b><a data-toggle="modal" data-target="#shipping_estimation" ' 'class="pull-right" style="cursor: pointer;">Weight Based Shipping Price Guide</a>' ), Field('price', placeholder='Price')), Fieldset( 'Images', HTML( """<p>Select up to 5 images for this item. Image size recommendations are 400x400</p>""" ), HTML( '{% load crispy_forms_tags %}{% crispy productImageFormSet %}' ), ), Fieldset( 'Sizes and Colors', Field('sizeVariation', placeholder='Choose a variation'), Div( Fieldset('Sizes', css_id="sizesFieldSet", css_class="hidden")), ), Submit('productCreationForm', 'Edit' if self.instance.pk else 'Create', css_class='tinvilleButton hidden', css_id='id_SubmitAddEditItem'), css_class="container col-xs-12", css_id="addItemEditor")) self.fields['description'] = BleachField(required=False) self.fields['description'].widget = TinyMCE() self.fields['category'] = forms.ModelChoiceField( queryset=get_model('catalogue', 'Category').objects.filter(depth=3), empty_label="Choose a Category", required=True, initial=self.get_value_if_in_edit_mode('category', None)) self.fields['price'].label = "" # Jon M TBD - Right now we only use 1 product class - "Apparel" self.instance.product_class = get_model( 'catalogue', 'ProductClass').objects.all()[:1].get() if sizes: for i, size in enumerate(sizes): if "sizeSet" in sizes[i] and sizes[i]["sizeSet"]: self.fields[sizes[i]["sizeFieldName"]] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=1), empty_label="Choose a size...", required=True, initial=sizes[i]["sizeSet"]) for j, colorAndQuantity in enumerate( sizes[i]["colorsAndQuantities"]): if colorAndQuantity[ 'primary_color'] and colorAndQuantity[ 'quantity']: self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_pc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["primary_color"], required=False) self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_sc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["secondary_color"], required=False) self.fields[colorAndQuantity['quantityFieldName']] \ = forms.IntegerField(initial=sizes[i]["colorsAndQuantities"][j]["quantity"], required=False, min_value=0) elif "sizeX" in sizes[i] and sizes[i][ "sizeX"] and "sizeY" in sizes[i] and sizes[i]["sizeY"]: self.fields[sizes[i]["sizeFieldNameX"]] \ = forms.DecimalField(initial=sizes[i]["sizeX"]) self.fields[sizes[i]["sizeFieldNameY"]] \ = forms.DecimalField(initial=sizes[i]["sizeY"]) for j, colorAndQuantity in enumerate( sizes[i]["colorsAndQuantities"]): self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_pc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["primary_color"], required=False) self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_sc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["secondary_color"], required=False) self.fields[colorAndQuantity['quantityFieldName']] \ = forms.IntegerField(initial=sizes[i]["colorsAndQuantities"][j]["quantity"], min_value=0) elif "sizeNum" in sizes[i] and sizes[i]["sizeNum"]: self.fields[sizes[i]["sizeFieldName"]] \ = forms.DecimalField(initial=sizes[i]["sizeNum"]) for j, colorAndQuantity in enumerate( sizes[i]["colorsAndQuantities"]): self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_pc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["primary_color"], required=False) self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_sc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["secondary_color"], required=False) self.fields[colorAndQuantity['quantityFieldName']] \ = forms.IntegerField(initial=sizes[i]["colorsAndQuantities"][j]["quantity"], min_value=0) elif "oneSize" in sizes[i] and sizes[i]["oneSize"]: for j, colorAndQuantity in enumerate( sizes[i]["colorsAndQuantities"]): self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_pc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["primary_color"], required=False) self.fields[colorAndQuantity["colorFieldName"].replace('_colorSelection', '_sc_colorSelection')] \ = forms.ModelChoiceField(queryset=get_model('catalogue', 'AttributeOption'). objects.filter(group=2), empty_label="Choose a color...", initial=sizes[i]["colorsAndQuantities"][j]["secondary_color"], required=False) self.fields[colorAndQuantity['quantityFieldName']] \ = forms.IntegerField(initial=sizes[i]["colorsAndQuantities"][j]["quantity"], min_value=0)
def test_return_type(self): """ Test bleached values are SafeString objects """ field = BleachField() self.assertIsInstance(field.to_python("some text"), SafeString)
class PostCreationForm(forms.ModelForm): body = BleachField() class Meta: model = Post fields = ('title', 'body', 'preview', 'type', 'tags', 'allow_comments')