Merge pull request #153 from Openscop/master
Allow non-image files to be uploaded
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 1.9 KiB |
After Width: | Height: | Size: 1.6 KiB |
|
@ -13,6 +13,11 @@
|
|||
<script type="text/javascript">
|
||||
document.write('<style>.noscript { display: none; }</style>');
|
||||
</script>
|
||||
<style type="text/css">
|
||||
a.thumb { text-align: center; display: block; float: left; width: 75px; height: 75px; word-wrap: break-word; line-height: 1.2em; overflow: hidden; }
|
||||
a.thumb img { display: inline-block; }
|
||||
span.filename { color: #666; font-size: 0.95em; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="page">
|
||||
|
@ -35,11 +40,10 @@
|
|||
<ul class="thumbs noscript">
|
||||
{% for file in files %}
|
||||
<li>
|
||||
<a {% if file.is_image %}class="thumb"{% endif %} href="{{ file.src }}">
|
||||
{% if file.is_image %}
|
||||
<img src="{{ file.thumb }}" style="max-width: 75px;"/>
|
||||
{% else %}
|
||||
{{ file.thumb }}
|
||||
<a class="thumb" href="{% if file.is_image %}{{ file.src }}{% else %}{{ file.thumb }}{% endif %}">
|
||||
<img src="{{ file.thumb }}" style="max-width: 75px;"/>
|
||||
{% if file.visible_filename %}
|
||||
<span class="filename">{{ file.visible_filename }}</span>
|
||||
{% endif %}
|
||||
</a>
|
||||
<div class="caption">
|
||||
|
|
|
@ -1,13 +1,27 @@
|
|||
import mimetypes
|
||||
import os.path
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.files.storage import default_storage
|
||||
from django.template.defaultfilters import slugify
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
|
||||
# Non-image file icons, matched from top to bottom
|
||||
fileicons_path = '{}/file-icons/'.format(getattr(settings, 'CKEDITOR_FILEICONS_PATH', '/static/ckeditor'))
|
||||
CKEDITOR_FILEICONS = getattr(settings, 'CKEDITOR_FILEICONS', [
|
||||
('\.pdf$', fileicons_path + 'pdf.png'),
|
||||
('\.doc$|\.docx$|\.odt$', fileicons_path + 'doc.png'),
|
||||
('\.txt$', fileicons_path + 'txt.png'),
|
||||
('\.ppt$', fileicons_path + 'ppt.png'),
|
||||
('\.xls$', fileicons_path + 'xls.png'),
|
||||
('.*', fileicons_path + 'file.png'), # Default
|
||||
])
|
||||
|
||||
|
||||
class NotAnImageException(Exception):
|
||||
pass
|
||||
|
||||
|
@ -28,6 +42,15 @@ def get_random_string():
|
|||
return ''.join(random.sample(string.ascii_lowercase * 6, 6))
|
||||
|
||||
|
||||
def get_icon_filename(file_name):
|
||||
"""
|
||||
Return the path to a file icon that matches the file name.
|
||||
"""
|
||||
for regex, iconpath in CKEDITOR_FILEICONS:
|
||||
if re.search(regex, file_name, re.I):
|
||||
return iconpath
|
||||
|
||||
|
||||
def get_thumb_filename(file_name):
|
||||
"""
|
||||
Generate thumb filename by adding _thumb to end of
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from datetime import datetime
|
||||
import os
|
||||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.files.storage import default_storage
|
||||
|
@ -48,11 +49,7 @@ class ImageUploadView(generic.View):
|
|||
try:
|
||||
backend.image_verify(upload)
|
||||
except utils.NotAnImageException:
|
||||
return HttpResponse("""
|
||||
<script type='text/javascript'>
|
||||
alert('Invalid image')
|
||||
window.parent.CKEDITOR.tools.callFunction({0});
|
||||
</script>""".format(request.GET['CKEditorFuncNum']))
|
||||
pass
|
||||
|
||||
# Open output file in which to store upload.
|
||||
upload_filename = get_upload_filename(upload.name, request.user)
|
||||
|
@ -120,13 +117,21 @@ def get_files_browse_urls(user=None):
|
|||
for filename in get_image_files(user=user):
|
||||
src = utils.get_media_url(filename)
|
||||
if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
|
||||
thumb = utils.get_media_url(utils.get_thumb_filename(filename))
|
||||
if is_image(src):
|
||||
thumb = utils.get_media_url(utils.get_thumb_filename(filename))
|
||||
visible_filename = None
|
||||
else:
|
||||
thumb = utils.get_icon_filename(filename)
|
||||
visible_filename = os.path.split(filename)[1]
|
||||
if len(visible_filename) > 20:
|
||||
visible_filename = visible_filename[0:19] + '...'
|
||||
else:
|
||||
thumb = src
|
||||
files.append({
|
||||
'thumb': thumb,
|
||||
'src': src,
|
||||
'is_image': is_image(src)
|
||||
'is_image': is_image(src),
|
||||
'visible_filename': visible_filename,
|
||||
})
|
||||
|
||||
return files
|
||||
|
|
7
tox.ini
|
@ -5,6 +5,7 @@ envlist=py27,py34
|
|||
commands=bash -exc "python manage.py test ckeditor_demo"
|
||||
whitelist_externals=/bin/bash
|
||||
setenv=DJANGO_SETTINGS_MODULE=ckeditor_demo.settings
|
||||
deps=https://www.djangoproject.com/download/1.7c3/tarball/
|
||||
Pillow==2.5.3
|
||||
selenium==2.42.1
|
||||
deps=
|
||||
Django==1.7.1
|
||||
Pillow==2.5.3
|
||||
selenium==2.42.1
|
||||
|
|