update editor
[redakcja.git] / apps / wiki / views.py
index 7101fc9..71c1806 100644 (file)
@@ -52,19 +52,19 @@ def get_history(chunk):
 
 
 @never_cache
 
 
 @never_cache
-# @login_required
 def editor(request, slug, chunk=None, template_name='wiki/bootstrap.html'):
     try:
         chunk = Chunk.get(slug, chunk)
     except Chunk.MultipleObjectsReturned:
 def editor(request, slug, chunk=None, template_name='wiki/bootstrap.html'):
     try:
         chunk = Chunk.get(slug, chunk)
     except Chunk.MultipleObjectsReturned:
-        # TODO: choice page
         raise Http404
     except Chunk.DoesNotExist:
         if chunk is None:
             try:
         raise Http404
     except Chunk.DoesNotExist:
         if chunk is None:
             try:
-                book = Book.objects.get(slug=slug)
+                Book.objects.get(slug=slug)
             except Book.DoesNotExist:
                 return http.HttpResponseRedirect(reverse("catalogue_create_missing", args=[slug]))
             except Book.DoesNotExist:
                 return http.HttpResponseRedirect(reverse("catalogue_create_missing", args=[slug]))
+            else:
+                raise Http404
         else:
             raise Http404
     if not chunk.book.accessible(request):
         else:
             raise Http404
     if not chunk.book.accessible(request):
@@ -75,7 +75,7 @@ def editor(request, slug, chunk=None, template_name='wiki/bootstrap.html'):
     last_books[slug, chunk.slug] = {
         'time': access_time,
         'title': chunk.pretty_name(),
     last_books[slug, chunk.slug] = {
         'time': access_time,
         'title': chunk.pretty_name(),
-        }
+    }
 
     if len(last_books) > MAX_LAST_DOCS:
         oldest_key = min(last_books, key=lambda x: last_books[x]['time'])
 
     if len(last_books) > MAX_LAST_DOCS:
         oldest_key = min(last_books, key=lambda x: last_books[x]['time'])
@@ -84,13 +84,15 @@ def editor(request, slug, chunk=None, template_name='wiki/bootstrap.html'):
 
     save_form = forms.DocumentTextSaveForm(user=request.user, chunk=chunk, prefix="textsave")
     try:
 
     save_form = forms.DocumentTextSaveForm(user=request.user, chunk=chunk, prefix="textsave")
     try:
-        version = int(request.GET.get('version', None))
-    except:
+        version = int(request.GET.get('version', ''))
+    except ValueError:
         version = None
     if version:
         text = chunk.at_revision(version).materialize()
     else:
         text = chunk.materialize()
         version = None
     if version:
         text = chunk.at_revision(version).materialize()
     else:
         text = chunk.materialize()
+    materials = set(os.listdir(os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, chunk.book.gallery)))
+    materials.update([n.rsplit('.', 1)[0] for n in materials])
     return render(request, template_name, {
         'serialized_document_data': json.dumps({
             'document': text,
     return render(request, template_name, {
         'serialized_document_data': json.dumps({
             'document': text,
@@ -110,7 +112,9 @@ def editor(request, slug, chunk=None, template_name='wiki/bootstrap.html'):
         },
         'tags': list(save_form.fields['stage_completed'].choices),
         'can_pubmark': request.user.has_perm('catalogue.can_pubmark'),
         },
         'tags': list(save_form.fields['stage_completed'].choices),
         'can_pubmark': request.user.has_perm('catalogue.can_pubmark'),
-        'slug': chunk.book.slug
+        'slug': chunk.book.slug,
+        'gallery': chunk.book.gallery,
+        'materials': json.dumps(list(materials)),
     })
 
 
     })
 
 
@@ -154,30 +158,8 @@ def text(request, chunk_id):
     if request.method == 'POST':
         form = forms.DocumentTextSaveForm(request.POST, user=request.user, chunk=doc, prefix="textsave")
         if form.is_valid():
     if request.method == 'POST':
         form = forms.DocumentTextSaveForm(request.POST, user=request.user, chunk=doc, prefix="textsave")
         if form.is_valid():
-            if request.user.is_authenticated():
-                author = request.user
-            else:
-                author = None
-            text = form.cleaned_data['text']
+            form.save()
             parent_revision = form.cleaned_data['parent_revision']
             parent_revision = form.cleaned_data['parent_revision']
-            if parent_revision is not None:
-                parent = doc.at_revision(parent_revision)
-            else:
-                parent = None
-            stage = form.cleaned_data['stage_completed']
-            tags = [stage] if stage else []
-            publishable = form.cleaned_data['publishable'] and request.user.has_perm('catalogue.can_pubmark')
-            doc.commit(author=author,
-                       text=text,
-                       parent=parent,
-                       description=form.cleaned_data['comment'],
-                       tags=tags,
-                       author_name=form.cleaned_data['author_name'],
-                       author_email=form.cleaned_data['author_email'],
-                       publishable=publishable,
-                       )
-            doc.book.for_cybernauts = form.cleaned_data['for_cybernauts']
-            doc.book.save()
             revision = doc.revision()
             return JSONResponse({
                 'text': doc.materialize() if parent_revision != revision else None,
             revision = doc.revision()
             return JSONResponse({
                 'text': doc.materialize() if parent_revision != revision else None,
@@ -255,7 +237,7 @@ def gallery(request, directory):
             return urllib.quote("%s/%s" % (base_url, smart_unicode(filename)))
 
         def is_image(filename):
             return urllib.quote("%s/%s" % (base_url, smart_unicode(filename)))
 
         def is_image(filename):
-            return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
+            return os.path.splitext(filename)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
 
         images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
         images.sort()
 
         images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
         images.sort()