Code revision for #19 pull request

This commit is contained in:
Rui Manuel da Silva Martins 2015-08-12 14:25:32 +01:00
parent 6bcba9559a
commit 82a76f423e
4 changed files with 36 additions and 44 deletions

2
.gitignore vendored
View file

@ -27,5 +27,7 @@ pip-log.txt
.tox
nosetests.xml
.pypirc
# Sphinx
_build

View file

@ -1,4 +1,3 @@
/* Creates the copy buttons in the header of each stream field */
$(document).ready(function(){
//All the stream fields with all his content
@ -25,7 +24,6 @@ $(document).ready(function(){
};
};
/* on click binding */
$('.translation-field-copy').click(function(event){
event.preventDefault();
@ -42,8 +40,10 @@ function requestCopyField(originID, targetID) {
var serializedOriginField = $.grep(serializedForm, function(obj){return obj.name.indexOf(originID) >= 0;});
var jsonString = JSON.stringify(serializedOriginField);
/* AJAX request that returns the html content of originID field
with the id's changed to targetID */
/*
* AJAX request that returns the html content of originID field
* with the id's changed to targetID
*/
$.ajax({
url: 'copy_translation_content',
type: 'GET',
@ -55,8 +55,8 @@ function requestCopyField(originID, targetID) {
var wrapperDiv = $("#"+targetID+"-count").parents('.input')[0];
$(wrapperDiv).html(data);
})
.fail(function() {
console.log("error");
.fail(function(error) {
console.log("wagtail-modeltranslation error: %s", error);
})
}

View file

@ -1,15 +0,0 @@
$(document).ready(function() {
$.each(langs, function(idx, lang_code){
$('#id_title_'+lang_code).on('focus', function() {
$('#id_slug_'+lang_code).data('previous-val', $('#id_slug_'+lang_code).val());
$(this).data('previous-val', $(this).val());
});
$('#id_title_'+lang_code).on('keyup keydown keypress blur', function() {
if ($('body').hasClass('create') || (!$('#id_slug_'+lang_code).data('previous-val').length || cleanForSlug($('#id_title_'+lang_code).data('previous-val')) === $('#id_slug_'+lang_code).data('previous-val'))) {
// only update slug if the page is being created from scratch, if slug is completely blank, or if title and slug prior to typing were identical
$('#id_slug_'+lang_code).val(cleanForSlug($('#id_title_'+lang_code).val()));
}
});
});
});

View file

@ -6,8 +6,10 @@ from django.utils.html import format_html, format_html_join
from django.conf import settings
from django.conf.urls import url
from django.http import QueryDict
from django.http import HttpResponse
from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import Page
@hooks.register('insert_editor_js')
@ -16,8 +18,8 @@ def translated_slugs():
'modeltranslation/js/wagtail_translated_slugs.js',
]
js_includes = format_html_join('\n', '<script src="{0}{1}"></script>',
((settings.STATIC_URL, filename) for filename in js_files)
js_includes = format_html_join('\n', '<script src="{0}{1}"></script>', (
(settings.STATIC_URL, filename) for filename in js_files)
)
lang_codes = []
@ -32,21 +34,19 @@ def translated_slugs():
###############################################################################
# Copy StreamFields content
###############################################################################
"""
Ajax view that allows to duplicate content
between translated streamfields
"""
from django.http import HttpResponse
def return_translation_target_field_rendered_html(request, page_id):
from wagtail.wagtailcore.models import Page
"""
Ajax view that allows to duplicate content
between translated streamfields
"""
page = Page.objects.get(pk=page_id)
if request.is_ajax():
origin_field_name = request.GET.get('origin_field_name')
target_field_name = request.GET.get('target_field_name')
origin_field_serialized = json.loads(request.GET.get('serializedOriginField'))
origin_field_serialized = json.loads(
request.GET.get('serializedOriginField'))
# Patch field prefixes from origin field to target field
target_field_patched = []
@ -55,7 +55,8 @@ def return_translation_target_field_rendered_html(request, page_id):
for att in item.iteritems():
target_value = att[1]
if att[0] == 'name':
target_value = att[1].replace(origin_field_name, target_field_name)
target_value = att[1].replace(
origin_field_name, target_field_name)
patched_item = {"name": target_value}
else:
patched_item["value"] = att[1]
@ -68,38 +69,42 @@ def return_translation_target_field_rendered_html(request, page_id):
q_data.update({item['name']: item['value']})
# get render html
target_field = page.specific._meta.get_field(target_field_name)
value_data = target_field.stream_block.value_from_datadict(q_data, {}, target_field_name)
target_field_content_html = target_field.formfield().widget.render(target_field_name, value_data)
value_data = target_field.stream_block.value_from_datadict(
q_data, {}, target_field_name)
target_field_content_html = target_field.formfield().widget.render(
target_field_name, value_data)
# return html json
return HttpResponse(json.dumps(target_field_content_html), content_type='application/json')
return HttpResponse(
json.dumps(target_field_content_html), content_type='application/json')
@hooks.register('register_admin_urls')
def copy_streamfields_content():
return [
url(r'(?P<page_id>\d+)/edit/copy_translation_content$', return_translation_target_field_rendered_html, name='' ),
url(r'(?P<page_id>\d+)/edit/copy_translation_content$',
return_translation_target_field_rendered_html, name=''),
]
@hooks.register('insert_editor_js')
def streamfields_translation_copy():
"""
Includes script in editor html file that creates
buttons to copy content between translated stream fields
Includes script in editor html file that creates
buttons to copy content between translated stream fields
and send a ajax request to copy the content.
"""
#includes the java script file in the html file
# includes the java script file in the html file
js_files = [
'modeltranslation/js/copy_stream_fields.js',
]
js_includes = format_html_join('\n', '<script src="{0}{1}"></script>',
((settings.STATIC_URL, filename) for filename in js_files)
js_includes = format_html_join('\n', '<script src="{0}{1}"></script>', (
(settings.STATIC_URL, filename) for filename in js_files)
)
return js_includes