4 from django import forms
5 from django.core.exceptions import ValidationError
6 from django.utils.http import urlquote
8 from filex.models import Favorite
9 from filex.utils import host_validator, path_validator, name_validator
13 return urlquote(os.path.normpath(path), safe='/~')
16 class FavoriteForm(forms.ModelForm):
19 fields = ('owner', 'host', 'path')
20 widgets = {'owner': forms.HiddenInput()}
23 return os.path.normpath(self.cleaned_data['path'])
26 class HostForm(forms.Form):
27 host = forms.CharField(label=u'Host', max_length=256, validators=[host_validator], widget=forms.HiddenInput())
30 class HostPathForm(HostForm):
31 path = forms.CharField(label=u'Ścieżka', max_length=1024, validators=[path_validator], widget=forms.HiddenInput())
34 return clean_path(self.cleaned_data['path'])
37 class HostPathNameForm(HostPathForm):
38 name = forms.CharField(label=u'Nazwa', max_length=256, validators=[name_validator])
41 return clean_path(self.cleaned_data['name'])
44 class HostItemsForm(HostForm):
45 dirs = forms.MultipleChoiceField(label=u'Katalogi', required=False, widget=forms.MultipleHiddenInput())
46 files = forms.MultipleChoiceField(label=u'Pliki', required=False, widget=forms.MultipleHiddenInput())
48 def __init__(self, data=None, *args, **kwargs):
49 super(HostItemsForm, self).__init__(data, *args, **kwargs)
52 # accept user defined choices
53 self.fields['dirs'].choices += ((v, v) for v in data.getlist('dirs'))
54 self.fields['files'].choices += ((v, v) for v in data.getlist('files'))
57 data = super(HostItemsForm, self).clean()
59 if not (data.get('dirs') or data.get('files')):
60 raise ValidationError('No items specified')
65 def _clean_paths(values):
66 errors, cleaned = [], []
70 except ValidationError as e:
71 e.message += ' - ' + name
74 cleaned.append(clean_path(name))
76 raise ValidationError(errors)
81 return self._clean_paths(self.cleaned_data['dirs'])
83 def clean_files(self):
84 return self._clean_paths(self.cleaned_data['files'])
87 class RenameForm(HostForm):
88 src = forms.CharField(label=u'Stara nazwa', max_length=1024, validators=[path_validator], widget=forms.HiddenInput())
89 dst = forms.CharField(label=u'Nowa nazwa', max_length=1024, validators=[path_validator])
92 return clean_path(self.cleaned_data['src'])
95 return clean_path(self.cleaned_data['dst'])
98 class ExtractForm(HostPathForm):
99 dst = forms.CharField(label=u'Katalog docelowy', max_length=1024, validators=[path_validator])
102 return clean_path(self.cleaned_data['dst'])
105 class CompressForm(HostPathForm):
106 archive = forms.CharField(label=u'Nazwa', max_length=1024, validators=[path_validator])
107 files = forms.MultipleChoiceField(label=u'Pliki', widget=forms.MultipleHiddenInput())
109 def __init__(self, data=None, *args, **kwargs):
110 super(CompressForm, self).__init__(data, *args, **kwargs)
113 # accept user defined choices
114 self.fields['files'].choices += ((v, v) for v in data.getlist('files'))
116 def clean_files(self):
117 errors, cleaned = [], []
118 for name in self.cleaned_data['files']:
121 except ValidationError as e:
122 e.message += ' - ' + name
125 cleaned.append(clean_path(name))
127 raise ValidationError(errors)
131 def clean_archive(self):
132 return clean_path(self.cleaned_data['archive'])
135 class ArchiveForm(CompressForm):
146 type = forms.ChoiceField(label=u'Typ archiwum', choices=TYPE_CHOICES, initial=ZIP)