Skip to content

Commit

Permalink
Merge pull request #25 from scream4ik/master
Browse files Browse the repository at this point in the history
django 1.8+ compatibility
  • Loading branch information
miguelramos authored Oct 18, 2016
2 parents 66e3e22 + 96055bc commit 8cbbfaf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ At the moment i'm without time to maintain this great project. To all the people
or
pip install https://github.com/miguelramos/django-media-manager/archive/master.zip

* Add filebrowser to INSTALLED_APPS.
* Add filebrowser to INSTALLED_APPS before django.contrib.admin.
* Add the following line _before_ the admin URLS:
* (r'^admin/filebrowser/', include('filebrowser.urls'))
* Collect static files
Expand Down
45 changes: 28 additions & 17 deletions filebrowser/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def _template():

class FileBrowseWidget(Input):
input_type = 'text'

class Media:
js = (os.path.join(URL_FILEBROWSER_MEDIA, 'js/AddFileBrowser.js'), )
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', '')
Expand All @@ -41,32 +41,35 @@ def __init__(self, attrs=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[
'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]
final_attrs['directory'] = \
os.path.split(value.path_relative_directory)[0]
except:
pass
return render_to_string(_template() + "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.'),
'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):
Expand All @@ -77,42 +80,48 @@ def __init__(self, max_length=None, min_length=None,
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)})
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', '')
super(FileBrowseField, self).__init__(*args, **kwargs)


def from_db_value(self, value, expression, connection, context):
if not value or isinstance(value, FileObject):
return value
return FileObject(url_to_path(value))

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, connection, prepared=False):
if value is None:
return None
return str(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
Expand All @@ -128,8 +137,10 @@ def formfield(self, **kwargs):
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
File renamed without changes.
3 changes: 1 addition & 2 deletions filebrowser/templates/suit/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_static admin_list admin_urls %}
{% load fb_tags fb_pagination %}
{% load url from future %}

{% block extrastyle %}
{{ block.super }}
Expand Down Expand Up @@ -178,4 +177,4 @@ <h4>{% trans 'Nothing found' %}!</h4>
</div>
</div>
</div>
{% endblock %}
{% endblock %}
3 changes: 1 addition & 2 deletions filebrowser/templates/suit/makedir.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_static admin_list admin_urls %}
{% load fb_tags fb_csrf %}
{% load url from future %}

{% block extrastyle %}
{{ block.super }}
Expand Down Expand Up @@ -49,4 +48,4 @@
</div>
</form>
</div>
{% endblock %}
{% endblock %}
3 changes: 1 addition & 2 deletions filebrowser/templates/suit/rename.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_static admin_list admin_urls %}
{% load fb_tags fb_csrf %}
{% load url from future %}

{% block extrastyle %}
{{ block.super }}
Expand Down Expand Up @@ -53,4 +52,4 @@
</div>
</form>
</div>
{% endblock %}
{% endblock %}
3 changes: 1 addition & 2 deletions filebrowser/templates/suit/upload.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_static admin_list admin_urls %}
{% load fb_tags %}
{% load url from future %}

{% block extrastyle %}
{{ block.super }}
Expand Down Expand Up @@ -119,4 +118,4 @@
</div>
</form>
</div>
{% endblock %}
{% endblock %}

0 comments on commit 8cbbfaf

Please sign in to comment.