from django.db import models
from django import forms
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from xadmin.sites import site
from xadmin.views import BaseAdminPlugin, ModelFormAdminView, DetailAdminView


class AdminImageField(forms.ImageField):

    def widget_attrs(self, widget):
        return {'label': self.label}


class AdminImageWidget(forms.FileInput):
    """
    A ImageField Widget that shows its current value if it has one.
    """
    def __init__(self, attrs={}):
        super(AdminImageWidget, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        output = []
        if value and hasattr(value, "url"):
            label = self.attrs.get('label', name)
            output.append('<a href="%s" target="_blank" title="%s" rel="gallery"><img src="%s" class="field_img"/></a><br/>%s ' %
                         (value.url, label, value.url, _('Change:')))
        output.append(super(AdminImageWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))


class ModelDetailPlugin(BaseAdminPlugin):

    def __init__(self, admin_view):
        super(ModelDetailPlugin, self).__init__(admin_view)
        self.include_image = False

    def get_field_attrs(self, attrs, db_field, **kwargs):
        if isinstance(db_field, models.ImageField):
            attrs['widget'] = AdminImageWidget
            attrs['form_class'] = AdminImageField
            self.include_image = True
        return attrs

    def get_field_result(self, result, field_name):
        if isinstance(result.field, models.ImageField):
            if result.value:
                img = getattr(result.obj, field_name)
                result.text = mark_safe('<a href="%s" target="_blank" title="%s" rel="gallery"><img src="%s" class="field_img"/></a>' % (img.url, result.label, img.url))
                self.include_image = True
        return result

    # Media
    def get_media(self, media):
        if self.include_image:
            media = media + self.vendor('image-gallery.js',
                                        'image-gallery.css')
        return media

    def block_before_fieldsets(self, context, node):
        if self.include_image:
            return """
              <div id="gallery" data-toggle="modal-gallery" data-target="#modal-gallery">
                <!-- modal-gallery is the modal dialog used for the image gallery -->
                <div id="modal-gallery" class="modal modal-gallery hide fade" tabindex="-1">
                    <div class="modal-header">
                        <a class="close" data-dismiss="modal">&times;</a>
                        <h3 class="modal-title"></h3>
                    </div>
                    <div class="modal-body"><div class="modal-image"></div></div>
                    <div class="modal-footer">
                        <a class="btn btn-primary modal-next">%s <i class="icon-arrow-right icon-white"></i></a>
                        <a class="btn btn-info modal-prev"><i class="icon-arrow-left icon-white"></i> %s</a>
                        <a class="btn btn-success modal-play modal-slideshow" data-slideshow="5000"><i class="icon-play icon-white"></i> %s</a>
                        <a class="btn btn-default modal-download" target="_blank"><i class="icon-download"></i> %s</a>
                    </div>
                </div>
            """ % (_('Next'), _('Previous'), _('Slideshow'), _('Download'))

    def block_after_fieldsets(self, context, node):
        if self.include_image:
            return "</div>"

site.register_plugin(ModelDetailPlugin, DetailAdminView)
site.register_plugin(ModelDetailPlugin, ModelFormAdminView)

@@ -805,35 +805,44 @@
 ank%22 title=%22%25s%22 
-rel
+data-gallery
 =%22gallery%22%3E%3Cimg 
@@ -1798,11 +1798,20 @@
 %25s%22 
-rel
+data-gallery
 =%22ga
@@ -2282,26 +2282,9 @@
 urn 
-%22%22%22%0A              
+'
 %3Cdiv
@@ -2354,16 +2354,230 @@
 allery%22%3E
+'%0A%0A    def block_after_fieldsets(self, context, node):%0A        if self.include_image:%0A            return %22%3C/div%3E%22%0A%0A    def block_extrabody(self, context, node):%0A        if self.include_image:%0A            return %22%22%22
 %0A       
@@ -2726,13 +2726,8 @@
 ery 
-hide 
 fade
@@ -2743,16 +2743,111 @@
 x=%22-1%22%3E%0A
+                  %3Cdiv class=%22modal-dialog%22%3E%0A                    %3Cdiv class=%22modal-content%22%3E%0A  
         
@@ -3025,39 +3025,43 @@
                 
+  
 %3C/div%3E%0A
+  
                 
@@ -3138,32 +3138,34 @@
                 
+ 
+ 
 %3Cdiv class=%22moda
@@ -3171,24 +3171,26 @@
 al-footer%22%3E%0A
+  
             
@@ -3306,32 +3306,34 @@
                 
+ 
+ 
 %3Ca class=%22btn bt
@@ -3393,32 +3393,34 @@
 te%22%3E%3C/i%3E %25s%3C/a%3E%0A
+  
                 
@@ -3563,32 +3563,34 @@
                 
+ 
+ 
 %3Ca class=%22btn bt
@@ -3680,32 +3680,34 @@
                 
+  
 %3C/div%3E%0A         
@@ -3717,202 +3717,189 @@
     
-%3C/div%3E%0A            %22%22%22 %25 (_('Next'), _('Previous'), _('Slideshow'), _('Download'))%0A%0A    def block_after_fieldsets(self, context, node):%0A        if self.include_image:%0A            return %22%3C/div%3E%22
+    %3C/div%3E%3C!-- /.modal-content --%3E%0A                  %3C/div%3E%3C!-- /.modal-dialog --%3E%0A                %3C/div%3E%0A            %22%22%22 %25 (_('Next'), _('Previous'), _('Slideshow'), _('Download'))
 %0A%0Asi

Active Image plugin.