Commits

Anonymous committed 504e42f

got autocomplete back-end working and front-end almost working

  • Participants
  • Parent commits 968dc22

Comments (0)

Files changed (4)

File qfm/templates/register.html

 <head>
   <title>{{account.name|escape}} Register</title>
   <link rel="stylesheet" href="{{ MEDIA_URL }}finance.css" />
+
+  <!-- yui http://www.djangosnippets.org/snippets/392/
+       http://developer.yahoo.com/yui/autocomplete/
+    -->
+     <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}yui/autocomplete/assets/skins/sam/autocomplete.css" />
+	<!-- Dependencies --> 
+	<script type="text/javascript" src="{{ MEDIA_URL }}yui/yahoo-dom-event/yahoo-dom-event.js"></script> 
+	 
+	<!-- OPTIONAL: Connection (required only if using XHR DataSource) --> 
+	<script type="text/javascript" src="{{ MEDIA_URL }}yui/connection/connection-min.js"></script> 
+	 
+	<!-- OPTIONAL: Animation (required only if enabling animation) --> 
+	<script type="text/javascript" src="{{ MEDIA_URL }}yui/animation/animation-min.js"></script> 
+	 
+	<!-- OPTIONAL: External JSON parser from http://www.json.org/ (enables JSON validation) -->
+	<script type="text/javascript" src="http://www.json.org/json.js"></script>
+	 
+	<!-- Source file --> 
+	<script type="text/javascript" src="{{ MEDIA_URL }}yui/autocomplete/autocomplete-min.js"></script> 
+    <!-- /yui-->
+
 </head>
 <body>
 <h1>{{account.name|escape}}</h1>
 {% endfor %}
 </table>
 
+
+<form action="{{ account.get_absolute_url }}" method="post">
+<hr /><h3>Transaction form@@</h3>
+<ul>
+  {{ txform.as_ul }}
+</ul>
+<hr />
+</form>
+
+
 <hr />
 <address>
 qfm by Dan Connolly Oct 2007
     (r'^$', 'dm93data.qfm.views.accounts'),
     (r'^register/(?P<acct_id>\d+)', 'dm93data.qfm.views.register'),
     (r'^export', 'dm93data.qfm.views.export'),
+    (r'^api/categories/', 'dm93data.qfm.views.category_choices'),
 )
 
 if settings.DEBUG:

File qfm/views.py

 from django.http import HttpResponse
 from django.template import loader, RequestContext
 from django.utils import simplejson
+from django import newforms as forms
+from django.core.urlresolvers import reverse
+
+from widgets import AutoCompleteWidget
 
 def accounts(request):
     accounts = Account.objects.filter(kind="AL")
     from django.conf import settings
     return RequestContext(request, {"MEDIA_URL": settings.MEDIA_URL})
 
+class TransactionForm(forms.Form):
+    category = forms.CharField()
+
+    def __init__(self, *args, **kwargs):
+        super(TransactionForm, self).__init__(*args, **kwargs)
+        # cribbed from http://www.djangosnippets.org/snippets/392/
+        w = self.fields['category'].widget = AutoCompleteWidget()
+        w.lookup_url = reverse('dm93data.qfm.views.category_choices')
+        w.schema = '["choices", "name"]' 
+
 def register(request, acct_id):
     account = Account.objects.get(id=int(acct_id))
     transactions = account.transaction_set.all()
     return render_to_response('register.html',
                               {'account': account,
                                'balance': bal,
-                               'transactions': transactions},
+                               'transactions': transactions,
+                               'txform': TransactionForm()},
                               context_instance=media_too(request)
                               )
 
                                     })
     return HttpResponse(body, mimetype="text/plain")
 
+def category_choices(request):
+    # http://www.djangosnippets.org/snippets/392/
+    # http://developer.yahoo.com/yui/autocomplete/
+    #http://superb-west.dl.sourceforge.net/sourceforge/yui/yui_2.3.1.zip
+    hint = request.GET.get('query', '')
+    accts = Account.objects.filter(kind="IE", name__istartswith = hint)
+    return JsonResponse({'choices': [ {'id': a.id,
+                                       'name': a.name}
+                                      for a in accts]})
+
+
+class JsonResponse(HttpResponse):
+    # cribbed from http://www.djangosnippets.org/snippets/154/
+    def __init__(self, object):
+        content = simplejson.dumps(object)
+        super(JsonResponse, self).__init__(content, mimetype='application/json')

File qfm/widgets.py

+# cribbed from
+# YUI Autocomplete
+# August 27, 2007
+# http://www.djangosnippets.org/snippets/392/
+
+from django import newforms as forms
+from django.newforms.util import smart_unicode
+from django.utils.html import escape
+
+AC_SNIPPET = """
+<div class="ac_container yui-skin-sam">
+    <input class="autocomplete_widget" id="%(id)s" name="%(name)s" type="text" value="%(value)s" />
+    <div id="%(id)s_container"></div>
+
+    <script type="text/javascript">
+        var acDataSource_%(id)s = new YAHOO.widget.DS_XHR("%(url)s", %(schema)s);
+        var acAutoComp_%(id)s = new YAHOO.widget.AutoComplete("%(id)s","%(id)s_container", acDataSource_%(id)s);
+        acAutoComp_%(id)s.useIFrame = true;
+        acAutoComp_%(id)s.typeAhead = true; 
+    </script>
+</div>
+"""
+
+class AutoCompleteWidget(forms.widgets.TextInput):
+    """ widget autocomplete dla zwyklych pol tekstowych (nie FK)
+    """
+    # YUI schema
+    schema = None
+    # url for YUI XHR Datasource
+    lookup_url = None
+
+    def render(self, name, value, attrs={}):
+        html_id = attrs.get('id', name)
+
+        if value: value = smart_unicode(value)
+        else: value = ''
+        
+        return AC_SNIPPET % {'id': html_id,
+                             'name': name,
+                             'value': escape(value),
+                             'url': self.lookup_url,
+                             'schema': self.schema}
+    
+
+def _test():
+    print AutoCompleteWidget().render('field_name', '')
+
+if __name__ == '__main__':
+    _test()