fix in handling file name with special characters
[qcg-portal.git] / filex / forms.py
index f73b175..e4d0c9e 100644 (file)
@@ -3,16 +3,9 @@ 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,}(?<!-)\.?))(?::\d+)?$', msg)
-path_validator = RegexValidator(r'^/(?:[^/\0]+/?)*$', msg)
-name_validator = RegexValidator(r'^[^/\0]+$', msg)
+from filex.utils import host_validator, path_validator, name_validator
 
 
 class FavoriteForm(forms.ModelForm):
@@ -21,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())
@@ -36,6 +32,9 @@ class HostPathForm(HostForm):
 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())