Skip to content

Commit

Permalink
Merge pull request #190 from rdmorganiser/calculations_in_views
Browse files Browse the repository at this point in the history
Calculations in views
  • Loading branch information
triole authored Apr 22, 2020
2 parents 543e0b5 + 90d6b95 commit 1aa3d2a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions rdmo/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'compressor',
'django_extensions',
'django_filters',
'mathfilters',
'mptt',
'rules',
# openapi specification tools
Expand Down
27 changes: 24 additions & 3 deletions rdmo/projects/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions rdmo/views/templatetags/view_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions testing/fixtures/views.json
Original file line number Diff line number Diff line change
Expand Up @@ -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<h1>Numbers</h1>\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": ""
}
}
]

0 comments on commit 1aa3d2a

Please sign in to comment.