From edd1abec6c3948dd007a7a7d75ddcc28e9197e41 Mon Sep 17 00:00:00 2001 From: Maciej Tronowski Date: Fri, 17 Apr 2015 18:03:25 +0200 Subject: [PATCH] validate user data in gridftp --- filex/forms.py | 132 ++++++++++++++-- filex/ftp.py | 20 ++- filex/static/filex/filex.js | 5 +- filex/templates/filex/upload.js.html | 2 +- filex/uploadhandler.py | 13 +- filex/urls.py | 16 +- filex/views.py | 287 +++++++++++++--------------------- qcg/templates/qcg/gridftp.html | 84 ++++++---- qcg/views.py | 4 +- 9 files changed, 315 insertions(+), 248 deletions(-) diff --git a/filex/forms.py b/filex/forms.py index db6587b..f73b175 100644 --- a/filex/forms.py +++ b/filex/forms.py @@ -1,9 +1,20 @@ # coding=utf-8 +import os + from django import forms +from django.core.exceptions import ValidationError +from django.core.validators import RegexValidator from filex.models import Favorite +msg = u'Invalid value' +host_validator = RegexValidator(r'^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+' + r'(?:[a-zA-Z]{2,6}\.?|[a-zA-Z0-9-]{2,}(?', {text: msg})); + var error = (xhr.responseJSON || {}).error || undefined; + + if (typeof error === 'string') + $errorModal.find('.modal-body').append($('
', {text: error}));
+
                     filex.idle();
                     $errorModal.modal();
 
@@ -47,6 +52,21 @@
                 };
             }
 
+            function conflictingName(name, modal) {
+                if (filex.files.some(function(item) { return item.get('name') == name })) {
+                    modal.find('.alert').remove();
+
+                    $('
', { + 'class': 'alert alert-danger', + html: ' Plik o podanej już nazwie istnieje!' + }).prependTo(modal.find('.modal-body')); + + return true; + } + + return false; + } + filex.files.on('change:checked reset', function() { var selected = filex.selectedFiles().length; @@ -68,7 +88,7 @@ }); $('#btn-upload').on('click', function() { - var url = '{% url 'gridftp_upload' %}?' + $.param({host: filex.host, path: filex.path.full() + '/'}); + var url = '{% url 'gridftp_upload' %}?' + $.param({host: filex.host, path: filex.path.full()}); var win = window.open(url, url, 'height=500,width=800'); win.focus(); @@ -76,11 +96,11 @@ $('#btn-delete').on('click', function() { var selected = _.groupBy(filex.selectedFiles(), function(item) { return item.get('type') }), - dirs = _.map(selected.directory || [], function (item) { return item.get('name') }), - files = _.map(selected.file || [], function (item) { return item.get('name') }), + path = filex.path.full() + '/', + dirs = _.map(selected.directory || [], function (item) { return path + item.get('name') }), + files = _.map(selected.file || [], function (item) { return path + item.get('name') }), data = { host: filex.host, - path: filex.path.full() + '/', dirs: dirs, files: files }, @@ -106,8 +126,10 @@ $errorModal.find('#error-modal-label').text('Błąd'); for (var i in keys) { - $('
', {text: keys[i]}).appendTo($errorList); - $('
', {text: response.fail[keys[i]]}).appendTo($errorList); + if(keys.hasOwnProperty(i)) { + $('
', {text: keys[i].replace(path, '')}).appendTo($errorList); + $('
', {text: response.fail[keys[i]]}).appendTo($errorList); + } } $errorModal.modal(); @@ -132,6 +154,10 @@ var $this = $(this); e.preventDefault(); + + if (conflictingName($this.find('#id_name').val(), $this)) + return; + filex.busy(); $this.modal('hide'); @@ -145,22 +171,26 @@ }); $('#rename-form').on('show.bs.modal', function() { - var $this = $(this); + $(this).find('#id_dst').val(filex.selectedFiles()[0].get('name')); + }).on('submit', function(e) { + e.preventDefault(); - var file = filex.selectedFiles()[0]; + var $this = $(this), + path = filex.path.full() + '/', + newName = $this.find('#id_dst').val(), + data = { + host: filex.host, + src: path + filex.selectedFiles()[0].get('name'), + dst: path + newName + }; - $this.find('#id_host').val(filex.host); - $this.find('#id_path').val(filex.path.full()); - $this.find('#id_src').val(file.get('name')); - $this.find('#id_dst').val(file.get('name')); - }).on('submit', function(e) { - var $this = $(this); + if (conflictingName(newName, $this)) + return; - e.preventDefault(); filex.busy(); $this.modal('hide'); - $.post($this.attr('action'), $this.serialize(), function() { + $.post($this.attr('action'), data, function() { status('Nazwę zmieniono pomyślnie'); filex.reloadFiles(); }, 'json').fail(failModal('Nie udało się zmienić nazwy')); @@ -170,27 +200,19 @@ e.preventDefault(); var $this = $(this), - name = $this.find('#id_name').val(), + name = $this.find('#id_archive').val(), type = $this.find('#id_type').val(), + path = filex.path.full(), archive = name + (name.endsWith(type) ? '' : type), data = { host: filex.host, - path: filex.path.full(), + path: path, files: _.map(filex.selectedFiles(), function (item) { return item.get('name') }), - archive: archive + archive: path + '/' + archive }; - // check if maybe file with given name exists - if (filex.files.some(function(item) { return item.get('name') == archive })) { - $this.find('.alert').remove(); - - $('
', { - 'class': 'alert alert-danger', - html: ' Plik o podanej już nazwie istnieje!' - }).prependTo($this.find('.modal-body')); - + if (conflictingName(archive, $this)) return; - } filex.busy(); $this.modal('hide'); @@ -206,7 +228,7 @@ var data = { host: filex.host, - archive: filex.path.full() + '/' + filex.selectedFiles()[0].get('name'), + path: filex.path.full() + '/' + filex.selectedFiles()[0].get('name'), dst: filex.path.full() }; diff --git a/qcg/views.py b/qcg/views.py index 72ab335..3f3532e 100644 --- a/qcg/views.py +++ b/qcg/views.py @@ -15,7 +15,7 @@ from django.utils.timezone import UTC from django_openid_auth.views import make_consumer from openid.extensions import ax -from filex.forms import NewDirForm, RenameForm, ArchiveForm +from filex.forms import HostPathNameForm, RenameForm, ArchiveForm from qcg.forms import FiltersForm, ColumnsForm, JobDescriptionForm, EnvFormSet from qcg.utils import paginator_context from qcg.service import update_user_data, submit_job @@ -184,7 +184,7 @@ def job_new(request): @login_required def gridftp(request): return render(request, 'qcg/gridftp.html', - {'new_dir_form': NewDirForm(), 'rename_form': RenameForm(), 'archive_form': ArchiveForm()}) + {'new_dir_form': HostPathNameForm(), 'rename_form': RenameForm(), 'archive_form': ArchiveForm()}) def gridftp_upload(request): -- 1.7.9.5