6 from django.db import models
7 from django import forms
8 from django.forms.widgets import Input
9 from django.db.models.fields import Field, CharField
10 from django.utils.encoding import force_unicode
11 from django.template.loader import render_to_string
12 from django.utils.translation import ugettext_lazy as _
15 from filebrowser.functions import _url_to_path, _dir_from_url, _get_version_path, _url_join
16 from filebrowser.fb_settings import *
17 from filebrowser.base import FileObject
20 class FileBrowseWidget(Input):
24 js = (os.path.join(URL_FILEBROWSER_MEDIA, 'js/AddFileBrowser.js'),)
26 def __init__(self, attrs=None):
27 self.directory = attrs.get('directory', '')
28 self.extensions = attrs.get('extensions', '')
29 self.format = attrs.get('format', '')
31 self.attrs = attrs.copy()
35 def render(self, name, value, attrs=None):
38 final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
39 final_attrs['search_icon'] = URL_FILEBROWSER_MEDIA + 'img/filebrowser_icon_show.gif'
40 final_attrs['directory'] = self.directory
41 final_attrs['extensions'] = self.extensions
42 final_attrs['format'] = self.format
43 final_attrs['ADMIN_THUMBNAIL'] = ADMIN_THUMBNAIL
44 final_attrs['DEBUG'] = DEBUG
47 final_attrs['directory'] = os.path.split(value.path_relative_directory)[0]
50 return render_to_string("filebrowser/custom_field.html", locals())
53 class FileBrowseFormField(forms.CharField):
54 widget = FileBrowseWidget
56 default_error_messages = {
57 'extension': _(u'Extension %(ext)s is not allowed. Only %(allowed)s is allowed.'),
60 def __init__(self, max_length=None, min_length=None,
61 directory=None, extensions=None, format=None,
63 self.max_length, self.min_length = max_length, min_length
64 self.directory = directory
65 self.extensions = extensions
67 self.format = format or ''
68 self.extensions = extensions or EXTENSIONS.get(format)
69 super(FileBrowseFormField, self).__init__(*args, **kwargs)
71 def clean(self, value):
72 value = super(FileBrowseFormField, self).clean(value)
75 file_extension = os.path.splitext(value)[1].lower()
76 if self.extensions and not file_extension in self.extensions:
77 raise forms.ValidationError(self.error_messages['extension'] % {'ext': file_extension, 'allowed': ", ".join(self.extensions)})
81 class FileBrowseField(Field):
82 __metaclass__ = models.SubfieldBase
84 def __init__(self, *args, **kwargs):
85 self.directory = kwargs.pop('directory', '')
86 self.extensions = kwargs.pop('extensions', '')
87 self.format = kwargs.pop('format', '')
88 return super(FileBrowseField, self).__init__(*args, **kwargs)
90 def to_python(self, value):
91 if not value or isinstance(value, FileObject):
93 return FileObject(_url_to_path(value))
95 def get_db_prep_value(self, value):
100 def get_manipulator_field_objs(self):
101 return [oldforms.TextField]
103 def get_internal_type(self):
106 def formfield(self, **kwargs):
108 attrs["directory"] = self.directory
109 attrs["extensions"] = self.extensions
110 attrs["format"] = self.format
112 'form_class': FileBrowseFormField,
113 'widget': FileBrowseWidget(attrs=attrs),
114 'directory': self.directory,
115 'extensions': self.extensions,
116 'format': self.format,
118 defaults.update(kwargs)
119 return super(FileBrowseField, self).formfield(**defaults)