Package: redsolutioncms.django-trusted-html
Django-trusted-html will make your html correct, pretty and safe.Usage lyricsSuppose that some users can post content to your site.
And you want to allow them to post formatted text, images, tables and videos.
The best way is using HTML as native format and WYSIWYG editor as user interface.
So your users will create content easy and will be happy.
They will be able to copy-and-paste content from other sites or GUI-Editors.
But you can become unhappy. Your site can looks not homogeneous because of
different font-families, colors, indents that will come with copy-and-pasted content.
Also you can want to protect your site from JavaScript injections.
In this way you might want to use django-trusted-html.SanitizingThis is application for sanitizing HTML from:javascript injectionsobjectionable CSS stylesobjectionable tagsobjectionable or inaccessible links, images and embedded objectsFor example you can:remove scripts from user`s content posted to your siteremove user-specified fonts and colors to make your site looks prettyallow user to post video for example only from ‘youtube.com’disable images arranged not on your own siteValid HTMLThis is application for making valid HTML:remove incorrect tags, attributes, css-properties and css-values not allowed to this propertycheck and remove broken link, and do some more things with themFor example you can:make all your content w3c validremove broken links to other sitesremove host name from links to you site.CustomYou can:choose one of presetsspecify settings of validationcustomize rules of validationInstallation:Puttrustedhtmlin to yourINSTALLED_APPSin yoursettings.pywithin your django project.Sync your database:./manage.py syncdbCustomize settings in yoursettings.py.To learn more about settings readtrustedhtml/settings.py.Requirements:Django 1.3+ is required.
For Django < 1.3 please check out django-trusted-html 0.1.2.Usage:In your models:You can use TrustedField in your modelfrom trustedhtml.fields import TrustedTextField

class MyModel(models.Model):
    html = TrustedTextField()Also you can specify one of predefined validatorsfrom trustedhtml.rules import full, normal, pretty
from trustedhtml.fields import TrustedTextField

class MyModel(models.Model):
    html = TrustedTextField(validator=pretty)trustedhtml.rules.fullrule will safe all html tags and css style described by w3c.trustedhtml.rules.normalrule will remove dangerous html element, or elements that can break you design.trustedhtml.rules.prettyrule also will remove colors, fonts, aligns, margins and other css and html attributes.By the way, if you have django-tinymce in INSTALLED_APPS, than you can use TrustedHTMLField.You can validate html before it will be saved:from trustedhtml.rules import pretty

class MyModel(models.Model):
    html = models.TextField()
    def save(self, *args, **kwargs):
        self.html = pretty.validate(self.html)
        super(MyModel, self).save(*args, **kwargs)Or:from trustedhtml.rules import pretty
from someapp.models import SomeModel

def content_save(sender, instance, **kwargs):
    instance.content = pretty.validate(instance.content)

pre_save.connect(content_save, sender=SomeModel)You can validate html by using widget:from django import forms
from trustedhtml.widgets import TrustedTextarea

class FormField(forms.TextField):
        widget = TrustedTextareaIf you are using django-pages-cms, you can just use TrustedWidget in templates:{% placeholder main_content with TrustedTextarea %}Or for older versions of django-pages-cms:{% placeholder main_content with trustedhtml.widgets.TrustedTextarea %}Also if you are using TinyMCE:{% placeholder main_content with trustedhtml.widgets.TrustedTinyMCE %}Or for older versions of django-pages-cms:{% placeholder main_content with TrustedTinyMCE %}You can just ask trusted html to validate specified fields in specified models.In some application:class SomeModel(models.Model):
        name = models.CharField(max_length=100)
    description = models.TextField()In yoursettings.py:TRUSTEDHTML_MODELS = [
        {
            'model': 'someapp.models.SomeModel',
            'fields': ['description', ],
        },
]Changelog:0.1.0 - Initial release0.1.1 - Allow <iframe> elements from trusted sits (for youtube movies)0.1.2 - Setup for RedsolutionCMS execute in postmake and write settings in the end of settings.py0.2.0 - Django 1.6 compatibility.Classifiers:Content plugins