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), safe='/~')
+from filex.utils import host_validator, path_validator, name_validator
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())
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())
e.message += ' - ' + name
errors.append(e)
else:
- cleaned.append(clean_path(name))
+ cleaned.append(os.path.normpath(name))
if errors:
raise ValidationError(errors)
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):
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):