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/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 diff --git a/rdmo/views/templatetags/view_tags.py b/rdmo/views/templatetags/view_tags.py index e47a874497..2b8f6e6cb1 100644 --- a/rdmo/views/templatetags/view_tags.py +++ b/rdmo/views/templatetags/view_tags.py @@ -38,21 +38,50 @@ 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) +@register.simple_tag(takes_context=True) +def get_set_numbers(context, set, attribute_path, index='*'): + 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) + 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) +@register.simple_tag(takes_context=True) +def get_number(context, attribute_path, set_index=0, index=0): + return get_numbers(context, attribute_path, set_index, index) + + @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) +@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' 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 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