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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# coding: utf-8
# imports
import os
# django imports
from django.db import models
from django import forms
from django.forms.widgets import Input
from django.db.models.fields import Field, CharField
from django.utils.encoding import force_unicode
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
# filebrowser imports
from filebrowser.settings import *
from filebrowser.base import FileObject
from filebrowser.functions import url_to_path
class FileBrowseWidget(Input):
input_type = 'text'
class Media:
js = (os.path.join(URL_FILEBROWSER_MEDIA, 'js/AddFileBrowser.js'), )
def __init__(self, attrs=None):
self.directory = attrs.get('directory', '')
self.extensions = attrs.get('extensions', '')
self.format = attrs.get('format', '')
if attrs is not None:
self.attrs = attrs.copy()
else:
self.attrs = {}
def render(self, name, value, attrs=None):
if value is None:
value = ""
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
final_attrs['search_icon'] = URL_FILEBROWSER_MEDIA + 'img/filebrowser_icon_show.gif'
final_attrs['directory'] = self.directory
final_attrs['extensions'] = self.extensions
final_attrs['format'] = self.format
final_attrs['ADMIN_THUMBNAIL'] = ADMIN_THUMBNAIL
final_attrs['DEBUG'] = DEBUG
if value != "":
try:
final_attrs['directory'] = os.path.split(value.path_relative_directory)[0]
except:
pass
return render_to_string("filebrowser/custom_field.html", locals())
class FileBrowseFormField(forms.CharField):
widget = FileBrowseWidget
default_error_messages = {
'extension': _(u'Extension %(ext)s is not allowed. Only %(allowed)s is allowed.'),
}
def __init__(self, max_length=None, min_length=None,
directory=None, extensions=None, format=None,
*args, **kwargs):
self.max_length, self.min_length = max_length, min_length
self.directory = directory
self.extensions = extensions
if format:
self.format = format or ''
self.extensions = extensions or EXTENSIONS.get(format)
super(FileBrowseFormField, self).__init__(*args, **kwargs)
def clean(self, value):
value = super(FileBrowseFormField, self).clean(value)
if value == '':
return value
file_extension = os.path.splitext(value)[1].lower()
if self.extensions and not file_extension in self.extensions:
raise forms.ValidationError(self.error_messages['extension'] % {'ext': file_extension, 'allowed': ", ".join(self.extensions)})
return value
class FileBrowseField(Field):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
self.directory = kwargs.pop('directory', '')
self.extensions = kwargs.pop('extensions', '')
self.format = kwargs.pop('format', '')
return super(FileBrowseField, self).__init__(*args, **kwargs)
def to_python(self, value):
if not value or isinstance(value, FileObject):
return value
return FileObject(url_to_path(value))
def get_db_prep_value(self, value):
if value is None:
return None
return unicode(value)
def get_manipulator_field_objs(self):
return [oldforms.TextField]
def get_internal_type(self):
return "CharField"
def formfield(self, **kwargs):
attrs = {}
attrs["directory"] = self.directory
attrs["extensions"] = self.extensions
attrs["format"] = self.format
defaults = {
'form_class': FileBrowseFormField,
'widget': FileBrowseWidget(attrs=attrs),
'directory': self.directory,
'extensions': self.extensions,
'format': self.format
}
defaults.update(kwargs)
return super(FileBrowseField, self).formfield(**defaults)
try:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^filebrowser\.fields\.FileBrowseField"])
except:
pass
|