Commits

Konstantine Rybnikov committed 8861ed7

autocomplete works now. Hurray!

Comments (0)

Files changed (6)

fat/fat_po_editor/bl/repository.py

 def get_all():
     return []
 
-class RepositoryAddError(Exception): pass
+class UserAlertError(Exception): pass
+
 def add(path):
     import os
     
     if not path:
-        raise RepositoryAddError(_(u'Repository path can not be empty.'))
+        raise UserAlertError(_(u'Repository path can not be empty.'))
     if not os.path.exists(path):
-        raise RepositoryAddError(_(u'Path %(path)s does not exist.') % dict(
+        raise UserAlertError(_(u'Path %(path)s does not exist.') % dict(
                 path=path))

fat/fat_po_editor/lib/watch.py

+# -*- coding: utf-8 -*-
+
+class watch_me(object):
+    """ Decorator to debug function
+    TODO (if you need) extend it with functionality
+    to accept lambda's for args, kw or ret_val filtering,
+    and maybe limit/offset.
+    """
+    def __init__(self, limit=None):
+        self.limit = limit
+    
+    def __call__(self, f):
+        def new_f(*args, **kw):
+            ret_val = f(*args, **kw)
+            
+            print((u"%(func)s(args=%(args)s, kw=%(kw)s) -> "
+                   u"%(ret_val)s") % dict(
+                    func=f.__name__,
+                    args=args,
+                    kw=kw,
+                    ret_val=ret_val
+                    )
+                  )
+
+            return ret_val
+            
+        return new_f

fat/fat_po_editor/templates/fat_po_editor/index.html

 <p>
     {% trans "Django is so much work to start :-(" %}
 </p>
+
 {% repositories_list repositories %}
+
 {% endblock %}

fat/fat_po_editor/util/json.py

 def json_http_response(data):
     return HttpResponse(simplejson.dumps(data),
                         mimetype="application/json")
+
+def json_http_response_success(data):
+    return json_http_response({
+            'status': 'success',
+            'data': data})
+
+def json_http_response_fail(data):
+    return json_http_response({
+            'status': 'fail',
+            'data': data})

fat/fat_po_editor/util/path.py

+# -*- coding: utf-8 -*-
+
+def autocomplete(path):
+    """Returns list of completion posibilities for a given path
+    """
+    import os
+    abspath = os.path.abspath(path)
+    if os.path.exists(abspath):
+        return [abspath] + [os.path.join(abspath, x) for x in os.listdir(abspath)]
+    else:
+        head, tail = os.path.split(abspath)
+        if os.path.exists(head):
+            return [
+                os.path.join(head, current_dir)
+                for current_dir in os.listdir(head)
+                if current_dir.startswith(tail)]

fat/fat_po_editor/views/repository.py

 from django.http import HttpResponse
 from django.utils.translation import ugettext as _
 
+from fat_po_editor.util.json import json_http_response_success
+from fat_po_editor.util.json import json_http_response_fail
+from fat_po_editor.util.json import json_http_response
 from fat_po_editor.bl import repository
-from fat_po_editor.util.json import json_http_response
 
 def add(request):
     if request.method == 'POST':
-        repository_path = request.POST.get('path')
+        repository_path = request.POST.get('path', '')
         try:
             repository.add(repository_path)
-        except repository.RepositoryAddError as e:
-            return json_http_response({
-                    'status': 'fail',
-                    'data': {
-                        'message': e.message}})
+        except repository.UserAlertError as e:
+            return json_http_response_fail({
+                        'message': e.message})
         
-        return json_http_response({
-                'status': 'success',
-                'data': {
-                    'message': _(u'Repository path %(repository_path)s was added') % dict(
-                        repository_path=repository_path)}})
+        return json_http_response_success({
+                'message': _(u'Repository path %(repository_path)s was added') % dict(
+                    repository_path=repository_path)})
     else:
         return HttpResponse(u'Waiting for POST data')
 
 def path_autocomplete(request):
-    pass
+    from fat_po_editor.util.path import autocomplete
+    
+    path = request.GET.get('term', '')
+    try:
+        paths = autocomplete(path)
+    except repository.UserAlertError as e:
+        return json_http_response_fail({
+                    'message': e.message})
+    return json_http_response(paths)