/
__init__.py
149 lines (127 loc) · 5.8 KB
/
__init__.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
"""
zine.plugins.img_upload
~~~~~~~~~~~~~~~~~~~~~~~~~~
A plugin to upload images to a static directory, to link then in
articles easily.
:copyright: (c) 2011 by gabriel pettier for more details.
:license: BSD, see LICENSE for more details.
"""
import os
from os.path import dirname, join, splitext
from random import choice
from string import letters
import PIL.Image as PIL
from zine.api import *
from zine.views.admin import render_admin_response
from zine.utils.admin import flash
from zine.utils.http import redirect
from zine.utils import dump_json
from zine.utils.forms import TextField
from zine.config import ConfigurationTransactionError
from zine.privileges import BLOG_ADMIN
TEMPLATES = join(dirname(__file__), 'templates')
SHARED_FILES = join(dirname(__file__), 'shared')
def inject_image_form(req, context):
"""This is called before the admin response is rendered. We add the
"""
add_script(url_for('img_upload/shared', filename='ajax-upload/ajaxupload.js'))
add_header_snippet('''
<script type="text/javascript">
$(document).ready(function() {
$(".formbox:first").after('<div id="img_upload">click to upload image</div>');
new AjaxUpload("img_upload", {
action: "/_services/json/img_upload/upload",
responseType: "json",
onComplete: function(file, response){
if (typeof(ckeditor) != 'undefined')
ckeditor.insertHtml(response);
else {
t = $('#f_text')
t.val(t.val() + response);
}
}
});
})
</script>''');
def add_image_manager_link(req, navigation_bar):
"""Called during the admin navigation bar setup. When the options menu is
traversed we insert our eric the fish link before the plugins link.
The outermost is the configuration editor, the next one the plugins
link and then we add our fish link.
"""
if not req.user.has_privilege(BLOG_ADMIN):
return
for link_id, url, title, children in navigation_bar:
if link_id == 'options':
children.insert(-3, ('img_upload', url_for('img_upload/config'),
_('Image upload')))
@require_privilege(BLOG_ADMIN)
def show_image_manager_options(req):
image_dir = req.args.get('images_directory')
base_url = req.args.get('base_url')
thumb_max_width = req.args.get('thumb_max_width')
thumb_max_height = req.args.get('thumb_max_height')
if image_dir:
try:
req.app.cfg.change_single('img_upload/images_directory', image_dir)
except ConfigurationTransactionError, e:
flash(_('The images directory could not be changed.'), 'error')
if base_url:
try:
req.app.cfg.change_single('img_upload/base_url', base_url)
except ConfigurationTransactionError, e:
flash(_('The base url could not be changed.'), 'error')
if thumb_max_width:
try:
req.app.cfg.change_single('img_upload/thumb_max_width', thumb_max_width)
except ConfigurationTransactionError, e:
flash(_('The thumb max width could not be changed.'), 'error')
if thumb_max_height:
try:
req.app.cfg.change_single('img_upload/thumb_max_height', thumb_max_height)
except ConfigurationTransactionError, e:
flash(_('The thumb max height could not be changed.'), 'error')
return render_admin_response('admin/img_uploader.html',
images_directory=req.app.cfg['img_upload/images_directory'],
base_url=req.app.cfg['img_upload/base_url'],
thumb_max_width=req.app.cfg['img_upload/thumb_max_width'],
thumb_max_height=req.app.cfg['img_upload/thumb_max_height'])
@require_privilege(BLOG_ADMIN)
def upload_image(req):
"""
"""
image = req.files['userfile']
if "image" in image.content_type:
name = image.filename
name, ext = splitext(name)
while name+ext in os.listdir(req.app.cfg['img_upload/images_directory']):
name += choice(letters)
image.save(os.path.join(req.app.cfg['img_upload/images_directory'], name+ext))
size = int(req.app.cfg['img_upload/thumb_max_width'] or 2000), int(req.app.cfg['img_upload/thumb_max_height'] or 2000)
img = PIL.open(req.app.cfg['img_upload/images_directory']+'/'+name+ext)
img.thumbnail(size, PIL.ANTIALIAS)
img.save(req.app.cfg['img_upload/images_directory']+'/'+name+'_tn'+ext)
return ''.join((
'<a href="', req.app.cfg['img_upload/base_url'], '/', name, ext,
'"><img src="', req.app.cfg['img_upload/base_url'], '/', name,
'_tn', ext, '"></img><a><br />'))
def setup(app, plugin):
"""This function is called by Zine in the application initialization
phase. Here we connect to the events and register our template paths,
url rules, views etc.
"""
app.connect_event('before-admin-response-rendered', inject_image_form)
app.connect_event('modify-admin-navigation-bar', add_image_manager_link)
app.add_config_var('img_upload/images_directory', TextField(default=''))
app.add_config_var('img_upload/thumb_max_width', TextField(default=''))
app.add_config_var('img_upload/thumb_max_height', TextField(default=''))
app.add_config_var('img_upload/base_url', TextField(default=''))
app.add_servicepoint('img_upload/upload', upload_image)
app.add_url_rule('/options/img_upload', prefix='admin',
endpoint='img_upload/config',
view=show_image_manager_options)
# add our templates to the searchpath so that Zine can find the
# admin panel template for the fish config panel.
app.add_template_searchpath(TEMPLATES)
app.add_shared_exports('img_upload', SHARED_FILES)