Exemplo n.º 1
0
 def compute_table_size(self):
     for header in self.comp.headers:
         size_cx, size_cy = calcul_text_size(header.descript, 9, 10,
                                             "center", True)
         self.size_rows[0] = max(self.size_rows[0], size_cy)
         self.columns.append([header.descript, size_cx * self.RATIO])
     row_idx = 0
     for recordid in self.comp.record_ids:
         record = self.comp.records[recordid]
         while len(self.rows) <= row_idx:
             self.rows.append([])
         while len(self.size_rows) <= (row_idx + 1):
             self.size_rows.append(0)
         hd_idx = 0
         for header in self.comp.headers:
             value = format_to_string(record[header.name], header.htype,
                                      header.formatstr)
             if header.htype == 'icon':
                 value = BASE64_PREFIX + value
                 img_size = get_image_size(value)
                 size_x = int(round(img_size[0] * DPI))
                 size_y = int(round(img_size[1] * DPI))
             else:
                 size_x, size_y = calcul_text_size(value, 9, 10, "start",
                                                   True)
             self.rows[row_idx].append(six.text_type(value))
             self.columns[hd_idx][1] = max(self.columns[hd_idx][1],
                                           size_x * self.RATIO)
             hd_idx += 1
         self.size_rows[row_idx + 1] = max(self.size_rows[row_idx + 1],
                                           size_y)
         row_idx += 1
Exemplo n.º 2
0
 def get_value_for_evaluate(self, field_list, field_value):
     dep_field = self.get_field_by_name(field_list[0])
     if (dep_field is None
             or is_simple_field(dep_field)) and not isinstance(
                 field_value, LucteriosModel):
         field_value = adapt_value(field_value)
         formatnum, formatstr = extract_format(
             get_format_from_field(dep_field))
         field_val = format_to_string(field_value, formatnum, formatstr)
     elif field_value is None:
         field_val = ""
     elif isinstance(field_value,
                     LucteriosModel) and not hasattr(field_value, "all"):
         field_val = field_value.evaluate("#" + ".".join(field_list[1:]))
     else:
         field_val = eval_sublist(field_list, field_value)
     return field_val
Exemplo n.º 3
0
    def evaluate(self, text):
        def eval_sublist(field_list, field_value):
            field_val = []
            for sub_model in field_value.all():
                field_val.append(
                    sub_model.evaluate("#" + ".".join(field_list[1:])))
            return "{[br/]}".join(field_val)

        if text == '#':
            return six.text_type(self)
        value = text
        if text is not None:
            for field in self.TO_EVAL_FIELD.findall(text):
                field_list = field[1:].split('.')
                if hasattr(self, field_list[0]):
                    field_value = getattr(self, field_list[0])
                else:
                    field_value = ""
                if PrintFieldsPlugIn.is_plugin(field_list[0]):
                    field_val = PrintFieldsPlugIn.get_plugin(
                        field_list[0]).evaluate("#" + ".".join(field_list[1:]))
                elif field_list[0][-4:] == '_set':
                    field_val = eval_sublist(field_list, field_value)
                else:
                    dep_field = self.get_field_by_name(field_list[0])
                    if (dep_field is None
                            or is_simple_field(dep_field)) and not isinstance(
                                field_value, LucteriosModel):
                        field_value = adapt_value(field_value)
                        formatnum, formatstr = extract_format(
                            get_format_from_field(dep_field))
                        field_val = format_to_string(field_value, formatnum,
                                                     formatstr)
                    else:
                        if field_value is None:
                            field_val = ""
                        elif isinstance(field_value,
                                        LucteriosModel) and not hasattr(
                                            field_value, "all"):
                            field_val = field_value.evaluate(
                                "#" + ".".join(field_list[1:]))
                        else:
                            field_val = eval_sublist(field_list, field_value)
                value = value.replace(field, six.text_type(field_val))
        return value
Exemplo n.º 4
0
 def get_print_value(self):
     return format_to_string(self.value, self._formatnum, self._formatstr)
