From 391ec2a2b189609e0bca254adf6106ca9c742c70 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 28 Jan 2011 04:31:54 +0100 Subject: Implement a file search mecanism (refs #142) --- ishtar/furnitures/forms.py | 23 ++++++++- ishtar/furnitures/urls.py | 2 + ishtar/furnitures/views.py | 37 +++++++++++++++ ishtar/furnitures/widgets.py | 92 +++++++++++++++++++++++++++++++++++- ishtar/templates/base.html | 2 + ishtar/templates/default_wizard.html | 5 +- 6 files changed, 158 insertions(+), 3 deletions(-) (limited to 'ishtar') diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py index 82bfcb88e..2725815b4 100644 --- a/ishtar/furnitures/forms.py +++ b/ishtar/furnitures/forms.py @@ -1066,15 +1066,36 @@ class OperationWizard(Wizard): + datas return datas +class OperationSelect(forms.Form): + town = forms.IntegerField(label=_(u"Town"), + widget=widgets.JQueryAutoComplete("/" + settings.URL_PATH + \ + 'autocomplete-town', associated_model=models.Town), + validators=[models.valid_id(models.Town)]) + operation_type = forms.ChoiceField(label=_("Operation type"), + choices=models.OperationType.get_types()) + year = forms.IntegerField(label=_("Year")) + class OperationFormSelection(forms.Form): form_label = _("Operation") associated_models = {'pk':models.Operation} currents = {'pk':models.Operation} + pk = forms.IntegerField(label=_("Operation"), required=False, + widget=widgets.JQueryJqGrid(reverse_lazy('json-operation'), + OperationSelect(), ['operation_type', 'year']), + validators=[models.valid_id(models.Operation)]) + + def clean(self): + cleaned_data = self.cleaned_data + if 'pk' not in cleaned_data or not cleaned_data['pk']: + raise forms.ValidationError(_(u"You should select an operation.")) + return cleaned_data + + """ pk = forms.IntegerField(label=_("Operation"), widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-operation'), associated_model=models.Operation), validators=[models.valid_id(models.Operation)]) - +""" class OperationFormGeneral(forms.Form): form_label = _("General") diff --git a/ishtar/furnitures/urls.py b/ishtar/furnitures/urls.py index 08af1cd42..aafe0b3dd 100644 --- a/ishtar/furnitures/urls.py +++ b/ishtar/furnitures/urls.py @@ -71,6 +71,8 @@ urlpatterns += patterns('ishtar.furnitures.views', name='autocomplete-file'), url(BASE_URL + r'autocomplete-operation/$', 'autocomplete_operation', name='autocomplete-operation'), + url(BASE_URL + r'json-operation/$', 'json_operation', + name='json-operation'), url(BASE_URL + r'update-current-item/$', 'update_current_item', name='update-current-item'), ) diff --git a/ishtar/furnitures/views.py b/ishtar/furnitures/views.py index c9ea7a0fd..811618449 100644 --- a/ishtar/furnitures/views.py +++ b/ishtar/furnitures/views.py @@ -141,6 +141,43 @@ def autocomplete_operation(request, non_closed=True): for operation in operations]) return HttpResponse(data, mimetype='text/plain') +def json_operation(request, non_closed=True): + if not request.GET: + return HttpResponse(mimetype='text/plain') + request_keys = {'town':'towns__pk', + 'operation_type':'operation_type__pk', + 'operation_code':'operation_code', + 'year':'year', + 'value':'name', + } + dct = {} + for k in request_keys: + q = request.GET.get(k) + if not q: + continue + dct[request_keys[k]] = q + if not dct: + return HttpResponse(mimetype='text/plain') + query = Q(**dct) + operations = models.Operation.objects.filter(query) + q = request.GET.get('sidx') + if q and q in request_keys: + k = request_keys[q] + if k.endswith("__pk"): + k = k[:-len("__pk")] + "__label" + q = request.GET.get('sord') + if q and q == u'desc': + k = "-" + k + operations.order_by(k) + data = json.dumps({ + "records":len(operations), + "rows":[{"id":unicode(operation.pk), + "cell":(unicode(operation.pk), unicode(operation), + unicode(operation.operation_type), unicode(operation.year))} + for operation in operations] + }) + return HttpResponse(data, mimetype='text/plain') + def autocomplete_organization(request, orga_type=None): if not request.GET.get('term'): return HttpResponse(mimetype='text/plain') diff --git a/ishtar/furnitures/widgets.py b/ishtar/furnitures/widgets.py index dc00f8676..b1d49f660 100644 --- a/ishtar/furnitures/widgets.py +++ b/ishtar/furnitures/widgets.py @@ -48,7 +48,6 @@ class JQueryDate(forms.TextInput): """ % (name, settings.COUNTRY) return rendered - class JQueryAutoComplete(forms.TextInput): def __init__(self, source, associated_model=None, options={}, attrs={}): """ @@ -121,3 +120,94 @@ objects.get(pk=value)) 'js' : self.render_js(name), } +class JQueryJqGrid(forms.RadioSelect): + COL_TPL = "{name:'%(idx)s', index:'%(idx)s', sortable:true}" + class Media: + js = ['%s/js/i18n/grid.locale-%s.js' % (settings.MEDIA_URL, + settings.COUNTRY), + '%s/js/jquery.jqGrid.min.js' % settings.MEDIA_URL, + ] + css = {'all':['%s/media/ui.jqgrid.css' % settings.MEDIA_URL]} + + def __init__(self, source, form, cols, attrs={}): + self.source = source + self.form = form + self.attrs = attrs + self.cols = cols + + def render(self, name, value=None, attrs=None): + rendered = unicode(self.form) + """ + for k in self.form.fields: + field = self.form.fields[k] + print unicode(field) + print field.label_tag + rendered += u"%s%s" % (field.label_tag, + unicode(field)) + """ + rendered += """ + + +""" % (name, unicode(_("Search"))) + extra_cols = [] + col_names, col_idx = [], [] + for k in self.form.fields: + field = self.form.fields[k] + col_idx.append(u'"%s"' % k) + if k in self.cols: + col_names.append(u'"%s"' % field.label) + extra_cols.append(self.COL_TPL % {'idx':k}) + col_names = col_names and ",".join([""]+col_names) or "" + col_idx = col_idx and ",".join(col_idx) or "" + extra_cols = extra_cols and ",".join([""]+extra_cols) or "" + rendered += """
+""" % (name, name, name) + rendered += """ + +""" % {'name':name, 'col_names':col_names, 'extra_cols':extra_cols, + 'name_label':unicode(_("Name")), 'source':unicode(self.source), + 'col_idx':col_idx} + return rendered + diff --git a/ishtar/templates/base.html b/ishtar/templates/base.html index e726cf782..22e6d4239 100644 --- a/ishtar/templates/base.html +++ b/ishtar/templates/base.html @@ -16,6 +16,8 @@ + {% block extra_head %} + {% endblock %} {% endblock %} -- cgit v1.2.3