4 from django import forms
5 from django.core.exceptions import ValidationError
6 from django.core.validators import RegexValidator
7 from django.utils.http import urlquote
9 from filex.models import Favorite
12 msg = u'Invalid value'
13 host_validator = RegexValidator(r'^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+'
14 r'(?:[a-zA-Z]{2,6}\.?|[a-zA-Z0-9-]{2,}(?<!-)\.?))(?::\d+)?$', msg)
15 path_validator = RegexValidator(r'^~?(?:/[^/\0]*)*$', msg)
16 name_validator = RegexValidator(r'^[^/\0]+$', msg)
20 return urlquote(os.path.normpath(path), safe='/~')
23 class FavoriteForm(forms.ModelForm):
26 fields = ('owner', 'host', 'path')
27 widgets = {'owner': forms.HiddenInput()}
30 class HostForm(forms.Form):
31 host = forms.CharField(label=u'Host', max_length=256, validators=[host_validator], widget=forms.HiddenInput())
34 class HostPathForm(HostForm):
35 path = forms.CharField(label=u'Ścieżka', max_length=1024, validators=[path_validator], widget=forms.HiddenInput())
38 return clean_path(self.cleaned_data['path'])
41 class HostPathNameForm(HostPathForm):
42 name = forms.CharField(label=u'Nazwa', max_length=256, validators=[name_validator])
45 return clean_path(self.cleaned_data['name'])
48 class HostItemsForm(HostForm):
49 dirs = forms.MultipleChoiceField(label=u'Katalogi', required=False, widget=forms.MultipleHiddenInput())
50 files = forms.MultipleChoiceField(label=u'Pliki', required=False, widget=forms.MultipleHiddenInput())
52 def __init__(self, data=None, *args, **kwargs):
53 super(HostItemsForm, self).__init__(data, *args, **kwargs)
56 # accept user defined choices
57 self.fields['dirs'].choices += ((v, v) for v in data.getlist('dirs'))
58 self.fields['files'].choices += ((v, v) for v in data.getlist('files'))
61 data = super(HostItemsForm, self).clean()
63 if not (data.get('dirs') or data.get('files')):
64 raise ValidationError('No items specified')
69 def _clean_paths(values):
70 errors, cleaned = [], []
74 except ValidationError as e:
75 e.message += ' - ' + name
78 cleaned.append(clean_path(name))
80 raise ValidationError(errors)
85 return self._clean_paths(self.cleaned_data['dirs'])
87 def clean_files(self):
88 return self._clean_paths(self.cleaned_data['files'])
91 class RenameForm(HostForm):
92 src = forms.CharField(label=u'Stara nazwa', max_length=1024, validators=[path_validator], widget=forms.HiddenInput())
93 dst = forms.CharField(label=u'Nowa nazwa', max_length=1024, validators=[path_validator])
96 return clean_path(self.cleaned_data['src'])
99 return clean_path(self.cleaned_data['dst'])
102 class ExtractForm(HostPathForm):
103 dst = forms.CharField(label=u'Katalog docelowy', max_length=1024, validators=[path_validator])
106 return clean_path(self.cleaned_data['dst'])
109 class CompressForm(HostPathForm):
110 archive = forms.CharField(label=u'Nazwa', max_length=1024, validators=[path_validator])
111 files = forms.MultipleChoiceField(label=u'Pliki', widget=forms.MultipleHiddenInput())
113 def __init__(self, data=None, *args, **kwargs):
114 super(CompressForm, self).__init__(data, *args, **kwargs)
117 # accept user defined choices
118 self.fields['files'].choices += ((v, v) for v in data.getlist('files'))
120 def clean_files(self):
121 errors, cleaned = [], []
122 for name in self.cleaned_data['files']:
125 except ValidationError as e:
126 e.message += ' - ' + name
129 cleaned.append(clean_path(name))
131 raise ValidationError(errors)
135 def clean_archive(self):
136 return clean_path(self.cleaned_data['archive'])
139 class ArchiveForm(CompressForm):
150 type = forms.ChoiceField(label=u'Typ archiwum', choices=TYPE_CHOICES, initial=ZIP)