X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=filex%2Fforms.py;h=4f7c8ddc7d7412ab489af324e621e8fdfa483b6c;hb=5e547a8cc20ef2bd450b3b839564e56e99099cee;hp=db6587bae44521fed47d380aaf331b5f81171db4;hpb=a0feb25389615e9d2ab2b036a491a1aeeb059a52;p=qcg-portal.git diff --git a/filex/forms.py b/filex/forms.py index db6587b..4f7c8dd 100644 --- a/filex/forms.py +++ b/filex/forms.py @@ -1,7 +1,16 @@ # coding=utf-8 +import os + from django import forms +from django.core.exceptions import ValidationError +from django.utils.http import urlquote from filex.models import Favorite +from filex.utils import host_validator, path_validator, name_validator + + +def clean_path(path): + return urlquote(os.path.normpath(path), safe='/~') class FavoriteForm(forms.ModelForm): @@ -10,29 +19,128 @@ 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()) + + +class HostPathForm(HostForm): + 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']) + + +class HostPathNameForm(HostPathForm): + name = forms.CharField(label=u'Nazwa', max_length=256, validators=[name_validator]) + + def clean_name(self): + return clean_path(self.cleaned_data['name']) + + +class HostItemsForm(HostForm): + dirs = forms.MultipleChoiceField(label=u'Katalogi', required=False, widget=forms.MultipleHiddenInput()) + files = forms.MultipleChoiceField(label=u'Pliki', required=False, widget=forms.MultipleHiddenInput()) + + def __init__(self, data=None, *args, **kwargs): + super(HostItemsForm, self).__init__(data, *args, **kwargs) + + if data is not None: + # accept user defined choices + self.fields['dirs'].choices += ((v, v) for v in data.getlist('dirs')) + self.fields['files'].choices += ((v, v) for v in data.getlist('files')) + + def clean(self): + data = super(HostItemsForm, self).clean() + + if not (data.get('dirs') or data.get('files')): + raise ValidationError('No items specified') + + return data + + @staticmethod + def _clean_paths(values): + errors, cleaned = [], [] + for name in values: + try: + path_validator(name) + except ValidationError as e: + e.message += ' - ' + name + errors.append(e) + else: + cleaned.append(clean_path(name)) + if errors: + raise ValidationError(errors) + + return cleaned + + def clean_dirs(self): + return self._clean_paths(self.cleaned_data['dirs']) + + def clean_files(self): + return self._clean_paths(self.cleaned_data['files']) + + +class RenameForm(HostForm): + src = forms.CharField(label=u'Stara nazwa', max_length=1024, validators=[path_validator], widget=forms.HiddenInput()) + dst = forms.CharField(label=u'Nowa nazwa', max_length=1024, validators=[path_validator]) + + def clean_src(self): + return clean_path(self.cleaned_data['src']) + + def clean_dst(self): + return clean_path(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']) + + +class CompressForm(HostPathForm): + archive = forms.CharField(label=u'Nazwa', max_length=1024, validators=[path_validator]) + files = forms.MultipleChoiceField(label=u'Pliki', widget=forms.MultipleHiddenInput()) + + def __init__(self, data=None, *args, **kwargs): + super(CompressForm, self).__init__(data, *args, **kwargs) + + if data is not None: + # accept user defined choices + self.fields['files'].choices += ((v, v) for v in data.getlist('files')) -class NewDirForm(forms.Form): - host = forms.CharField(label=u'Host', max_length=256, widget=forms.HiddenInput()) - path = forms.CharField(label=u'Ścieżka', max_length=1024, widget=forms.HiddenInput()) - name = forms.CharField(label=u'Nazwa', max_length=256) + def clean_files(self): + errors, cleaned = [], [] + for name in self.cleaned_data['files']: + try: + name_validator(name) + except ValidationError as e: + e.message += ' - ' + name + errors.append(e) + else: + cleaned.append(clean_path(name)) + if errors: + raise ValidationError(errors) + return cleaned -class RenameForm(forms.Form): - host = forms.CharField(label=u'Host', max_length=256, widget=forms.HiddenInput()) - path = forms.CharField(label=u'Ścieżka', max_length=1024, widget=forms.HiddenInput()) - src = forms.CharField(label=u'Stara nazwa', max_length=256, widget=forms.HiddenInput()) - dst = forms.CharField(label=u'Nowa nazwa', max_length=256) + def clean_archive(self): + return clean_path(self.cleaned_data['archive']) -class ArchiveForm(NewDirForm): +class ArchiveForm(CompressForm): ZIP = '.zip' GZIP = '.tar.gz' BZIP = '.tar.bz2' TYPE_CHOICES = ( - (ZIP, 'Archiwum zip'), - (GZIP, 'Archiwum tar.gz'), - (BZIP, 'Archiwum tar.bz2'), + (ZIP, 'zip'), + (GZIP, 'tar.gz'), + (BZIP, 'tar.bz2'), ) - type = forms.ChoiceField(label=u'Typ', choices=TYPE_CHOICES, initial=ZIP) + type = forms.ChoiceField(label=u'Typ archiwum', choices=TYPE_CHOICES, initial=ZIP)