This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
models.BleachField's form field set to forms.BleachField (#19)
* models.BleachField's form field set to forms.BleachField * BleachField form field label implemented models.BleachField form field label set to field's verbose_name * CustomBleachWidget customized and set in settings.py as bleach default widget * testproject person model, person form and model_form view created * test_default_widget BLEACH_DEFAULT_WIDGET setting overridden to ensure being independent of settings * ModelFormField tests added * forms.py flake8 errors fixed * testproject views.py request.path removed for redirect to fix CodeQl failur check * formfield test added with specified choices and test_same_allowed_args improved
- Loading branch information
1 parent
70f4e21
commit 0dfcd6d
Showing
14 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ coverage.xml | |
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
django-bleach.db | ||
|
||
# Flask stuff: | ||
instance/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django import forms | ||
from django.test import TestCase, override_settings | ||
from django_bleach import forms as bleach_forms | ||
from .test_models import BleachContent | ||
|
||
|
||
class BleachContentModelForm(forms.ModelForm): | ||
class Meta: | ||
model = BleachContent | ||
fields = '__all__' | ||
|
||
|
||
class TestModelFormField(TestCase): | ||
@override_settings(BLEACH_DEFAULT_WIDGET='testproject.forms.CustomBleachWidget') | ||
def setUp(self): | ||
model_form = BleachContentModelForm() | ||
self.form_field = model_form.fields['content'] | ||
self.choice_form_field = model_form.fields['choice'] | ||
self.model_field = BleachContent()._meta.get_field('content') | ||
self.default_widget_class = bleach_forms.get_default_widget() | ||
|
||
def test_formfield_type(self): | ||
""" Check content's form field is instance of BleachField | ||
""" | ||
self.assertIsInstance(self.form_field, bleach_forms.BleachField) | ||
|
||
def test_custom_widget(self): | ||
""" Check content form field's widget is instance of default widget | ||
""" | ||
self.assertIsInstance(self.form_field.widget, self.default_widget_class) | ||
|
||
def test_same_allowed_args(self): | ||
""" Check model and form's allowed arguments (tags, attributes, ...) are same | ||
""" | ||
form_allowed_args: dict = self.form_field.bleach_options | ||
model_allowed_args: dict = self.model_field.bleach_kwargs | ||
|
||
self.assertEqual(model_allowed_args, form_allowed_args) | ||
|
||
def test_with_choices(self): | ||
""" Check if choices specified, use TextField's default widget (Select). | ||
""" | ||
form_field_widget = self.choice_form_field.widget.__class__ | ||
self.assertEqual(form_field_widget, forms.widgets.Select) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 3.2.4 on 2021-06-15 12:39 | ||
|
||
from django.db import migrations, models | ||
import django_bleach.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Person', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=20)), | ||
('biography', django_bleach.models.BleachField(max_length=100, verbose_name='Person biography')), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.db import models | ||
from django_bleach.models import BleachField | ||
|
||
|
||
class Person(models.Model): | ||
name = models.CharField(max_length=20) | ||
biography = BleachField( | ||
max_length=100, | ||
verbose_name='Person biography', | ||
allowed_tags=['p', 'a', 'li', 'ul', 'strong'], | ||
allowed_attributes=['class', 'href', 'style'], | ||
allowed_protocols=['http', 'https'], | ||
allowed_styles=['color', 'background-color'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{% load bleach_tags %} | ||
|
||
<style> | ||
table { | ||
width: 100%; | ||
} | ||
|
||
td, th { | ||
border: 1px solid #dddddd; | ||
padding: 8px; | ||
} | ||
</style> | ||
|
||
<body> | ||
<h2>Model form example</h2> | ||
<strong>Note: You must migrate project to add person!</strong> | ||
|
||
<form action="/model_form" method="post"> | ||
{% csrf_token %} | ||
|
||
{{ form.errors }} | ||
{{ form.as_p }} | ||
|
||
<div> | ||
<input type="submit" value="Submit" /> | ||
</div> | ||
</form> | ||
|
||
<div> | ||
<h3>People list</h3> | ||
<table> | ||
<tbody> | ||
<tr> | ||
<th>Name</th> | ||
<th>Biography</th> | ||
</tr> | ||
{% for person in people %} | ||
<tr> | ||
<td>{{person.name}}</td> | ||
<td>{{person.biography|safe}}</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
</div> | ||
|
||
<a href="{% url 'home' %}">Form example</a> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters