more api changes, preparing for Android app
[wolnelektury.git] / apps / catalogue / views.py
index a6f394f..aedc4ca 100644 (file)
@@ -28,6 +28,7 @@ from django.utils.functional import Promise
 from django.utils.encoding import force_unicode
 from django.utils.http import urlquote_plus
 from django.views.decorators import cache
+from django.utils import translation
 from django.utils.translation import ugettext as _
 from django.views.generic.list_detail import object_list
 
@@ -82,7 +83,7 @@ def book_list(request, filter=None, template_name='catalogue/book_list.html'):
     form = forms.SearchForm()
 
     books_by_parent = {}
-    books = models.Book.objects.all().order_by('parent_number', 'title').only('title', 'parent', 'slug')
+    books = models.Book.objects.all().order_by('parent_number', 'sort_key').only('title', 'parent', 'slug')
     if filter:
         books = books.filter(filter).distinct()
         book_ids = set((book.pk for book in books))
@@ -193,14 +194,10 @@ def tagged_object_list(request, tags=''):
 
             objects = fragments
     else:
-        # get relevant books and their tags
-        objects = models.Book.tagged.with_all(tags)
-        if not shelf_is_set:
-            # eliminate descendants
-            l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in objects])
-            descendants_keys = [book.pk for book in models.Book.tagged.with_any(l_tags)]
-            if descendants_keys:
-                objects = objects.exclude(pk__in=descendants_keys)
+        if shelf_is_set:
+            objects = models.Book.tagged.with_all(tags)
+        else:
+            objects = models.Book.tagged_top_level(tags)
 
         # get related tags from `tag_counter` and `theme_counter`
         related_counts = {}
@@ -232,7 +229,6 @@ def tagged_object_list(request, tags=''):
             'only_author': only_author,
             'only_my_shelf': only_my_shelf,
             'formats_form': forms.DownloadFormatsForm(),
-
             'tags': tags,
         }
     )
@@ -258,7 +254,7 @@ def book_detail(request, slug):
     book_tag = book.book_tag()
     tags = list(book.tags.filter(~Q(category='set')))
     categories = split_tags(tags)
-    book_children = book.children.all().order_by('parent_number', 'title')
+    book_children = book.children.all().order_by('parent_number', 'sort_key')
     
     _book = book
     parents = []
@@ -491,13 +487,14 @@ def json_tags_starting_with(request, callback=None):
     if len(prefix) < 2:
         return HttpResponse('')
     tags_list = []
-    result = ""   
     for tag in _tags_starting_with(prefix, request.user):
         if not tag.name in tags_list:
-            result += "\n" + tag.name
             tags_list.append(tag.name)
-    dict_result = {"matches": tags_list}
-    return JSONResponse(dict_result, callback)
+    if request.GET.get('mozhint', ''):
+        result = [prefix, tags_list]
+    else:
+        result = {"matches": tags_list}
+    return JSONResponse(result, callback)
 
 # ====================
 # = Shelf management =
@@ -679,6 +676,27 @@ def delete_shelf(request, slug):
 # ==================
 # = Authentication =
 # ==================
+
+@cache.never_cache
+def simple_login(request):
+    if request.method == "GET":
+        #next = request.REQUEST.get('next', '')
+        #if next == '':
+        form = AuthenticationForm(prefix='login')
+        return render_to_response('auth/login.html', locals(),
+                context_instance=RequestContext(request))
+        #else:
+        #    return HttpResponseRedirect("/"+next)
+            
+    elif request.method == "POST":
+        form = AuthenticationForm(data=request.POST, prefix='login')
+        if form.is_valid():
+            auth.login(request, form.get_user())   
+        url = request.META['HTTP_REFERER'].split("next=")[1]
+        url = url.replace("%3F","?").replace("%3D","=")
+        return HttpResponseRedirect(url)    
+
+
 @require_POST
 @cache.never_cache
 def login(request):
@@ -764,3 +782,18 @@ def xmls(request):
     temp.seek(0)
     response.write(temp.read())
     return response
+
+
+
+# info views for API
+
+def book_info(request, id, lang='pl'):
+    book = get_object_or_404(models.Book, id=id)
+    # set language by hand
+    translation.activate(lang)
+    return render_to_response('catalogue/book_info.html', locals(),
+        context_instance=RequestContext(request))
+
+def tag_info(request, id):
+    tag = get_object_or_404(models.Tag, id=id)
+    return HttpResponse(tag.description)