fix unicode errors
[qcg-portal.git] / filex / forms.py
index 26ca737..e4d0c9e 100644 (file)
@@ -3,21 +3,9 @@ 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
-
-
-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))
+from filex.utils import host_validator, path_validator, name_validator
 
 
 class FavoriteForm(forms.ModelForm):
@@ -26,6 +14,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())
@@ -35,12 +26,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 clean_path(self.cleaned_data['path'])
+        return os.path.normpath(self.cleaned_data['path'])
 
 
 class HostPathNameForm(HostPathForm):
     name = forms.CharField(label=u'Nazwa', max_length=256, validators=[name_validator])
 
+    def clean_name(self):
+        return os.path.normpath(self.cleaned_data['name'])
+
 
 class HostItemsForm(HostForm):
     dirs = forms.MultipleChoiceField(label=u'Katalogi', required=False, widget=forms.MultipleHiddenInput())
@@ -72,7 +66,7 @@ class HostItemsForm(HostForm):
                 e.message += ' - ' + name
                 errors.append(e)
             else:
-                cleaned.append(clean_path(name))
+                cleaned.append(os.path.normpath(name))
         if errors:
             raise ValidationError(errors)
 
@@ -90,17 +84,17 @@ class RenameForm(HostForm):
     dst = forms.CharField(label=u'Nowa nazwa', max_length=1024, validators=[path_validator])
 
     def clean_src(self):
-        return clean_path(self.cleaned_data['src'])
+        return os.path.normpath(self.cleaned_data['src'])
 
     def clean_dst(self):
-        return clean_path(self.cleaned_data['dst'])
+        return os.path.normpath(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 clean_path(self.cleaned_data['dst'])
+        return os.path.normpath(self.cleaned_data['dst'])
 
 
 class CompressForm(HostPathForm):
@@ -123,14 +117,14 @@ class CompressForm(HostPathForm):
                 e.message += ' - ' + name
                 errors.append(e)
             else:
-                cleaned.append(clean_path(name))
+                cleaned.append(os.path.normpath(name))
         if errors:
             raise ValidationError(errors)
 
         return cleaned
 
     def clean_archive(self):
-        return clean_path(self.cleaned_data['archive'])
+        return os.path.normpath(self.cleaned_data['archive'])
 
 
 class ArchiveForm(CompressForm):