From 121cf3a2431b5105f622f19a434844ecf69f0c70 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Tue, 16 Feb 2016 00:44:15 +0100 Subject: [PATCH] Added new packages --- .../citoplasma/templates/forms/i18nform.phtml | 16 ++++++ paramecio/cromosoma/extrafields/i18nfield.py | 53 +++++++++++++++++++ paramecio/cromosoma/extraforms/i18nform.py | 17 ++++++ tests/i18nfieldtest.py | 39 ++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 paramecio/citoplasma/templates/forms/i18nform.phtml create mode 100644 paramecio/cromosoma/extrafields/i18nfield.py create mode 100644 paramecio/cromosoma/extraforms/i18nform.py create mode 100644 tests/i18nfieldtest.py diff --git a/paramecio/citoplasma/templates/forms/i18nform.phtml b/paramecio/citoplasma/templates/forms/i18nform.phtml new file mode 100644 index 0000000..0042263 --- /dev/null +++ b/paramecio/citoplasma/templates/forms/i18nform.phtml @@ -0,0 +1,16 @@ +${add_js_home_local('tools.js', 'admin')} +
+${form.show()} +<%def name="select_lang(i18n, lang_selected)"> + % if i18n==lang_selected: + choose_flag + % else: + no_choose_flag + % endif + +% if lang_selected!=None: + % for i18n in arr_i18n: + ${i18n} + % endfor +% endif +
\ No newline at end of file diff --git a/paramecio/cromosoma/extrafields/i18nfield.py b/paramecio/cromosoma/extrafields/i18nfield.py new file mode 100644 index 0000000..04022c3 --- /dev/null +++ b/paramecio/cromosoma/extrafields/i18nfield.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +import json +from paramecio.cromosoma.webmodel import PhangoField +from paramecio.cromosoma.coreforms import TextForm +from paramecio.cromosoma.extraforms.i18nform import I18nForm +from paramecio.citoplasma.i18n import I18n +from paramecio.citoplasma.httputils import GetPostFiles + +class I18nField(PhangoField): + + def __init__(self, name): + + super().__init__(name) + + self.name_form=I18nForm + self.parameters=[TextForm(name, '')] + + def check(self, value): + + 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) + + final_value[I18n.default_lang]=final_value.get(I18n.default_lang, '') + + if final_value[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(final_value) + + + def obtain_lang_value(self, lang, value): + + return value.get(self.name+'_'+lang, '') + + def obtain_lang_from_post(self, lang, value): + + return GetPostFiles.post.get(self.name+'_'+lang, '') + + \ No newline at end of file diff --git a/paramecio/cromosoma/extraforms/i18nform.py b/paramecio/cromosoma/extraforms/i18nform.py new file mode 100644 index 0000000..76e4c44 --- /dev/null +++ b/paramecio/cromosoma/extraforms/i18nform.py @@ -0,0 +1,17 @@ +#!/usr/bin/python3 + +from paramecio.cromosoma.coreforms import BaseForm +from paramecio.citoplasma.i18n import I18n +from paramecio.citoplasma.mtemplates import standard_t + +class I18nForm(BaseForm): + + def __init__(self, name, value, form): + + super().__init__(name, value) + + self.form_child=form + + def form(): + + return standard_t.load_template('forms/i18nform.phtml', form=self.form_child) \ No newline at end of file diff --git a/tests/i18nfieldtest.py b/tests/i18nfieldtest.py new file mode 100644 index 0000000..33b77f2 --- /dev/null +++ b/tests/i18nfieldtest.py @@ -0,0 +1,39 @@ +from settings import config +from paramecio.cromosoma.extrafields.i18nfield import I18nField +from paramecio.citoplasma.httputils import GetPostFiles +from paramecio.citoplasma.i18n import I18n +import unittest + +class TestFieldMethods(unittest.TestCase): + + def test_i18nfield(self): + + field=I18nField('i18n') + + value=field.check({}) + + self.assertTrue(field.error) + + value=field.check({'i18n_es-ES': 'Mi text', 'i18n_en-US': 'My Text'}) + + self.assertFalse(field.error) + + GetPostFiles.post={'i18n_es-ES': 'Mi text', 'i18n_en-US': 'My Text'} + + value=field.check('') + + self.assertFalse(field.error) + + I18n.default_lang='en-US' + + GetPostFiles.post={'i18n_es-ES': 'My Text'} + + value=field.check('') + + self.assertTrue(field.error) + + #phrase=slugify.slugify('this!()is a crap phrase o}çÇf oh yeah¡\'') + + #self.assertEqual(phrase, 'this---is-a-crap-phrase-o---f-oh-yeah--') + + \ No newline at end of file