update pyqcg version
[qcg-portal.git] / filex / forms.py
index f73b175..4f7c8dd 100644 (file)
@@ -3,16 +3,14 @@ import os
 
 from django import forms
 from django.core.exceptions import ValidationError
-from django.core.validators import RegexValidator
+from django.utils.http import urlquote
 
 from filex.models import Favorite
+from filex.utils import host_validator, path_validator, name_validator
 
 
-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,}(?<!-)\.?))(?::\d+)?$', msg)
-path_validator = RegexValidator(r'^/(?:[^/\0]+/?)*$', msg)
-name_validator = RegexValidator(r'^[^/\0]+$', msg)
+def clean_path(path):
+    return urlquote(os.path.normpath(path), safe='/~')
 
 
 class FavoriteForm(forms.ModelForm):
@@ -21,6 +19,9 @@ class FavoriteForm(forms.ModelForm):
         fields = ('owner', 'host', 'path')
         widgets = {'owner': forms.HiddenInput()}
 
+    def clean_path(self):
+        return os.path.normpath(self.cleaned_data['path'])
+
 
 class HostForm(forms.Form):
     host = forms.CharField(label=u'Host', max_length=256, validators=[host_validator], widget=forms.HiddenInput())
@@ -30,12 +31,15 @@ class HostPathForm(HostForm):
     path = forms.CharField(label=u'Ścieżka', max_length=1024, validators=[path_validator], widget=forms.HiddenInput())
 
     def clean_path(self):
-        return os.path.normpath(self.cleaned_data['path'])
+        return clean_path(self.cleaned_data['path'])
 
 
 class HostPathNameForm(HostPathForm):
     name = forms.CharField(label=u'Nazwa', max_length=256, validators=[name_validator])
 
+    def clean_name(self):
+        return clean_path(self.cleaned_data['name'])
+
 
 class HostItemsForm(HostForm):
     dirs = forms.MultipleChoiceField(label=u'Katalogi', required=False, widget=forms.MultipleHiddenInput())
@@ -67,7 +71,7 @@ class HostItemsForm(HostForm):
                 e.message += ' - ' + name
                 errors.append(e)
             else:
-                cleaned.append(os.path.normpath(name))
+                cleaned.append(clean_path(name))
         if errors:
             raise ValidationError(errors)
 
@@ -85,17 +89,17 @@ class RenameForm(HostForm):
     dst = forms.CharField(label=u'Nowa nazwa', max_length=1024, validators=[path_validator])
 
     def clean_src(self):
-        return os.path.normpath(self.cleaned_data['src'])
+        return clean_path(self.cleaned_data['src'])
 
     def clean_dst(self):
-        return os.path.normpath(self.cleaned_data['dst'])
+        return clean_path(self.cleaned_data['dst'])
 
 
 class ExtractForm(HostPathForm):
     dst = forms.CharField(label=u'Katalog docelowy', max_length=1024, validators=[path_validator])
 
     def clean_dst(self):
-        return os.path.normpath(self.cleaned_data['dst'])
+        return clean_path(self.cleaned_data['dst'])
 
 
 class CompressForm(HostPathForm):
@@ -118,14 +122,14 @@ class CompressForm(HostPathForm):
                 e.message += ' - ' + name
                 errors.append(e)
             else:
-                cleaned.append(os.path.normpath(name))
+                cleaned.append(clean_path(name))
         if errors:
             raise ValidationError(errors)
 
         return cleaned
 
     def clean_archive(self):
-        return os.path.normpath(self.cleaned_data['archive'])
+        return clean_path(self.cleaned_data['archive'])
 
 
 class ArchiveForm(CompressForm):