Fixed file upload.
[redakcja.git] / apps / explorer / views.py
index e3ca63a..9d01fc2 100644 (file)
@@ -38,7 +38,7 @@ def file_path(fileid):
 def with_repo(view):
     """Open a repository for this view"""
     def view_with_repo(request, *args, **kwargs):          
-        kwargs['repo'] = wlrepo.MercurialLibrary(settings.REPOSITORY_PATH)
+        kwargs['repo'] = wlrepo.open_library(settings.REPOSITORY_PATH, 'hg')
         return view(request, *args, **kwargs)
     return view_with_repo
 
@@ -59,51 +59,41 @@ def ajax_login_required(view):
 @with_repo
 def file_list(request, repo):   
     import api.forms
+    from api.resources import library_resource
+
     bookform = api.forms.DocumentUploadForm()
+
+    # short-circut the api document list
+    doctree = library_resource.handler.read(request)
+    # print "DOCTREE:", doctree['documents']
+        
     return direct_to_template(request, 'explorer/file_list.html', extra_context={
-        'files': repo.documents(), 'bookform': bookform,
+        'filetree': doctree['documents'], 'bookform': bookform,
     })
 
 @permission_required('explorer.can_add_files')
 @with_repo
 def file_upload(request, repo):
-    other_errors = []
-    if request.method == 'POST':
-        form = forms.BookUploadForm(request.POST, request.FILES)
-        if form.is_valid():
-            try:
-                # prepare the data
-                f = request.FILES['file']
-                decoded = f.read().decode('utf-8')
-                fileid = form.cleaned_data['bookname'].lower()
-                rpath = file_path(fileid)
-
-                if form.cleaned_data['autoxml']:
-                    decoded = librarian.wrap_text(decoded, unicode(date.today()) )
-                
-                def upload_action():
-                    repo._add_file(rpath, decoded.encode('utf-8') )
-                    repo._commit(message="File %s uploaded by user %s" % \
-                        (rpath, request.user.username), user=request.user.username)
-
-                repo.in_branch(upload_action, 'default')
+    from api.resources import library_resource
+    from api.forms import DocumentUploadForm
+    from django.http import HttpRequest, HttpResponseRedirect
 
-                # if everything is ok, redirect to the editor
-                return HttpResponseRedirect( reverse('editor_view',
-                        kwargs={'path': fileid}) )
+    response = library_resource.handler.create(request)
 
-            except hg.RepositoryException, e:
-                other_errors.append(u'Błąd repozytorium: ' + unicode(e) )
-            #except UnicodeDecodeError, e:
-            #    other_errors.append(u'Niepoprawne kodowanie pliku: ' + e.reason \
-            #     + u'. Żądane kodowanie: ' + e.encoding)
-        # invalid form
-
-    # get
-    form = forms.BookUploadForm()
-    return direct_to_template(request, 'explorer/file_upload.html',\
-        extra_context = {'form' : form, 'other_errors': other_errors})
-   
+    if isinstance(response, HttpResponse):
+        data = json.loads(response.content)
+        
+        if response.status_code == 201:
+            return HttpResponseRedirect( \
+                reverse("editor_view", args=[ data['name'] ]) )
+        else:
+            bookform = DocumentUploadForm(request.POST, request.FILES)
+            bookform.is_valid()
+            
+            return direct_to_template(request, 'explorer/file_upload.html',
+                extra_context={'bookform': bookform } )
+    
+       
 #
 # Edit the file
 #
@@ -348,27 +338,26 @@ def file_dc(request, path, repo):
 # Display the main editor view
 
 @login_required
-@with_repo
-def display_editor(request, path, repo):    
-
+# @with_repo
+def display_editor(request, path):    
     # this is the only entry point where we create an autobranch for the user
     # if it doesn't exists. All other views SHOULD fail.
-    def ensure_branch_exists():
-        parent = repo.get_branch_tip('default')
-        repo._create_branch(file_branch(path, request.user), parent)
+    #def ensure_branch_exists():
+    #    parent = repo.get_branch_tip('default')
+    #    repo._create_branch(file_branch(path, request.user), parent)
         
-    try:
-        repo.with_wlock(ensure_branch_exists)
+#    try:
+    #    repo.with_wlock(ensure_branch_exists)
         
-        return direct_to_template(request, 'explorer/editor.html', extra_context={
-            'fileid': path,
-            'panel_list': ['lewy', 'prawy'],
-            'availble_panels': models.EditorPanel.objects.all(),
-            'scriptlets': toolbar_models.Scriptlet.objects.all()
-        })
-    except KeyError:
-        return direct_to_template(request, 'explorer/nofile.html', \
-            extra_context = { 'fileid': path })
+    return direct_to_template(request, 'explorer/editor.html', extra_context={
+        'fileid': path,
+        'panel_list': ['lewy', 'prawy'],
+        'availble_panels': models.EditorPanel.objects.all(),
+        # 'scriptlets': toolbar_models.Scriptlet.objects.all()
+    })
+#    except KeyError:
+#        return direct_to_template(request, 'explorer/nofile.html', \
+#            extra_context = { 'fileid': path })
 
 # ===============
 # = Panel views =