Usunięcie aplikacji api.
[redakcja.git] / apps / filebrowser / forms.py
1 # coding: utf-8
2
3 import re, os
4
5 from django import forms
6 from django.forms.formsets import BaseFormSet
7 from django.utils.translation import ugettext as _
8
9 # filebrowser imports
10 from filebrowser.fb_settings import MAX_UPLOAD_SIZE
11 from filebrowser.functions import _get_file_type, _convert_filename
12
13 alnum_name_re = re.compile(r'^[\sa-zA-Z0-9._/-]+$')
14
15 class MakeDirForm(forms.Form):
16     """
17     Form for creating Directory.
18     """
19     
20     def __init__(self, path, *args, **kwargs):
21         self.path = path
22         super(MakeDirForm, self).__init__(*args, **kwargs)
23         
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)
25     
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'])
35     
36
37 class RenameForm(forms.Form):
38     """
39     Form for renaming File/Directory.
40     """
41     
42     def __init__(self, path, file_extension, *args, **kwargs):
43         self.path = path
44         self.file_extension = file_extension
45         super(RenameForm, self).__init__(*args, **kwargs)
46     
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)
48     
49     def clean_name(self):
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'])
58     
59