Commits

Jason Goldstein  committed dd28650

Widgets are now templates

  • Participants
  • Parent commits 3181d62

Comments (0)

Files changed (4)

File README.markdown

 
 In the future some of this stuff might be inheritable... that'd be cool.
 
-### The Shameful Widgets Module
+### Other requirements
 
-Um... um... Okay, you've got me on this one. It's pretty gross. I'm really paricular about form styling - I want nice looking upload buttons, a 'currently' slot, clearing that I can actually put where I want it, thumbnails, and yes, now a crop button.
-
-Django Forms are both awesome and royally suck when you try to extend them. Suggestions on sucking less are welcome.
-
-It also assumes you use easy-thumbnails.
+It also assumes you use easy-thumbnails. But anything (solr-thumbnails) that uses the same API should work fine.
 
 
 ### Other Notes

File cropper/templates/cropper/crop_widget.html

+{% load thumbnail %}
+<span class="imgWidget">
+    {% if value %} 
+        <b class="thumb">
+            <img src="{% thumbnail value "75x75" %}">
+        </b>
+    {% endif %}
+    <i class="context">
+        {% if value %}            
+        <span>
+            <a class="thumbTxt" href="{{ link }}">{{ link|truncatechars:"20" }}</a>
+        </span>
+        {% endif %}
+        <span>
+            {% if value %} 
+                Change <a class="button" href="{{ crop_url }}?post-save-redirect={{ crop_redirect }}">Crop</a>
+            {% endif %}
+
+            <b class="wrap_file_input">
+                Select File {{ input }}
+            </b>
+            <i class="current"></i>
+        </span>
+        {% if value and not required and clear %} 
+            <span>Clear {{ clear }}</span>
+        {% endif %}
+    </i>
+</span>

File cropper/widgets.py

 from django.forms import ClearableFileInput, CheckboxInput
-from django.utils.safestring import mark_safe
 from django.template.defaultfilters import urlencode
-from django.template import Template, Context
+from django.template.loader import render_to_string
 
-def _snippet(template="", context={}):
-    """ Simple template render """
-    t = Template(template)
-    c = Context(context)
-    html = t.render(c)
-    return html
 
 
 class CropImageWidget(ClearableFileInput):
     """ ClearableFileInput with thumbnails and crop. """
 
-    def _crop_button(self, substitutions):
-        link = self.attrs.get('data-crop-url')
-        post_save_link = urlencode(self.attrs.get('post-save-redirect') or "")
-
-        if not link:
-            return ""
-        return """<a class="button" href="%s?post-save-redirect=%s">Crop</a>""" % (link, post_save_link)
-
     def render(self, name, value, attrs=None):
 
-        substitutions = {
+        link = None
+        if value and hasattr(value, "url"):
+            link = value.url
+
+        checkbox_name = self.clear_checkbox_name(name)
+        checkbox_id = self.clear_checkbox_id(checkbox_name)
+        context = {
             'initial_text': self.initial_text,
             'input_text': self.input_text,
             'clear_template': '',
             'clear_checkbox_label': self.clear_checkbox_label,
+            'required': self.is_required,
+            'input': super(ClearableFileInput, self).render(name, value, attrs),
+            'value': value,
+            'link': link,
+            'attrs': attrs,
+            'crop_url': self.attrs.get('data-crop-url'),
+            'crop_redirect': urlencode(self.attrs.get('post-save-redirect') or ""),
+            'clear': CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id}),
+            'required': self.is_required,
         }
 
-        substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)
-        img = None
-        if value and hasattr(value, "url"):
-            substitutions['initial'] = '<a class="thumbTxt" href="%(link)s">%(txt)s</a>' % {
-                'link':value.url, 'txt':value.name[:20] }
-
-            img = _snippet(u'{% load thumbnail %}<img src="{% thumbnail photo "75x75" %}">', { 'photo': value })
-
-            if not self.is_required:
-                checkbox_name = self.clear_checkbox_name(name)
-                checkbox_id = self.clear_checkbox_id(checkbox_name)
-                substitutions['clear_checkbox_name'] = checkbox_name
-                substitutions['clear_checkbox_id'] = checkbox_id
-                substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
-                substitutions['clear_template'] = self.template_with_clear % substitutions
-
-        styleable_input = '<b class="wrap_file_input">Select File %s</b><i class="current"></i>' % substitutions['input']
-        
-        clear_ui = "" # None by default
-        if not self.is_required and substitutions.get('clear'):
-            clear_ui = '<span>Clear %s</span>' % substitutions['clear']
-
-        output_list = ['<span class="imgWidget">',]
-        if substitutions.get('initial'):
-            output_list += [
-                '<b class="thumb">',
-                    img,
-                '</b>',
-                '<i class="context">',
-                        '<span>%s</span>' % substitutions['initial'],
-                        '<span>Change {1} {0}</span>'.format(styleable_input, self._crop_button(substitutions)),
-                        clear_ui,
-                '</i>',
-            ]
-        else:
-            output_list += [
-                styleable_input,
-            ]
-
-        output_list += ['</span>',] # close wrapper
-        output = u"".join(output_list)
+        return render_to_string('cropper/crop_widget.html', context)
 
-        return mark_safe(output)
 
 
 setup(
     name='Scruffy Cropper',
-    version="0.1.3",
+    version="0.2.0",
     author='Jason Goldstein',
     author_email='jason@betheshoe.com',
     url='https://bitbucket.org/whatisjasongoldstein/django-cropper',