diff --git a/cloudkittydashboard/dashboards/admin/summary/views.py b/cloudkittydashboard/dashboards/admin/summary/views.py index acb9f0d..7870d8b 100644 --- a/cloudkittydashboard/dashboards/admin/summary/views.py +++ b/cloudkittydashboard/dashboards/admin/summary/views.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +from django.conf import settings from django.utils.translation import gettext_lazy as _ from horizon import tables @@ -19,6 +20,12 @@ from openstack_dashboard.api import keystone as api_keystone from cloudkittydashboard.api import cloudkitty as api from cloudkittydashboard.dashboards.admin.summary import tables as sum_tables +from cloudkittydashboard import utils + +rate_prefix = getattr(settings, + 'OPENSTACK_CLOUDKITTY_RATE_PREFIX', None) +rate_postfix = getattr(settings, + 'OPENSTACK_CLOUDKITTY_RATE_POSTFIX', None) class IndexView(tables.DataTableView): @@ -39,6 +46,9 @@ class IndexView(tables.DataTableView): for tenant in summary: tenant['name'] = tenants.get(tenant.id, '-') summary[-1]['name'] = 'Cloud Total' + for tenant in summary: + tenant['rate'] = utils.formatRate(tenant['rate'], + rate_prefix, rate_postfix) return summary @@ -65,4 +75,7 @@ class TenantDetailsView(tables.DataTableView): 'rate': sum([float(item['rate']) for item in summary]), }) summary = api.identify(summary, key='res_type', name=True) + for item in summary: + item['rate'] = utils.formatRate(item['rate'], + rate_prefix, rate_postfix) return summary diff --git a/cloudkittydashboard/dashboards/project/rating/views.py b/cloudkittydashboard/dashboards/project/rating/views.py index e5565ba..87ee80c 100644 --- a/cloudkittydashboard/dashboards/project/rating/views.py +++ b/cloudkittydashboard/dashboards/project/rating/views.py @@ -22,6 +22,12 @@ from horizon import tables from cloudkittydashboard.api import cloudkitty as api from cloudkittydashboard.dashboards.project.rating \ import tables as rating_tables +from cloudkittydashboard import utils + +rate_prefix = getattr(settings, + 'OPENSTACK_CLOUDKITTY_RATE_PREFIX', None) +rate_postfix = getattr(settings, + 'OPENSTACK_CLOUDKITTY_RATE_POSTFIX', None) class IndexView(tables.DataTableView): @@ -38,6 +44,9 @@ class IndexView(tables.DataTableView): total = sum([r.get('rate') for r in data]) data.append({'type': 'TOTAL', 'rate': total}) + for item in data: + item['rate'] = utils.formatRate(item['rate'], + rate_prefix, rate_postfix) return data diff --git a/cloudkittydashboard/utils.py b/cloudkittydashboard/utils.py index 3a9f5fc..95fd3d4 100644 --- a/cloudkittydashboard/utils.py +++ b/cloudkittydashboard/utils.py @@ -25,3 +25,12 @@ class TemplatizableDict(dict): def __setattr__(self, key, val): self[key] = val + + +def formatRate(rate: float, prefix: str, postfix: str) -> str: + rate = str(rate) + if prefix: + rate = prefix + rate + if postfix: + rate = rate + postfix + return rate diff --git a/doc/source/installation.rst b/doc/source/installation.rst index a19bfdd..cde4505 100644 --- a/doc/source/installation.rst +++ b/doc/source/installation.rst @@ -40,3 +40,37 @@ For more detailed information about CloudKitty installation check out the .. _installation section: https://cloudkitty.readthedocs.org/en/latest/installation.html + + +Configuration +============= + +To configure CloudKitty dashboard, add variables to your Horizon settings +file. +For more details about how to add variables to Horizon settings checkout the +`Horizon Settings Reference documentation`_. + + +.. _Horizon Settings Reference documentation: https://docs.openstack.org/horizon/latest/configuration/settings.html + +Rate Pre/Postfix +---------------- + +You can configure pre/postfix to rate vaules shown at the dashboard. + +Here's example of setting rate currency to US Dollar. + +.. code-block:: python + + # You can choose to have prefix or postfix or both. + # Prefix and postfix are not mutally exclusive. + OPENSTACK_CLOUDKITTY_RATE_PREFIX = '$' + OPENSTACK_CLOUDKITTY_RATE_POSTFIX = 'USD' + +Some symbols (Such as Non-ASCII) might require to use unicode value directly. + +.. code-block:: python + + # British Pound + OPENSTACK_CLOUDKITTY_RATE_PREFIX = u'\xA3' + OPENSTACK_CLOUDKITTY_RATE_POSTFIX = 'GBP' diff --git a/releasenotes/notes/add-options-to-attach-pre-post-fix-to-rate-value-2d78f5cb7c289445.yaml b/releasenotes/notes/add-options-to-attach-pre-post-fix-to-rate-value-2d78f5cb7c289445.yaml new file mode 100644 index 0000000..f1d87b8 --- /dev/null +++ b/releasenotes/notes/add-options-to-attach-pre-post-fix-to-rate-value-2d78f5cb7c289445.yaml @@ -0,0 +1,10 @@ +--- +features: + - | + Adds optional Horizon settings variable OPENSTACK_CLOUDKITTY_RATE_PREFIX + and OPENSTACK_CLOUDKITTY_RATE_POSTFIX. These allow users to attach + pre/postfix to their rate vaules shown at the dashboard such as currency. + These values can be set in ``.py`` settings snippets under + ``openstack_dashboard/local/local_settings.d`` directory. Follow + https://docs.openstack.org/horizon/latest/configuration/settings.html + for more details.