Skip to content
This repository
Browse code

Experimenting with returning field html

  • Loading branch information...
commit d57c0f6baa67ddb28f3320aecde7b0fd112b0f60 1 parent 11b1eb7
Charles Leifer authored September 04, 2012
24  flask_peewee/filters.py
@@ -270,17 +270,24 @@ def get_form(self, field_dict):
270 270
             field_dict,
271 271
         )
272 272
 
  273
+    def get_form_data(self, field_name, form):
  274
+        data = form.data
  275
+        import ipdb; ipdb.set_trace()
  276
+        for piece in field_name.split(self.separator):
  277
+            data = data[piece]
  278
+        return data
  279
+
  280
+    def get_query_field(self, field_name, form):
  281
+        field = form
  282
+        for piece in field_name.split(self.separator):
  283
+            field = field[piece]
  284
+        return field
  285
+
273 286
     def get_query_value(self, raw_field_name, form=None):
274 287
         if form is None:
275 288
             return request.args.getlist(raw_field_name)
276  
-        if self.separator in raw_field_name:
277  
-            pieces = raw_field_name.split(self.separator)
278  
-            data = form.data[pieces[0]]
279  
-            for piece in pieces[1:]:
280  
-                data = data[piece]
281  
-        else:
282  
-            data = form.data[raw_field_name]
283 289
 
  290
+        data = self.get_form_data(raw_field_name, form)
284 291
         if not isinstance(data, list):
285 292
             data = [data]
286 293
         return data
@@ -336,7 +343,8 @@ def process_request(self, query):
336 343
                 q_objects = []
337 344
                 for filter_idx, filter_value in zip(filter_idx_list, filter_value_list):
338 345
                     idx = int(filter_idx)
339  
-                    cleaned.append((qf_s, idx, qf_v, filter_value))
  346
+                    form_field = self.get_query_field(qf_v, form)
  347
+                    cleaned.append((qf_s, idx, qf_v, filter_value, form_field))
340 348
                     query_filter = self._query_filters[field][idx]
341 349
                     q_objects.append(query_filter.query(filter_value))
342 350
 
10  flask_peewee/static/js/admin.js
@@ -123,11 +123,15 @@ var Admin = window.Admin || {};
123 123
     });
124 124
   }
125 125
 
126  
-  ModelAdminFilter.prototype.add_row = function(qf_v, qf_s, ival, sval) {
  126
+  ModelAdminFilter.prototype.add_row = function(qf_v, qf_s, ival, sval, ifield) {
127 127
     var select_elem = this.lookups_elem.find('#'+qf_s),
128 128
         input_elem = this.lookups_elem.find('#'+qf_v),
129 129
         field_label = $('#filter-'+qf_s).text();
130 130
 
  131
+    if (ifield) {
  132
+        input_elem = $(ifield);
  133
+    }
  134
+
131 135
     var self = this,
132 136
         select_clone = select_elem.clone(),
133 137
         input_clone = input_elem.clone(),
@@ -173,8 +177,8 @@ var Admin = window.Admin || {};
173 177
   }
174 178
 
175 179
   /* pull request data and simulate adding a filter */
176  
-  ModelAdminFilter.prototype.add_filter_request = function(qf_s, filter_idx, qf_v, filter_val) {
177  
-    return this.add_row(qf_v, qf_s, filter_val, filter_idx);
  180
+  ModelAdminFilter.prototype.add_filter_request = function(qf_s, filter_idx, qf_v, filter_val, filter_field) {
  181
+    return this.add_row(qf_v, qf_s, filter_val, filter_idx, filter_field);
178 182
   }
179 183
 
180 184
   /* export */
4  flask_peewee/templates/admin/models/base_filters.html
@@ -13,9 +13,9 @@
13 13
        * special-case foreign key lookups since they have a `repr` that we'll
14 14
        * need to display on the button
15 15
        */
16  
-      {% for qf_s, filter_idx, qf_v, filter_val in active_filters %}
  16
+      {% for qf_s, filter_idx, qf_v, filter_val, form_field in active_filters %}
17 17
         admin_filter.add_filter_request(
18  
-          '{{ qf_s }}', {{ filter_idx }}, '{{ qf_v }}', {{ filter_val|tojson|safe }}
  18
+          '{{ qf_s }}', {{ filter_idx }}, '{{ qf_v }}', null, '{{ form_field()|safe }}'
19 19
         );
20 20
       {% endfor %}
21 21
     });

0 notes on commit d57c0f6

Please sign in to comment.
Something went wrong with that request. Please try again.