Coder Social home page Coder Social logo

pombredanne / django-janitor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bradmontgomery/django-janitor

0.0 1.0 0.0 480 KB

django-janitor allows you to use bleach to clean HTML stored in a Model's field.

License: MIT License

Python 100.00%

django-janitor's Introduction

Overview

django-janitor allows you to use bleach to clean HTML stored in arbitrary Models.

This is useful when you've got content stored in a 3rd-party app, but you'd like to use a whitelist for allowed HTML tags.

Features

  • You don't need to edit an existing app (django-janitor listens for a Models' pre_save signal)
  • Allowed HTML tags, attributes, and css are specified in a whitelist
  • Whitelists are managed in Django's Admin
  • There are (sensible) default whitelists included

Requirements

  • Requires Bleach >= 1.4
  • Works with Django 1.4 to 1.6.5

Installation

To install the most recent release:

pip install django-janitor

To install from the current repository:

pip install git+https://github.com/bradmontgomery/django-janitor

Then, add janitor to your installed apps, and run syncdb or run the south migrations:

python manage.py migrate janitor

Usage

Visit the Janitor app in Django's Admin, and create a new Field sanitizer. Then select the Model and specify the fieldname which should be cleaned. After you set up the whitelists for Tags, Attributes, etc, save the Field sanitizer.

From now on, when the Model is saved, it's content in will be cleaned using bleach.

Here's a Screenshot:

screenshot

Tests

There are a few tests in janitor/tests. You can run these with:

python manage.py tests janitor

These tests dynamically add a sample app/model to INSTALLED_APPS, then call syncdb.

Management Commands

There are a few managment commands avaialable to make it easier to use django-janitor. The first is clean_all which will look at all of the models that have a related Field Sanitizer, calling the models' save method to trigger the pre_save signal (which forces the fields to be cleaned):

python manage.py clean_all

This is useful if you've created a FieldSanitizer for a model with existing content.

The second management command is clean_model, which works in a similar fashion, but allows you to specify an app and a model:

python manage.py clean_model myapp.MyModel

Finally, list_html_elements and list_html_elements_for_model exist to help you discover what HTML tags are being used in existing content. While these commands do require that a FieldSanitizer be configured for existing Models, they may be used to help you decide which tags to include in a whitelist.

You should run these commands before using clean_all or clean_model to see what sort of data exists before it's cleaned:

python manage.py list_html_elements

Or:

python manage.py list_html_elements_for_model myapp.MyModel

django-janitor's People

Contributors

bradmontgomery avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.