some book merging automation
[redakcja.git] / apps / catalogue / views.py
index a44c7e8..3c58f3f 100644 (file)
@@ -19,11 +19,10 @@ from django.views.generic.simple import direct_to_template
 import librarian.html
 import librarian.text
 
 import librarian.html
 import librarian.text
 
-from apiclient import api_call
 from catalogue import forms
 from catalogue import helpers
 from catalogue.helpers import active_tab
 from catalogue import forms
 from catalogue import helpers
 from catalogue.helpers import active_tab
-from catalogue.models import Book, Chunk
+from catalogue.models import Book, Chunk, BookPublishRecord, ChunkPublishRecord
 from catalogue import xml_tools
 
 #
 from catalogue import xml_tools
 
 #
@@ -34,30 +33,35 @@ from django.views.decorators.cache import never_cache
 logger = logging.getLogger("fnp.catalogue")
 
 
 logger = logging.getLogger("fnp.catalogue")
 
 
+def slug_filter(qs, value, filter_field, model, model_field='slug', unset=''):
+    if value == unset:
+        return qs.filter(**{filter_field: None})
+    if value is None:
+        return qs
+    try:
+        obj = model._default_manager.get(**{model_field: value})
+    except model.DoesNotExist:
+        return qs.none()
+    else:
+        return qs.filter(**{filter_field: obj})
+
+
 @active_tab('all')
 @never_cache
 @active_tab('all')
 @never_cache
-def document_list(request):
-    chunks_list = helpers.ChunksList(Chunk.objects.order_by(
-        'book__title', 'book', 'number'))
-
-    return direct_to_template(request, 'catalogue/document_list.html', extra_context={
-        'books': chunks_list,
-        #'books': [helpers.BookChunks(b) for b in Book.objects.all().select_related()],
-        'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
-                        key=lambda x: x[1]['time'], reverse=True),
-    })
+def document_list(request, filters=None):
+    chunks = Chunk.objects.order_by('book__title', 'book', 'number')
 
 
+    chunks = slug_filter(chunks, request.GET.get('user', None), 'user', User, 'username')
+    chunks = slug_filter(chunks, request.GET.get('stage', None), 'stage', Chunk.tag_model, 'slug')
 
 
-@active_tab('unassigned')
-@never_cache
-def unassigned(request):
-    chunks_list = helpers.ChunksList(Chunk.objects.filter(
-        user=None).order_by('book__title', 'book__id', 'number'))
+    chunks_list = helpers.ChunksList(chunks)
 
     return direct_to_template(request, 'catalogue/document_list.html', extra_context={
         'books': chunks_list,
         'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
                         key=lambda x: x[1]['time'], reverse=True),
 
     return direct_to_template(request, 'catalogue/document_list.html', extra_context={
         'books': chunks_list,
         'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
                         key=lambda x: x[1]['time'], reverse=True),
+        'stages': Chunk.tag_model.objects.all(),
+        'users': User.objects.annotate(count=Count('chunk')).order_by('-count', 'last_name', 'first_name'),
     })
 
 
     })
 
 
@@ -78,6 +82,8 @@ def user(request, username=None):
         'books': chunks_list,
         'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
                         key=lambda x: x[1]['time'], reverse=True),
         'books': chunks_list,
         'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
                         key=lambda x: x[1]['time'], reverse=True),
+        'viewed_user': user,
+        'stages': Chunk.tag_model.objects.all(),
     })
 my = login_required(active_tab('my')(user))
 
     })
 my = login_required(active_tab('my')(user))
 
@@ -409,10 +415,8 @@ def book_edit(request, slug):
 def publish(request, slug):
     book = get_object_or_404(Book, slug=slug)
     try:
 def publish(request, slug):
     book = get_object_or_404(Book, slug=slug)
     try:
-        ret = api_call(request.user, "books", {"book_xml": book.materialize()})
+        book.publish(request.user)
     except BaseException, e:
         return http.HttpResponse(e)
     else:
     except BaseException, e:
         return http.HttpResponse(e)
     else:
-        book.last_published = datetime.now()
-        book.save()
         return http.HttpResponseRedirect(book.get_absolute_url())
         return http.HttpResponseRedirect(book.get_absolute_url())