From 55c2ec36c3ae560ae423adc87fbde57ceec54955 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Tue, 31 Mar 2020 16:29:54 +0200 Subject: [PATCH 1/7] Add mathfilters and tag to retrieve number of value --- rdmo/core/settings.py | 1 + rdmo/views/templatetags/view_tags.py | 18 ++++++++++++++++++ requirements.txt | 1 + 3 files changed, 20 insertions(+) diff --git a/rdmo/core/settings.py b/rdmo/core/settings.py index 1d60da7fc9..3d2f022beb 100644 --- a/rdmo/core/settings.py +++ b/rdmo/core/settings.py @@ -30,6 +30,7 @@ 'compressor', 'django_extensions', 'django_filters', + 'mathfilters', 'mptt', 'rules', # openapi specification tools diff --git a/rdmo/views/templatetags/view_tags.py b/rdmo/views/templatetags/view_tags.py index e47a874497..90c753d167 100644 --- a/rdmo/views/templatetags/view_tags.py +++ b/rdmo/views/templatetags/view_tags.py @@ -1,6 +1,9 @@ +import logging + from django import template register = template.Library() +log = logging.getLogger(__name__) @register.simple_tag(takes_context=True) @@ -48,6 +51,21 @@ def get_value(context, attribute_path, set_index=0, index=0): return get_values(context, attribute_path, set_index, index) +@register.simple_tag(takes_context=True) +def get_number(context, attribute_path, set_index=0, index=0): + i = 0 + val = get_values(context, attribute_path, set_index, index) + try: + i = val.text + except KeyError: + return i + else: + try: + return float(i) + except TypeError: + return int(i) + + @register.simple_tag(takes_context=True) def get_set_value(context, set, attribute_path, index=0): return get_set_values(context, set, attribute_path, index) diff --git a/requirements.txt b/requirements.txt index d169a49f2e..7bb5657149 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ django-compressor~=2.3 django-extensions~=2.1 django-filter~=2.2 django-libsass~=0.7 +django-mathfilters==1.0.0 django-mptt~=0.10 django-rest-swagger~=2.2 django-settings-export~=1.2 From 9463c1c9efc57e9d169c78240b3f53c2dc351150 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Tue, 31 Mar 2020 16:48:30 +0200 Subject: [PATCH 2/7] Fix error handling --- rdmo/views/templatetags/view_tags.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rdmo/views/templatetags/view_tags.py b/rdmo/views/templatetags/view_tags.py index 90c753d167..b406051731 100644 --- a/rdmo/views/templatetags/view_tags.py +++ b/rdmo/views/templatetags/view_tags.py @@ -1,9 +1,6 @@ -import logging - from django import template register = template.Library() -log = logging.getLogger(__name__) @register.simple_tag(takes_context=True) @@ -57,7 +54,7 @@ def get_number(context, attribute_path, set_index=0, index=0): val = get_values(context, attribute_path, set_index, index) try: i = val.text - except KeyError: + except AttributeError: return i else: try: From 789e3ad155b152f4f445fd20bfd2a3bf75ea2b56 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Wed, 1 Apr 2020 13:04:22 +0200 Subject: [PATCH 3/7] Add two new tags: get_set_numbers, get_set_sum --- rdmo/views/templatetags/view_tags.py | 38 +++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/rdmo/views/templatetags/view_tags.py b/rdmo/views/templatetags/view_tags.py index b406051731..df024090d3 100644 --- a/rdmo/views/templatetags/view_tags.py +++ b/rdmo/views/templatetags/view_tags.py @@ -43,6 +43,27 @@ def get_set_values(context, set, attribute_path, index='*'): return get_values(context, attribute_path, set.set_index, index) +@register.simple_tag(takes_context=True) +def get_set_numbers(context, set, attribute_path, index='*'): + numbers = [] + vals = get_values(context, attribute_path, set.set_index, index) + for val in vals: + i = 0 + try: + i = val.text + except AttributeError: + numbers.append(i) + else: + numbers.append(make_number(i)) + return numbers + + +@register.simple_tag(takes_context=True) +def get_set_sum(context, set, attribute_path, index='*'): + numbers = get_set_numbers(context, set, attribute_path, index='*') + return sum(numbers) + + @register.simple_tag(takes_context=True) def get_value(context, attribute_path, set_index=0, index=0): return get_values(context, attribute_path, set_index, index) @@ -57,10 +78,7 @@ def get_number(context, attribute_path, set_index=0, index=0): except AttributeError: return i else: - try: - return float(i) - except TypeError: - return int(i) + return make_number(i) @register.simple_tag(takes_context=True) @@ -120,3 +138,15 @@ def render_set_value_inline_list(context, set, attribute_path): return {'values': get_values(context, attribute_path, set.set_index)} except KeyError: return None + + +def make_number(val): + val = val.replace(',', '.') + try: + return float(val) + except ValueError: + pass + try: + return int(val) + except ValueError: + return 0 From d891199be4ad0c64a8720417972835ba4366c7d8 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Wed, 1 Apr 2020 18:28:14 +0200 Subject: [PATCH 4/7] Improve resilience --- rdmo/views/templatetags/view_tags.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/rdmo/views/templatetags/view_tags.py b/rdmo/views/templatetags/view_tags.py index df024090d3..4323239593 100644 --- a/rdmo/views/templatetags/view_tags.py +++ b/rdmo/views/templatetags/view_tags.py @@ -141,12 +141,16 @@ def render_set_value_inline_list(context, set, attribute_path): def make_number(val): - val = val.replace(',', '.') - try: - return float(val) - except ValueError: - pass - try: - return int(val) - except ValueError: - return 0 + if isinstance(val, str): + val = val.replace(',', '.') + if isinstance(val, float) is False: + try: + return int(val) + except ValueError: + pass + try: + return float(val) + except ValueError: + return 0 + else: + return val From bca17ba209dc2f2ab6a9dd2b19d1e8c420e03325 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Thu, 9 Apr 2020 17:18:10 +0200 Subject: [PATCH 5/7] Add property to models --- rdmo/projects/models.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/rdmo/projects/models.py b/rdmo/projects/models.py index 10ca8dac7a..8a6525b0df 100644 --- a/rdmo/projects/models.py +++ b/rdmo/projects/models.py @@ -1,14 +1,14 @@ import iso8601 - +from django.contrib.auth.models import User from django.db import models from django.db.models.signals import post_save -from django.contrib.auth.models import User from django.urls import reverse from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ +from rdmo.core.constants import (VALUE_TYPE_BOOLEAN, VALUE_TYPE_CHOICES, + VALUE_TYPE_DATETIME) from rdmo.core.models import Model -from rdmo.core.constants import VALUE_TYPE_CHOICES, VALUE_TYPE_DATETIME, VALUE_TYPE_BOOLEAN from rdmo.domain.models import Attribute from rdmo.options.models import Option from rdmo.questions.models import Catalog @@ -285,3 +285,24 @@ def is_true(self): @property def is_false(self): return self.text in self.FALSE_TEXT + + @property + def as_number(self): + try: + val = self.text + except AttributeError: + return 0 + else: + if isinstance(val, str): + val = val.replace(',', '.') + if isinstance(val, float) is False: + try: + return int(val) + except ValueError: + pass + try: + return float(val) + except ValueError: + return 0 + else: + return val From 1b3289efe78853f29268478a7dcdb9e36d44fdc0 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Tue, 21 Apr 2020 11:02:09 +0200 Subject: [PATCH 6/7] Add get_numbers to view_tags --- rdmo/views/templatetags/view_tags.py | 52 +++++++++------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/rdmo/views/templatetags/view_tags.py b/rdmo/views/templatetags/view_tags.py index 4323239593..2b8f6e6cb1 100644 --- a/rdmo/views/templatetags/view_tags.py +++ b/rdmo/views/templatetags/view_tags.py @@ -38,6 +38,14 @@ def get_values(context, attribute_path, set_index='*', index='*'): return '' +@register.simple_tag(takes_context=True) +def get_numbers(context, attribute_path, set_index='*', index='*'): + numbers = [] + for value_set in get_values(context, attribute_path, set_index, index): + numbers.append([value.as_number for value in value_set]) + return numbers + + @register.simple_tag(takes_context=True) def get_set_values(context, set, attribute_path, index='*'): return get_values(context, attribute_path, set.set_index, index) @@ -45,22 +53,12 @@ def get_set_values(context, set, attribute_path, index='*'): @register.simple_tag(takes_context=True) def get_set_numbers(context, set, attribute_path, index='*'): - numbers = [] - vals = get_values(context, attribute_path, set.set_index, index) - for val in vals: - i = 0 - try: - i = val.text - except AttributeError: - numbers.append(i) - else: - numbers.append(make_number(i)) - return numbers + return get_numbers(context, attribute_path, set.set_index, index) @register.simple_tag(takes_context=True) def get_set_sum(context, set, attribute_path, index='*'): - numbers = get_set_numbers(context, set, attribute_path, index='*') + numbers = get_set_numbers(context, set, attribute_path, index) return sum(numbers) @@ -71,14 +69,7 @@ def get_value(context, attribute_path, set_index=0, index=0): @register.simple_tag(takes_context=True) def get_number(context, attribute_path, set_index=0, index=0): - i = 0 - val = get_values(context, attribute_path, set_index, index) - try: - i = val.text - except AttributeError: - return i - else: - return make_number(i) + return get_numbers(context, attribute_path, set_index, index) @register.simple_tag(takes_context=True) @@ -86,6 +77,11 @@ def get_set_value(context, set, attribute_path, index=0): return get_set_values(context, set, attribute_path, index) +@register.simple_tag(takes_context=True) +def get_set_number(context, set, attribute_path, index=0): + return get_set_numbers(context, set, attribute_path, index) + + @register.simple_tag(takes_context=True) def get_set(context, attribute_path): id_path = attribute_path.rstrip('/') + '/id' @@ -138,19 +134,3 @@ def render_set_value_inline_list(context, set, attribute_path): return {'values': get_values(context, attribute_path, set.set_index)} except KeyError: return None - - -def make_number(val): - if isinstance(val, str): - val = val.replace(',', '.') - if isinstance(val, float) is False: - try: - return int(val) - except ValueError: - pass - try: - return float(val) - except ValueError: - return 0 - else: - return val From 90d6b95aafeea54ef1a90f8b73b468389a1bdf10 Mon Sep 17 00:00:00 2001 From: Jochen Klar Date: Tue, 21 Apr 2020 11:13:53 +0200 Subject: [PATCH 7/7] Add test view --- testing/fixtures/views.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/testing/fixtures/views.json b/testing/fixtures/views.json index 19df065be2..13cd768c16 100644 --- a/testing/fixtures/views.json +++ b/testing/fixtures/views.json @@ -19,5 +19,26 @@ "help_lang4": "", "help_lang5": "" } + }, + { + "model": "views.view", + "pk": 2, + "fields": { + "uri": "http://example.com/terms/views/numbers", + "uri_prefix": "http://example.com/terms", + "key": "numbers", + "comment": "", + "template": "{% load view_tags %}\n{% load mathfilters %}\n\n

Numbers

\n\n{% get_numbers 'individual/collection/range' as n %}\n\n{% for nn in n %}\n{% for nnn in nn %}\n{{ nnn|sub:1 }}\n{% endfor %}\n{% endfor %}", + "title_lang1": "View B", + "title_lang2": "Ansicht B", + "title_lang3": "", + "title_lang4": "", + "title_lang5": "", + "help_lang1": "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est.", + "help_lang2": "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est.", + "help_lang3": "", + "help_lang4": "", + "help_lang5": "" + } } ] \ No newline at end of file