Commits

Harut  committed b3109a7

correct rendering of insanities.forms.FileField "delete" checkbox

  • Participants
  • Parent commits 0ad4c2d
  • Branches unstable

Comments (0)

Files changed (3)

File insanities/forms/files.py

 
     def render(self, roles=None):
         value = self.parent.python_data.get(self.name, None)
+        delete = self.form.data.get(self.input_name + '__delete', False)
         if value is None:
-            mode = 'empty'
+            value = self.parent.initial.get(self.name, None)
+            if isinstance(value, StoredFile):
+                mode = 'existing'
+            else:
+                value = None
+                mode = 'empty'
         elif isinstance(value, StoredFile):
             mode = 'existing'
         elif isinstance(value, self.temp_file_cls):
             assert None
         return self.env.render('widgets/%s' % self.template, value=value,
                                mode=mode, input_name=self.input_name,
-                               temp_url=self.env.temp_url, null=self.null)
+                               delete=delete, temp_url=self.env.temp_url,
+                               null=self.null)
 
     def delete_temp_file(self, temp_name):
         uid, ext = path.splitext(temp_name)

File insanities/forms/form.py

 
 
 class FormEnvironment(object):
-    '''
-        Default environment class. Passes data to form template and renders it.
-    '''
+    
     def __init__(self, jinja_env, globals={}, locals={}, **kwargs):
         self.jinja_env = jinja_env
         self.globals = globals
         self.env = env
         self.name = name
         self.data = data = MultiDict()
+        self.initial = initial
         self.python_data = initial.copy()
         # clone all fields
         self.fields = [field(parent=self) for field in self.fields]

File insanities/forms/templates/widgets/fileinput.html

 {% endif %}
 {% if mode != 'empty' and null %}
   <label for="id-{{ input_name }}__delete">удалить</label>
-  <input type="checkbox" name="{{ input_name }}__delete" id="id-{{ input_name }}__delete" />
+  <input type="checkbox" name="{{ input_name }}__delete" id="id-{{ input_name }}__delete"
+         {% if delete %}checked="checked"{% endif %} />
   <br/>
 {% endif %}
 <input type="file" name="{{ input_name }}__file" />