Commits

Vladimir Sidorenko committed c1ac947

initial commit

  • Participants

Comments (0)

Files changed (8)

+graft *
+prune .hg
+prune .ropeproject

redactor/__init__.py

Empty file added.

redactor/admin.py

+from django.contrib import admin
+from django.db.models import TextField
+from redactor.widgets import JQueryEditor
+
+
+class RedactorModelAdmin(admin.ModelAdmin):
+    formfield_overrides = {
+        TextField: {'widget': JQueryEditor},
+    }
+
+    class Media:
+        js = ('https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js', )

redactor/forms.py

+from django import forms
+
+class ImageForm(forms.Form):
+    file = forms.ImageField()
+from django.conf.urls.defaults import *
+from redactor.views import redactor_upload
+
+urlpatterns = patterns('',
+    url('^upload/(?P<upload_to>.*)', redactor_upload, name='redactor_upload'),
+)

redactor/views.py

+import os
+
+from django.conf import settings
+from django.contrib.auth.decorators import user_passes_test
+from django.core.files.storage import default_storage
+from django.http import HttpResponse
+from django.views.decorators.csrf import csrf_exempt
+from django.views.decorators.http import require_POST
+
+from redactor.forms import ImageForm
+
+
+UPLOAD_PATH = getattr(settings, 'REDACTOR_UPLOAD_IMAGES', 'redactor/images/')
+
+
+@csrf_exempt
+@require_POST
+@user_passes_test(lambda u: u.is_staff)
+def redactor_upload(request, upload_to=None, form_class=ImageForm):
+    form = form_class(request.POST, request.FILES)
+    if form.is_valid():
+        file = form.cleaned_data['file']
+        path = os.path.join(upload_to or  UPLOAD_PATH, file.name)
+        real_path = default_storage.save(path, file)
+        return HttpResponse(os.path.join(settings.MEDIA_URL, real_path))
+    return HttpResponse(status=403)

redactor/widgets.py

+from django.forms import widgets
+from django.utils.safestring import mark_safe
+from django.core.urlresolvers import reverse
+from django.conf import settings
+from django.utils import simplejson as json
+
+OPTIONS = getattr(settings, 'REDACTOR_OPTIONS', {'focus': True})
+
+def extend(dict1, dict2):
+    return dict(dict1, **dict2)
+
+class JQueryEditor(widgets.Textarea):
+
+    class Media:
+        js = ('redactor/redactor.js',)
+        css = {
+            'all': ('redactor/css/redactor.css', ),
+        }
+
+    def __init__(self, *args, **kwargs):
+        self.upload_to = kwargs.pop('upload_to', '')
+        self.options = dict(OPTIONS, **kwargs.pop('redactor_options', {}))
+
+        kwargs['attrs'] = extend({
+            'cols': 100,
+            'rows': 30,
+        }, kwargs.get('attrs', {}))
+
+        super(JQueryEditor, self).__init__(*args, **kwargs)
+
+    def render(self, name, value, attrs=None):
+        input_ = super(JQueryEditor, self).render(name, value, attrs)
+        final_attrs = self.build_attrs(attrs)
+        id_ = final_attrs.get('id')
+
+        options = json.dumps(extend({
+            'upload': reverse('redactor_upload', kwargs={'upload_to': self.upload_to}),
+        }, self.options))
+
+        return mark_safe(input_ + (
+            '<script type="text/javascript">' +
+            'jQuery(document).ready(function(){' +
+            '$("#%s").redactor(%s);' % (id_, options) +
+            '});</script>'
+        ))
+from setuptools import setup, find_packages
+
+setup(name="django-redactor",
+           version="0.1",
+           description="Django application for http://imperavi.ru/redactor/",
+           author="Vladimir Sidorenko",
+           author_email="yoyavova@gmail.com",
+           packages=find_packages(),
+           include_package_data=True,
+)