1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# coding: utf-8
# imports
import re, os
# django imports
from django import forms
from django.forms.formsets import BaseFormSet
from django.utils.translation import ugettext as _
# filebrowser imports
from filebrowser.settings import MAX_UPLOAD_SIZE, FOLDER_REGEX
from filebrowser.functions import convert_filename
alnum_name_re = re.compile(FOLDER_REGEX, re.U)
class MakeDirForm(forms.Form):
"""
Form for creating Folder.
"""
def __init__(self, path, *args, **kwargs):
self.path = path
super(MakeDirForm, self).__init__(*args, **kwargs)
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)
def clean_dir_name(self):
if self.cleaned_data['dir_name']:
# only letters, numbers, underscores, spaces and hyphens are allowed.
if not alnum_name_re.search(self.cleaned_data['dir_name']):
raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
# Folder must not already exist.
if os.path.isdir(os.path.join(self.path, convert_filename(self.cleaned_data['dir_name']))):
raise forms.ValidationError(_(u'The Folder already exists.'))
return convert_filename(self.cleaned_data['dir_name'])
class RenameForm(forms.Form):
"""
Form for renaming Folder/File.
"""
def __init__(self, path, file_extension, *args, **kwargs):
self.path = path
self.file_extension = file_extension
super(RenameForm, self).__init__(*args, **kwargs)
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)
def clean_name(self):
if self.cleaned_data['name']:
# only letters, numbers, underscores, spaces and hyphens are allowed.
if not alnum_name_re.search(self.cleaned_data['name']):
raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.'))
# folder/file must not already exist.
if os.path.isdir(os.path.join(self.path, convert_filename(self.cleaned_data['name']))):
raise forms.ValidationError(_(u'The Folder already exists.'))
elif os.path.isfile(os.path.join(self.path, convert_filename(self.cleaned_data['name']) + self.file_extension)):
raise forms.ValidationError(_(u'The File already exists.'))
return convert_filename(self.cleaned_data['name'])
|