Exemplo n.º 5
0
    def test_formating_en(self):
        set_locale_lang('en')
        activate('en')
        self.assertEqual(
            format_to_string(1234.56, "N3", "{[i]}{0}{[/i]};{[b]}{0}{[/b]}"),
            "{[i]}1,234.560{[/i]}", "check num positive formated")
        self.assertEqual(
            format_to_string(-1234.56, "N3", "{[i]}{0}{[/i]};{[b]}{0}{[/b]}"),
            "{[b]}1,234.560{[/b]}", "check num negative formated")
        self.assertEqual(format_to_string(1234.56, "N0", "{0}"), "1,235",
                         "check int no-formated")

        self.assertEqual(format_to_string(1234.56, "N3", "{0}"), "1,234.560",
                         "check num positive no-formated")
        self.assertEqual(format_to_string(-1234.56, "N3", "{0}"), "-1,234.560",
                         "check num negative no-formated")
        self.assertEqual(format_to_string(1234.56, "C2USD", "{0}"),
                         "1,234.56 $US", "check currency positive no-formated")
        self.assertEqual(format_to_string(-1234.56, "C2USD",
                                          "{0}"), "-1,234.56 $US",
                         "check currency negative no-formated")

        self.assertEqual(format_to_string("2017-04-23", "D", "{0}"),
                         "April 23, 2017", "check date")
        self.assertEqual(format_to_string("12:54:25.014", "T", "{0}"), "12:54",
                         "check time")
        self.assertEqual(
            format_to_string("2017-04-23T12:54:25.014", "H", "{0}"),
            "Sunday, April 23, 2017 at 12:54", "check date time")

        self.assertEqual(format_to_string(True, "B", "{0}"), "Yes",
                         "check bool true")
        self.assertEqual(format_to_string(False, "B", "{0}"), "No",
                         "check bool false")
Exemplo n.º 6
0
    def test_formating_fr(self):
        set_locale_lang('fr')
        activate('fr')

        self.assertEqual(
            format_to_string(1234.56, "N3", "{[i]}{0}{[/i]};{[b]}{0}{[/b]}"),
            "{[i]}1 234,560{[/i]}", "check num positive formated")
        self.assertEqual(
            format_to_string(-1234.56, "N3", "{[i]}{0}{[/i]};{[b]}{0}{[/b]}"),
            "{[b]}1 234,560{[/b]}", "check num negative formated")
        self.assertEqual(format_to_string(1234.56, "N0", "{0}"), "1 235",
                         "check int no-formated")

        self.assertEqual(format_to_string(1234.56, "N3", "{0}"), "1 234,560",
                         "check num positive no-formated")
        self.assertEqual(format_to_string(-1234.56, "N3", "{0}"), "-1 234,560",
                         "check num negative no-formated")
        self.assertEqual(format_to_string(1234.56, "C2EUR", "{0}"),
                         "1 234,56 €", "check currency positive no-formated")
        self.assertEqual(format_to_string(-1234.56, "C2EUR", "{0}"),
                         "-1 234,56 €", "check currency negative no-formated")

        self.assertEqual(format_to_string(1234.56, 'C2EUR', '{0};'),
                         "1 234,56 €", "currency mode 0 +")
        self.assertEqual(
            format_to_string(1234.56, 'C2EUR', 'Crédit {0};Débit {0}'),
            "Crédit 1 234,56 €", "currency mode 1 +")
        self.assertEqual(
            format_to_string(
                1234.56, 'C2EUR',
                '{[font color="green"]}Crédit {0}{[/font]};{[font color="blue"]}Débit {0}{[/font]}'
            ), "{[font color=\"green\"]}Crédit 1 234,56 €{[/font]}",
            "currency mode 2 +")
        self.assertEqual(format_to_string(1234.56, 'N2', '{0}'), "1 234,56",
                         "currency mode 3 +")
        self.assertEqual(format_to_string(1234.56, 'C2EUR', '{0};{0}'),
                         "1 234,56 €", "currency mode 4 +")
        self.assertEqual(format_to_string(1234.56, 'C2EUR', '{0}'),
                         "1 234,56 €", "currency mode 5 +")
        self.assertEqual(
            format_to_string(
                1234.56, 'C2EUR',
                '{[font color="green"]}{0}{[/font]};{[font color="blue"]}{0}{[/font]}'
            ), "{[font color=\"green\"]}1 234,56 €{[/font]}",
            "currency mode 6 +")

        self.assertEqual(format_to_string(-1234.56, 'C2EUR', '{0};'), "",
                         "currency mode 0 -")
        self.assertEqual(
            format_to_string(-1234.56, 'C2EUR', 'Crédit {0};Débit {0}'),
            "Débit 1 234,56 €", "currency mode 1 -")
        self.assertEqual(
            format_to_string(
                -1234.56, 'C2EUR',
                '{[font color="green"]}Crédit {0}{[/font]};{[font color="blue"]}Débit {0}{[/font]}'
            ), "{[font color=\"blue\"]}Débit 1 234,56 €{[/font]}",
            "currency mode 2 -")
        self.assertEqual(format_to_string(-1234.56, 'N2', '{0}'), "-1 234,56",
                         "currency mode 3 -")
        self.assertEqual(format_to_string(-1234.56, 'C2EUR', '{0};{0}'),
                         "1 234,56 €", "currency mode 4 -")
        self.assertEqual(format_to_string(-1234.56, 'C2EUR', '{0}'),
                         "-1 234,56 €", "currency mode 5 -")
        self.assertEqual(
            format_to_string(
                -1234.56, 'C2EUR',
                '{[font color="green"]}{0}{[/font]};{[font color="blue"]}{0}{[/font]}'
            ), "{[font color=\"blue\"]}1 234,56 €{[/font]}",
            "currency mode 6 -")

        self.assertEqual(format_to_string("2017-04-23", "D", "{0}"),
                         "23 avril 2017", "check date")
        self.assertEqual(format_to_string("12:54:25.014", "T", "{0}"), "12:54",
                         "check time")
        self.assertEqual(
            format_to_string("2017-04-23T12:54:25.014", "H", "{0}"),
            "dimanche 23 avril 2017 à 12:54", "check date time")

        self.assertEqual(format_to_string(True, "B", "{0}"), "Oui",
                         "check bool true")
        self.assertEqual(format_to_string(False, "B", "{0}"), "Non",
                         "check bool false")
