Fixes for i18nfield

This commit is contained in:
Antonio de la Rosa 2017-04-29 06:04:19 +02:00
parent c5b8cd390c
commit e218f4a4e7
9 changed files with 207 additions and 28 deletions

View file

@ -83,9 +83,11 @@ class GenerateAdminClass:
else:
return ""
url_action=add_get_parameters(self.url, op_admin=2, id=getpostfiles.get['id'])
form=show_form(post, edit_forms, self.t, False)
return self.t.render_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'])
return self.t.render_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'], url_action=url_action)
elif getpostfiles.get['op_admin']=='2':
@ -118,7 +120,7 @@ class GenerateAdminClass:
redirect(self.url)
else:
form=show_form(getpostfiles.post, edit_forms, self.t, True)
return self.t.render_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'])
return self.t.render_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=getpostfiles.get['id'], url_action=url_action)
pass
@ -165,3 +167,77 @@ class GenerateAdminClass:
return self.t.render_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=object_id)
"""
class GenerateConfigClass:
def __init__(self, model, url, t):
self.model_name=''
self.model=model
self.title_name=model.label
self.t=t
self.url=url
self.arr_fields_edit=list(model.fields.keys())
del self.arr_fields_edit[self.arr_fields_edit.index(model.name_field_id)]
self.template_insert='utils/insertform.phtml'
def show(self):
getpostfiles=GetPostFiles()
getpostfiles.obtain_query()
getpostfiles.query['op_config']=getpostfiles.query.get('op_config', '0')
if len(self.model.forms)==0:
self.model.create_forms()
title_edit=I18n.lang('common', 'edit', 'Edit')+' '+self.title_name
edit_forms=OrderedDict()
form_values={}
for key_form in self.arr_fields_edit:
edit_forms[key_form]=self.model.forms[key_form]
url_action=add_get_parameters(self.url, op_config=1)
if getpostfiles.query['op_config']=='1':
getpostfiles.obtain_post()
c=self.model.select_count()
insert_model=self.model.insert
if c:
insert_model=self.model.update
if insert_model(getpostfiles.post):
set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
redirect(self.url)
else:
form=show_form(getpostfiles.post, edit_forms, self.t, True)
return self.t.render_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id='0', url_action=url_action)
else:
form_values=self.model.select_a_row_where()
if not form_values:
form_values={}
form=show_form(form_values, edit_forms, self.t, True)
return self.t.render_template(self.template_insert, admin=self, title_edit=title_edit, form=form, model=self.model, id=0, url_action=url_action)

View file

@ -90,7 +90,7 @@ class PTemplate:
module=path.dirname(module)
"""
self.autoescape_ext=('html', 'htm', 'xml', 'phtml')
self.autoescape_ext=('html', 'htm', 'xml', 'phtml', 'pjs')
"""
self.cache_enabled=cache_enabled

View file

@ -1,22 +1,73 @@
${add_js_home_local('tools.js', 'admin')}
<%
choose=''
%>
<div id="languages_form">
<%def name="select_lang(i18n, lang_selected)">
% if i18n==lang_selected:
choose_flag
% else:
no_choose_flag
% endif
% if i18n==lang_selected:
<%
return "choose_flag"
%>
% else:
<%
return "no_choose_flag"
%>
% endif
</%def>
<%def name="hide_lang(i18n, lang_selected)">
% if i18n!=lang_selected:
style="display:none;"
% endif
</%def>
% if lang_selected!=None:
% for i18n in arr_i18n:
<input type="hidden" name="${name_form}" id="${name_form}_i18n_value" value="pepe" />
${form.change_name(name_form+'_'+i18n)}
<span${hide_lang(i18n, lang_selected)} id="${name_form}_switch">${form.form()|n}</span>
<a class="${select_lang(i18n, lang_selected)} lang_form" href="#"><img src="${make_media_url_module('images/languages/'+i18n+'.png', 'admin')}" alt="${name_form}_${i18n}"/></a>
<span ${hide_lang(i18n, lang_selected)} id="${name_form}_${i18n}_switch" class="${name_form}_i18n_form">${form.form()|n}</span>
<a class="${select_lang(i18n, lang_selected)} ${name_form}_i18n_flag lang_form" id="${name_form}_${i18n}_element" href="#"><img src="${make_media_url_module('images/languages/'+i18n+'.png', 'admin')}" alt="${name_form}_${i18n}"/></a>
% endfor
% endif
</div>
</div>
<script>
$(".${name_form}_i18n_flag").click(function () {
$('.${name_form}_i18n_flag').removeClass('choose_flag').addClass('no_choose_flag');
$(this).addClass('choose_flag');
name_id=$(this).attr('id').replace('_element', '');
name_form=name_id.split('_')[0];
$('.'+name_form+'_i18n_form').hide();
$('#'+name_id+'_switch').show();
});
//In submit convert in json
$(".${name_form}_i18n_flag").closest('form').submit( function () {
arr_i18n={}
$(".${name_form}_i18n_form").each(function () {
child_input=$(this).children();
name_id=child_input.attr('id').replace('${name_form}_', '').replace('_form', '');
arr_i18n[name_id]=child_input.val();
});
$("#${name_form}_i18n_value").val(JSON.stringify(arr_i18n));
});
</script>

View file

@ -5,7 +5,7 @@
${title_edit} \
%endif
</p>
<form method="post" action="${add_get_parameters(admin.url, op_admin=2, id=id)}">
<form method="post" action="${url_action}">
<h2>${title_edit}</h2>
<span class="error">${model.query_error}</span>
${ form|n }

View file

@ -37,7 +37,9 @@ class BaseForm:
def change_name(self, new_name):
self.name=new_name
self.name_field_id=self.name+'_form'
return ""
class SimpleTextForm(BaseForm):

View file

@ -6,6 +6,7 @@ from paramecio.cromosoma.coreforms import BaseForm
from paramecio.cromosoma.extraforms.i18nform import I18nForm
from paramecio.citoplasma.i18n import I18n
from paramecio.citoplasma.httputils import GetPostFiles
import json
class I18nField(PhangoField):
@ -21,25 +22,30 @@ class I18nField(PhangoField):
self.error=False
self.txt_error=''
final_value={}
func_get=self.obtain_lang_from_post
if type(value).__name__=='dict':
func_get=self.obtain_lang_value
for lang in I18n.dict_i18n:
final_value[lang]=func_get(lang, value)
arr_values={}
final_value[I18n.default_lang]=final_value.get(I18n.default_lang, '')
if final_value[I18n.default_lang]=='':
try:
arr_values=json.loads(value)
if not arr_values:
arr_values={}
except:
arr_values={}
arr_real_values={}
for lang in I18n.dict_i18n:
arr_real_values[lang]=arr_values.get(lang, value)
arr_values=arr_real_values
if arr_values[I18n.default_lang]=='':
self.error=True
self.txt_error='Sorry, You need default language '+I18n.default_lang
return json.dumps(final_value)
return json.dumps(arr_values)
return json.dumps(final_value)
return json.dumps(arr_values)
def get_type_sql(self):
@ -51,6 +57,10 @@ class I18nField(PhangoField):
def obtain_lang_from_post(self, lang, value):
return GetPostFiles.post.get(self.name+'_'+lang, '')
#getpost=GetPostFiles()
#getpost.obtain_post()
return "" #GetPostFiles.post.get(self.name+'_'+lang, '')

View file

@ -78,5 +78,7 @@ ${HeaderHTML.header_home()|n}
<script>
var navigation = responsiveNav(".nav-collapse", {customToggle: "#toggle"});
</script>
<%block name="jscript_block">
</%block>
</body>
</html>

View file

View file

@ -0,0 +1,38 @@
#!/usr/bin/python3
from paramecio.wsgiapp import app
from paramecio.citoplasma.mtemplates import env_theme, PTemplate
from settings import config
import os
#t=PTemplate(env)
#t.add_filter(make_admin_url)
workdir=os.getcwd()
arr_t={}
#dynamic javascript load
@app.route('/mediajs/<module>/<lang>/<filename:path>')
def send_javascript(module, lang, filename):
path_module=workdir+'/modules/'+module+'/js/'
path=workdir+'/themes/'+config.theme+'/js/'+module
file_path_module=path_module+'/'+filename
file_path_theme=path+'/'+filename
file_path=file_path_module
load_path=path_module
if os.path.isfile(file_path_theme):
file_path=file_path_theme
load_path=path
if not load_path in arr_t:
env=env_theme(load_path)
arr_t[load_path]=PTemplate(env)
return arr_t[module].load_template(file_path)