Code Monkey home page Code Monkey logo

django-sirtrevor-editor's Introduction

django-sirtrevor-editor

django-sirtrevor-editor is a simple Django app that provides a content editing widget based on the fantastic Sir Trevor project.

Forked from django-sirtrevor

Quick start

  1. Install django-sirtrevor-editor:

    pip install django-sirtrevor-editor
    
  2. Add sirtrevor to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = (
        ...
        'sirtrevor',
    )
    
  3. Add sir trevor urls:

    url(r'^sirtrevor/', include('sirtrevor.urls')),
    
  4. Create a model that makes use of SirTrevorField:

    from django.db import models
    from sirtrevor.fields import SirTrevorField
    
    class MyModel(models.Model):
        ...
        content = SirTrevorField()
        ...
    
  5. Now you can โ€ฆ

    • see it in action in the Django admin
    • create a ModelForm from your model
    • create a plain Form and use sirtrevor.forms.SirTrevorFormField
    • use sirtrevor.widgets.SirTrevorWidget as a widget replacement for a Textarea

Configuration

Sir Trevor has a few configuration options. You can customize most of them project-wide in your settings.py or some on a per-widget basis as kwargs for SirTrevorWidget.

Available options (CONFIGURATION_SETTINGS / widget_kwargs):

SIRTREVOR_BLOCK_TYPES / st_block_types
A list of block types to use with the editor. Defaults to ['Text', 'List', 'Quote', 'Image', 'Video', 'Tweet', 'Heading']
SIRTREVOR_DEFAULT_TYPE / st_default_type
The default block to start the editor with. Defaults to None
SIRTREVOR_BLOCK_LIMIT / st_block_limit
The overall total number of blocks that can be displayed. Defaults to 0
SIRTREVOR_BLOCK_TYPE_LIMITS / st_block_type_limits
Limit on the number of blocks that can be displayed by its type. Defaults to {}
SIRTREVOR_REQUIRED / st_required
Mandatory block types that are required for validatation. Defaults to None
SIRTREVOR_UPLOAD_URL / st_upload_url
URL for AJAX image uploads. Defaults to /sirtrevor/attachments/ (depending on where you include django-sirtrevor-editor's URLs in urls.py)
SIRTREVOR_UPLOAD_PATH
Path where to store uploaded images relative to MEDIA_ROOT. (not configurable via widget kwargs) Defaults to attachments
SIRTREVOR_ATTACHMENT_PROCESSOR
A string containing a dotted path to a function that will be run before saving an uploaded image. See below for more details. (not configurable via widget kwargs) Defaults to None

Resizing Images

You can resize uploaded images by implementing a function somewhere in your code and pointing SIRTREVOR_ATTACHMENT_PROCESSOR to its location. The first argument will be the file object and the method must return a SimpleUploadFile object.

Example implemented in utils.py in an app called core. SIRTREVOR_ATTACHMENT_PROCESSOR set to core.utils.resize_attachment:

from PIL import Image
from StringIO import StringIO
from django.core.files.uploadedfile import SimpleUploadedFile


def resize_attachment(file_):
    size = (1024, 9999)
    try:
        temp = StringIO()
        image = Image.open(file_)
        image.thumbnail(size, Image.ANTIALIAS)
        image.save(temp, 'jpeg')
        temp.seek(0)
        return SimpleUploadedFile(file_.name, temp.read(), content_type='image/jpeg')
    except Exception as ex:
        return file_

Custom Blocks

Sir Trevor can be extended through custom blocks. Starting with 0.2.1 django-sirtrevor-editor also has basic support for custom blocks.

Here is a little step-by-step guide:

myapp/blocks.py:

from sirtrevor.blocks import BaseBlock

class MyCustomBlock(BaseBlock):
    name = 'MyCustomName'

    class Media:
        js = ['sirtrevor/blocks/mycustomblock.js']

myapp/models.py:

import sirtrevor
from .blocks import MyCustomBlock

sirtrevor.register_block(MyCustomBlock)

myapp/static/sirtrevor/blocks/mycustomblock.js:

SirTrevor.Blocks.MyCustomName = SirTrevor.Block.extend({
    type: 'mycustomblock',
    // ...
});

Please refer to Sir Trevor's docs regarding custom blocks for details about the JavaScript part of a custom block.

myapp/templates/sirtrevor/blocks/mycustomblock.html:

<div class="content-block mycustom-block">
    <!-- Whatever JSON the custom block creates is available in the template -->
</div>

settings.py:

# ...

SIRTREVOR_BLOCK_TYPES = ['Text', '...', 'MyCustomName']

# ...

For reference please check out django-sirtrevor-editor-file which implements a simple block type for file downloads.

License

MIT

django-sirtrevor-editor's People

Contributors

bradleyg avatar iraycd avatar k0ng avatar petry avatar philippbosch avatar

Stargazers

 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.