gridftp: multiple fixes in handling and presenting errors
[qcg-portal.git] / filex / forms.py
1 # coding=utf-8
2 import os
3
4 from django import forms
5 from django.core.exceptions import ValidationError
6 from django.utils.http import urlquote
7
8 from filex.models import Favorite
9 from filex.utils import host_validator, path_validator, name_validator
10
11
12 def clean_path(path):
13     return urlquote(os.path.normpath(path), safe='/~')
14
15
16 class FavoriteForm(forms.ModelForm):
17     class Meta:
18         model = Favorite
19         fields = ('owner', 'host', 'path')
20         widgets = {'owner': forms.HiddenInput()}
21
22     def clean_path(self):
23         return os.path.normpath(self.cleaned_data['path'])
24
25
26 class HostForm(forms.Form):
27     host = forms.CharField(label=u'Host', max_length=256, validators=[host_validator], widget=forms.HiddenInput())
28
29
30 class HostPathForm(HostForm):
31     path = forms.CharField(label=u'Ścieżka', max_length=1024, validators=[path_validator], widget=forms.HiddenInput())
32
33     def clean_path(self):
34         return clean_path(self.cleaned_data['path'])
35
36
37 class HostPathNameForm(HostPathForm):
38     name = forms.CharField(label=u'Nazwa', max_length=256, validators=[name_validator])
39
40     def clean_name(self):
41         return clean_path(self.cleaned_data['name'])
42
43
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())
47
48     def __init__(self, data=None, *args, **kwargs):
49         super(HostItemsForm, self).__init__(data, *args, **kwargs)
50
51         if data is not None:
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'))
55
56     def clean(self):
57         data = super(HostItemsForm, self).clean()
58
59         if not (data.get('dirs') or data.get('files')):
60             raise ValidationError('No items specified')
61
62         return data
63
64     @staticmethod
65     def _clean_paths(values):
66         errors, cleaned = [], []
67         for name in values:
68             try:
69                 path_validator(name)
70             except ValidationError as e:
71                 e.message += ' - ' + name
72                 errors.append(e)
73             else:
74                 cleaned.append(clean_path(name))
75         if errors:
76             raise ValidationError(errors)
77
78         return cleaned
79
80     def clean_dirs(self):
81         return self._clean_paths(self.cleaned_data['dirs'])
82
83     def clean_files(self):
84         return self._clean_paths(self.cleaned_data['files'])
85     
86     
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])
90
91     def clean_src(self):
92         return clean_path(self.cleaned_data['src'])
93
94     def clean_dst(self):
95         return clean_path(self.cleaned_data['dst'])
96
97
98 class ExtractForm(HostPathForm):
99     dst = forms.CharField(label=u'Katalog docelowy', max_length=1024, validators=[path_validator])
100
101     def clean_dst(self):
102         return clean_path(self.cleaned_data['dst'])
103
104
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())
108
109     def __init__(self, data=None, *args, **kwargs):
110         super(CompressForm, self).__init__(data, *args, **kwargs)
111
112         if data is not None:
113             # accept user defined choices
114             self.fields['files'].choices += ((v, v) for v in data.getlist('files'))
115
116     def clean_files(self):
117         errors, cleaned = [], []
118         for name in self.cleaned_data['files']:
119             try:
120                 name_validator(name)
121             except ValidationError as e:
122                 e.message += ' - ' + name
123                 errors.append(e)
124             else:
125                 cleaned.append(clean_path(name))
126         if errors:
127             raise ValidationError(errors)
128
129         return cleaned
130
131     def clean_archive(self):
132         return clean_path(self.cleaned_data['archive'])
133
134
135 class ArchiveForm(CompressForm):
136     ZIP = '.zip'
137     GZIP = '.tar.gz'
138     BZIP = '.tar.bz2'
139
140     TYPE_CHOICES = (
141         (ZIP, 'zip'),
142         (GZIP, 'tar.gz'),
143         (BZIP, 'tar.bz2'),
144     )
145
146     type = forms.ChoiceField(label=u'Typ archiwum', choices=TYPE_CHOICES, initial=ZIP)