1. Waldemar Kornewald
  2. djangotoolbox

Commits

Thomas Wanschik  committed 795b543

refactored BlobField

  • Participants
  • Parent commits 77c5072
  • Branches default

Comments (0)

Files changed (2)

File fields.py

View file
 from django.db import models
-from django.forms import FileField, widgets
-from django.utils.safestring import mark_safe
 
 class ListField(models.Field):
     def __init__(self, field_type, *args, **kwargs):
     def formfield(self, **kwargs):
         return None
 
-class BlobWidget(widgets.FileInput):
-    def render(self, name, value, attrs=None):
-        try:
-            blob_size = len(value)
-        except:
-            blob_size = 0
-        original = super(BlobWidget, self).render(name, value, attrs=None)
-        return mark_safe('%s<p>Current Blob Length: %s</p>' % (original,
-                                                               blob_size))
-
 class BlobField(models.Field):
     """
-    A field for storing blobs of binary data though App Engine's binary field
-    property.
+    A field for storing blobs of binary data
     """
-
-    def __init__(self, *args, **kwargs):
-        super(BlobField, self).__init__(*args, **kwargs)
-
     def get_internal_type(self):
         return 'BlobField'
 
     def formfield(self, **kwargs):
         # A file widget is provided, but use  model FileField or ImageField 
         # for storing specific files most of the time
+        from .widgets import BlobWidget
+        from django.forms import FileField
         defaults = {'form_class': FileField, 'widget': BlobWidget}
         defaults.update(kwargs)
-        field = super(BlobField, self).formfield(**defaults)
-
-        # I set the class so that any Blob Store upload middleware can ignore
-        # this input
-        field.widget.attrs['class'] = "BlobFieldFileInput"
-        return field
+        return super(BlobField, self).formfield(**defaults)
 
     def get_db_prep_value(self, value, connection, prepared=False):        
         try:
         raise TypeError("BlobFields do not support lookups")
 
     def value_to_string(self, obj):
-        value = self._get_val_from_obj(obj)
-        return str(value)
+        return str(self._get_val_from_obj(obj))

File widgets.py

View file
+from django.forms import widgets
+from django.template.defaultfilters import filesizeformat
+from django.utils.safestring import mark_safe
+
+class BlobWidget(widgets.FileInput):
+    def render(self, name, value, attrs=None):
+        try:
+            blob_size = len(value)
+        except:
+            blob_size = 0
+
+        blob_size = filesizeformat(blob_size)
+        original = super(BlobWidget, self).render(name, value, attrs=None)
+        return mark_safe('%s<p>Current size: %s</p>' % (original, blob_size))