From e218f4a4e7d07a0914ca639773b8e8e61241c34f Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Sat, 29 Apr 2017 06:04:19 +0200 Subject: [PATCH] Fixes for i18nfield --- paramecio/citoplasma/generate_admin_class.py | 80 ++++++++++++++++++- paramecio/citoplasma/mtemplates.py | 2 +- .../citoplasma/templates/forms/i18nform.phtml | 67 ++++++++++++++-- .../templates/utils/insertform.phtml | 2 +- paramecio/cromosoma/coreforms.py | 4 +- paramecio/cromosoma/extrafields/i18nfield.py | 40 ++++++---- .../modules/admin/templates/admin/home.html | 2 + paramecio/modules/javascript/__init__.py | 0 paramecio/modules/javascript/load_js.py | 38 +++++++++ 9 files changed, 207 insertions(+), 28 deletions(-) create mode 100644 paramecio/modules/javascript/__init__.py create mode 100644 paramecio/modules/javascript/load_js.py diff --git a/paramecio/citoplasma/generate_admin_class.py b/paramecio/citoplasma/generate_admin_class.py index 3ab8804..32da437 100644 --- a/paramecio/citoplasma/generate_admin_class.py +++ b/paramecio/citoplasma/generate_admin_class.py @@ -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) + diff --git a/paramecio/citoplasma/mtemplates.py b/paramecio/citoplasma/mtemplates.py index dba4e24..4d30b0a 100644 --- a/paramecio/citoplasma/mtemplates.py +++ b/paramecio/citoplasma/mtemplates.py @@ -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 diff --git a/paramecio/citoplasma/templates/forms/i18nform.phtml b/paramecio/citoplasma/templates/forms/i18nform.phtml index fd8c489..2ad1dbc 100644 --- a/paramecio/citoplasma/templates/forms/i18nform.phtml +++ b/paramecio/citoplasma/templates/forms/i18nform.phtml @@ -1,22 +1,73 @@ ${add_js_home_local('tools.js', 'admin')} +<% + +choose='' + +%>
<%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 name="hide_lang(i18n, lang_selected)"> % if i18n!=lang_selected: style="display:none;" % endif + % if lang_selected!=None: % for i18n in arr_i18n: + + ${form.change_name(name_form+'_'+i18n)} - ${form.form()|n} - ${name_form}_${i18n} + ${form.form()|n} + ${name_form}_${i18n} % endfor % endif -
\ No newline at end of file + + diff --git a/paramecio/citoplasma/templates/utils/insertform.phtml b/paramecio/citoplasma/templates/utils/insertform.phtml index 5aa6260..28f4f62 100644 --- a/paramecio/citoplasma/templates/utils/insertform.phtml +++ b/paramecio/citoplasma/templates/utils/insertform.phtml @@ -5,7 +5,7 @@ ${title_edit} \ %endif

-
+

${title_edit}

${model.query_error} ${ form|n } diff --git a/paramecio/cromosoma/coreforms.py b/paramecio/cromosoma/coreforms.py index 394ae4b..c311475 100644 --- a/paramecio/cromosoma/coreforms.py +++ b/paramecio/cromosoma/coreforms.py @@ -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): diff --git a/paramecio/cromosoma/extrafields/i18nfield.py b/paramecio/cromosoma/extrafields/i18nfield.py index 5e88326..28ba810 100644 --- a/paramecio/cromosoma/extrafields/i18nfield.py +++ b/paramecio/cromosoma/extrafields/i18nfield.py @@ -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, '') diff --git a/paramecio/modules/admin/templates/admin/home.html b/paramecio/modules/admin/templates/admin/home.html index 48ab49f..eb82ca2 100644 --- a/paramecio/modules/admin/templates/admin/home.html +++ b/paramecio/modules/admin/templates/admin/home.html @@ -78,5 +78,7 @@ ${HeaderHTML.header_home()|n} +<%block name="jscript_block"> + diff --git a/paramecio/modules/javascript/__init__.py b/paramecio/modules/javascript/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/paramecio/modules/javascript/load_js.py b/paramecio/modules/javascript/load_js.py new file mode 100644 index 0000000..13dbce4 --- /dev/null +++ b/paramecio/modules/javascript/load_js.py @@ -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///') +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)