From 10f48e60a7f7499a6602098fc44298e05d05af50 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Rekucki?= Date: Fri, 4 Sep 2009 10:43:37 +0200 Subject: [PATCH 1/1] Poprawki do file upload i toolbar. --- apps/explorer/views.py | 49 +++++++++++++++++------ lib/hg.py | 8 ++-- project/static/js/codemirror/util.js | 2 +- project/static/js/jquery.logging.js | 9 ++--- project/templates/base.html | 2 +- project/templates/explorer/editor.html | 2 +- project/templates/explorer/file_list.html | 3 +- 7 files changed, 50 insertions(+), 25 deletions(-) diff --git a/apps/explorer/views.py b/apps/explorer/views.py index ed581911..0c558fbf 100644 --- a/apps/explorer/views.py +++ b/apps/explorer/views.py @@ -60,21 +60,44 @@ def file_list(request, repo): @permission_required('explorer.can_add_files') @with_repo def file_upload(request, repo): - form = forms.BookUploadForm(request.POST, request.FILES) - if form.is_valid(): - f = request.FILES['file'] - - def upload_action(): - print 'Adding file: %s' % f.name - repo._add_file(f.name, f.read().decode('utf-8')) - repo._commit(message="File %s uploaded from platform by %s" % - (f.name, request.user.username), user=request.user.username) - - repo.in_branch(upload_action, 'default') - return HttpResponseRedirect( reverse('editor_view', kwargs={'path': f.name}) ) + 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') + + def upload_action(): + print 'Adding file: %s' % f.name + repo._add_file(f.name, f.read().decode('utf-8')) + repo._commit( + message="File %s uploaded from platform by %s" %\ + (f.name, request.user.username), \ + user=request.user.username \ + ) + + # end of upload + + repo.in_branch(upload_action, 'default') + + # if everything is ok, redirect to the editor + return HttpResponseRedirect( reverse('editor_view', + kwargs={'path': f.name}) ) + + 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} ) + extra_context = {'form' : form, 'other_errors': other_errors}) # # Edit the file diff --git a/lib/hg.py b/lib/hg.py index 9e08e516..b94b0d64 100644 --- a/lib/hg.py +++ b/lib/hg.py @@ -7,8 +7,7 @@ import mercurial.merge, mercurial.error encoding.encoding = 'utf-8' -class RepositoryDoesNotExist(Exception): - pass + class Repository(object): """Abstrakcja repozytorium Mercurial. Działa z Mercurial w wersji 1.3.1.""" @@ -100,7 +99,7 @@ class Repository(object): except KeyError, ke: raise RepositoryException("Can't switch to branch '%s': no such branch." % bname , ke) except util.Abort, ae: - raise repositoryException("Can't switch to branch '%s': %s" % (bname, ae.message), ae) + raise RepositoryException("Can't switch to branch '%s': %s" % (bname, ae.message), ae) finally: wlock.release() @@ -114,3 +113,6 @@ class RepositoryException(Exception): def __init__(self, msg, cause=None): Exception.__init__(self, msg) self.cause = cause + +class RepositoryDoesNotExist(RepositoryException): + pass diff --git a/project/static/js/codemirror/util.js b/project/static/js/codemirror/util.js index a1390a91..61f927d1 100644 --- a/project/static/js/codemirror/util.js +++ b/project/static/js/codemirror/util.js @@ -112,5 +112,5 @@ function addEventHandler(node, type, handler, removeFunc) { } function nodeText(node) { - return node.innerText || node.textContent || node.nodeValue || ""; + return node.textContent || node.innerText || node.nodeValue || ""; } diff --git a/project/static/js/jquery.logging.js b/project/static/js/jquery.logging.js index 0f0df519..315d48f5 100644 --- a/project/static/js/jquery.logging.js +++ b/project/static/js/jquery.logging.js @@ -1,9 +1,8 @@ (function($) { - const LEVEL_DEBUG = 1; - const LEVEL_INFO = 2; - const LEVEL_WARN = 3; - - const LOG_LEVEL = LEVEL_DEBUG; + var LEVEL_DEBUG = 1; + var LEVEL_INFO = 2; + var LEVEL_WARN = 3; + var LOG_LEVEL = LEVEL_DEBUG; var mozillaLog = function() { if (window.console) diff --git a/project/templates/base.html b/project/templates/base.html index 86b63adb..54809993 100644 --- a/project/templates/base.html +++ b/project/templates/base.html @@ -12,7 +12,7 @@