Exemplo n.º 7
0
    def test_formating_general(self):
        set_locale_lang('fr')
        activate('fr')

        self.assertEqual(format_to_string(None, None, None), "---",
                         "check None")
        self.assertEqual(format_to_string("abc", None, None), "abc",
                         "check string simple")
        self.assertEqual(format_to_string("abc", None, "{0}"), "abc",
                         "check string simple +")
        self.assertEqual(format_to_string(["abc", "uvw", "xyz"], None, None),
                         "abc{[br/]}uvw{[br/]}xyz", "check string multiple")
        self.assertEqual(
            format_to_string("abc", None, "{[i]}{[b]}{0}{[/b]}{[/i]}"),
            "{[i]}{[b]}abc{[/b]}{[/i]}", "check string formated")
        self.assertEqual(
            format_to_string(["abc", "uvw", "xyz"], None, "{0} : {1} --> {2}"),
            "abc : uvw --> xyz", "check string splited")
        self.assertEqual(
            format_to_string([65.4, 456.04, 894730.124], "N2",
                             "{0} : {1} --> {2}"),
            "65,40 : 456,04 --> 894 730,12", "check float splited")
        self.assertEqual(
            format_to_string({
                "value": "abc",
                "format": "{[b]}{0}{[/b]}"
            }, None, "{[i]}{0}{[/i]}"), "{[i]}{[b]}abc{[/b]}{[/i]}",
            "check string riched formated")

        self.assertEqual(
            format_to_string(1234.56, None, "{[i]}{0}{[/i]};{[b]}{0}{[/b]}"),
            "{[i]}1234.56{[/i]}", "check num positive")
        self.assertEqual(
            format_to_string(-1234.56, None, "{[i]}{0}{[/i]};{[b]}{0}{[/b]}"),
            "{[b]}1234.56{[/b]}", "check num negative")

        self.assertEqual(format_to_string(0.000001, 'C2EUR', '{0};A'), "A",
                         "currency mode 0 null")
        self.assertEqual(format_to_string(-0.000001, 'C2EUR', '{0};A'), "A",
                         "currency mode 0 null")
        self.assertEqual(format_to_string(-0.000001, 'C2EUR', '{0};A;B'), "B",
                         "currency mode 0 null")
        self.assertEqual(format_to_string(0.000001, 'C2EUR', '{0};A;B'), "B",
                         "currency mode 0 null")

        self.assertEqual(
            format_to_string(0, {
                '0': 'aaa',
                '1': 'bbb',
                '2': 'ccc'
            }, "{0}"), "aaa", "check select 0")
        self.assertEqual(
            format_to_string(1, {
                '0': 'aaa',
                '1': 'bbb',
                '2': 'ccc'
            }, "{0}"), "bbb", "check select 1")
        self.assertEqual(
            format_to_string(2, {
                '0': 'aaa',
                '1': 'bbb',
                '2': 'ccc'
            }, "{0}"), "ccc", "check select 2")
        self.assertEqual(
            format_to_string(3, {
                '0': 'aaa',
                '1': 'bbb',
                '2': 'ccc'
            }, "{0}"), "3", "check select 3")