<commit_before># -*- 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 <NAME> 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){$('#img_upload').after(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', max_width_thumb)
        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', max_height_thumb)
        except ConfigurationTransactionError, e:
            flash(_('The thumb max height could not be changed.'), 'error')<commit_msg>fix max size config form validation<commit_after># -*- 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 <NAME> 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){$('#img_upload').after(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')
{'fraction_non_alphanumeric': 0.11233940842545563, 'fraction_numerical': 0.00179265013444876, 'mean_word_length': 3.210691823899371, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6556093', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1897, 'n_words': 542}