Commits

kmike  committed d374fb2

Translated GearsUploader can now be used in admin.

  • Participants
  • Parent commits bf3892f

Comments (0)

Files changed (2)

File generic_images/admin.py

 
 admin.site.register(AttachedImage)
 
+def attachedimage_form_factory(lang='en'):
+    ''' Returns ModelForm class to be used in admin.
+        'lang' is the language for GearsUploader (can be 'en' and 'ru' at the
+        moment).
+    '''
+    class _AttachedImageAdminForm(forms.ModelForm):
 
-class AttachedImageAdminForm(forms.ModelForm):
-    ''' Form for AttachedImage model to be used in inline admin '''
+        caption = forms.CharField(label=_('Caption'), required=False)
 
-    caption = forms.CharField(label=_('Caption'), required=False)
+        class Media:
+            js = [
+                  'generic_images/js/mootools-1.2.3-core-yc.js',
+                  'generic_images/js/GearsUploader.%s.yui.js' % lang,
+                  'generic_images/js/AttachedImageInline.js',
+            ]
 
-    class Media:
-        js = [
-              'generic_images/js/mootools-1.2.3-core-yc.js',
-              'generic_images/js/GearsUploader.en.yui.js',
-              'generic_images/js/AttachedImageInline.js',
-        ]
+        class Meta:
+            model = AttachedImage
+    return _AttachedImageAdminForm
 
-    class Meta:
+AttachedImageAdminForm = attachedimage_form_factory()
+''' Form for AttachedImage model to be used in inline admin '''
+
+
+def attachedimages_inline_factory(lang='en', max_width=''):
+    '''  Returns InlineModelAdmin for attached images.
+        'lang' is the language for GearsUploader (can be 'en' and 'ru' at the
+        moment). 'max_width' is default resize width parameter to be set in
+        widget.
+    '''
+
+    class _AttachedImagesInline(GenericTabularInline):
         model = AttachedImage
+        form = attachedimage_form_factory(lang)
+        template = 'generic_images/attached_images_inline.html'
+        max_w = max_width
 
+    return _AttachedImagesInline
 
-class AttachedImagesInline(GenericTabularInline):
-    ''' InlineModelAdmin for attached images.
-        Adds multi-image uploader with progress bar, before-upload image
-        previews and client-side resizing. Uploader is based
-        on GearsUploader (http://bitbucket.org/kmike/gearsuploader/) project.
+AttachedImagesInline = attachedimages_inline_factory()
+''' InlineModelAdmin for attached images.
+    Adds multi-image uploader with progress bar, before-upload image
+    previews and client-side resizing. Uploader is based
+    on GearsUploader (http://bitbucket.org/kmike/gearsuploader/) project.
 
-        To make this work copy ``generic_images`` folder from
-        ``generic_images/media/`` to your ``MEDIA_ROOT``. Then use
-        AttachedImagesInline class for you inlines::
+    To make this work copy ``generic_images`` folder from
+    ``generic_images/media/`` to your ``MEDIA_ROOT``. Then use
+    ``AttachedImagesInline`` class for you inlines::
 
-            #admin.py
+        #admin.py
 
-            from django.contrib import admin
-            from generic_images.admin import AttachedImagesInline
+        from django.contrib import admin
+        from generic_images.admin import AttachedImagesInline
 
-            class MyModelAdmin(admin.ModelAdmin):
-                inlines = [AttachedImagesInline]
+        class MyModelAdmin(admin.ModelAdmin):
+            inlines = [AttachedImagesInline]
 
-            admin.site.register(MyModel, MyModelAdmin)
+        admin.site.register(MyModel, MyModelAdmin)
 
 
-        Just before standard formset the following uploader is displayed:
+    Just before standard formset the following uploader is displayed:
 
-        .. image:: img/admin-with-formset.png
+    .. image:: img/admin-with-formset.png
 
-        Gears plugin is here
+    Gears plugin is here
 
-        .. image:: img/admin-nogears.png
+    .. image:: img/admin-nogears.png
 
-        Message is displayed if Gears plugin is not available
+    Message is displayed if Gears plugin is not available
 
-        .. image:: img/admin-previews.png
+    .. image:: img/admin-previews.png
 
-        User can select several files at once using Ctrl or Shift keys
-        (Cmd on Mac) in standard OS file selection dialog. He can also remove
-        images from selection by clicking on thumbnails. Several files can also
-        be selected by opening file selection dialog several times.
+    User can select several files at once using Ctrl or Shift keys
+    (Cmd on Mac) in standard OS file selection dialog. He can also remove
+    images from selection by clicking on thumbnails. Several files can also
+    be selected by opening file selection dialog several times.
 
-        .. image:: img/admin-uploading.png
+    .. image:: img/admin-uploading.png
 
-        User presses 'Upload' button and upload process begins
+    User presses 'Upload' button and upload process begins
 
 
-        By default the 'Resize ..' checkbox is unchecked and the input field is
-        blank. If it is unchecked then images are not resized before uploading.
-        User can check it and set his max image width.
+    By default the 'Resize ..' checkbox is unchecked and the input field is
+    blank. If it is unchecked then images are not resized before uploading.
+    User can check it and set his max image width.
 
-        In order to set the default value and mark the checkbox as checked by
-        default subclass AttachedImagesInline and set ``max_width`` parameter::
+    In order to set the default value and mark the checkbox as checked by
+    default create customized ``AttachedImagesInline`` class using
+    :func:`attachedimages_inline_factory` function. This function can also be
+    used to change uploader language (language auto-discovering is not
+    implemented yet)::
 
-            class MyImagesInline(AttachedImagesInline):
-                max_width=600
+        from generic_images.admin import attachedimages_inline_factory
 
-            class MyModelAdmin(admin.ModelAdmin):
-                inlines = [MyImagesInline]
+        class MyModelAdmin(admin.ModelAdmin):
+            inlines = [attachedimages_inline_factory(lang='ru', max_width=1024)]
 
-            admin.site.register(MyModel, MyModelAdmin)
+        admin.site.register(MyModel, MyModelAdmin)
 
-    '''
-    model = AttachedImage
-    form = AttachedImageAdminForm
-    template = 'generic_images/attached_images_inline.html'
 
-#    max_width=600
+'''

File generic_images/models.py

 #coding: utf-8
 import os
-import random
+#import random
 
 from django.db import models
 from django.contrib.auth.models import User
 from django.core.files.storage import default_storage
 from django.db.models import Max
-from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import ugettext_lazy as _
 
 from generic_images.signals import image_saved, image_deleted