5 from django import forms
6 from django.forms.formsets import BaseFormSet
7 from django.utils.translation import ugettext as _
10 from filebrowser.fb_settings import MAX_UPLOAD_SIZE
11 from filebrowser.functions import _get_file_type, _convert_filename
13 alnum_name_re = re.compile(r'^[\sa-zA-Z0-9._/-]+$')
15 class MakeDirForm(forms.Form):
17 Form for creating Directory.
20 def __init__(self, path, *args, **kwargs):
22 super(MakeDirForm, self).__init__(*args, **kwargs)
24 dir_name = forms.CharField(widget=forms.TextInput(attrs=dict({ 'class': 'vTextField' }, max_length=50, min_length=3)), label=_(u'Name'), help_text=_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'), required=True)
26 def clean_dir_name(self):
27 if self.cleaned_data['dir_name']:
28 # only letters, numbers, underscores, spaces and hyphens are allowed.
29 if not alnum_name_re.search(self.cleaned_data['dir_name']):
30 raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
31 # directory must not already exist.
32 if os.path.isdir(os.path.join(self.path, _convert_filename(self.cleaned_data['dir_name']))):
33 raise forms.ValidationError(_(u'The Folder already exists.'))
34 return _convert_filename(self.cleaned_data['dir_name'])
37 class RenameForm(forms.Form):
39 Form for renaming File/Directory.
42 def __init__(self, path, file_extension, *args, **kwargs):
44 self.file_extension = file_extension
45 super(RenameForm, self).__init__(*args, **kwargs)
47 name = forms.CharField(widget=forms.TextInput(attrs=dict({ 'class': 'vTextField' }, max_length=50, min_length=3)), label=_(u'New Name'), help_text=_('Only letters, numbers, underscores, spaces and hyphens are allowed.'), required=True)
50 if self.cleaned_data['name']:
51 # only letters, numbers, underscores, spaces and hyphens are allowed.
52 if not alnum_name_re.search(self.cleaned_data['name']):
53 raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
54 # file/directory must not already exist.
55 if os.path.isdir(os.path.join(self.path, _convert_filename(self.cleaned_data['name']))) or os.path.isfile(os.path.join(self.path, _convert_filename(self.cleaned_data['name']) + self.file_extension)):
56 raise forms.ValidationError(_(u'The File/Folder already exists.'))
57 return _convert_filename(self.cleaned_data['name'])