[CORD-2810] Only synchronizers can write feedback state
Change-Id: Ia3f28763a0212a570186e4c6c5cb118c40325b5b
diff --git a/lib/xos-genx/xos-genx-tests/test_django_generator.py b/lib/xos-genx/xos-genx-tests/test_django_generator.py
index 0ba90b5..c3ffef5 100644
--- a/lib/xos-genx/xos-genx-tests/test_django_generator.py
+++ b/lib/xos-genx/xos-genx-tests/test_django_generator.py
@@ -73,6 +73,32 @@
self.assertEqual(len(blank_true), 1)
self.assertEqual(len(blank_false), 1)
+ def test_feedback_state(self):
+ """
+ [XOS-GenX] Generate DJANGO models, verify feedback_state fields
+ """
+ xproto = \
+ """
+ option app_label = "test";
+
+ message ParentFoo {
+ required string parent_name = 1 [null = False, blank = False, feedback_state = True];
+ }
+
+ message Foo (ParentFoo) {
+ required string name = 1 [null = False, blank = False, feedback_state = True];
+ }
+ """
+
+ args = FakeArgs()
+ args.inputs = xproto
+ args.target = 'django.xtarget'
+ output = XOSProcessor.process(args)
+
+ print output
+
+ self.assertIn("feedback_state_fields = ['parent_name', 'name']", output)
+
if __name__ == '__main__':
unittest.main()
diff --git a/lib/xos-genx/xos-genx-tests/test_jinja2_django.py b/lib/xos-genx/xos-genx-tests/test_jinja2_django.py
new file mode 100644
index 0000000..ea8738e
--- /dev/null
+++ b/lib/xos-genx/xos-genx-tests/test_jinja2_django.py
@@ -0,0 +1,50 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import unittest
+from xosgenx.jinja2_extensions.django import *
+
+class Jinja2BaseTests(unittest.TestCase):
+ def test_xproto_optioned_fields_to_list(self):
+
+ fields = [
+ {
+ 'name': 'has_feedback_1',
+ 'options': {
+ 'feedback_state': 'True',
+ }
+ },
+ {
+ 'name': 'has_feedback_2',
+ 'options': {
+ 'feedback_state': 'True',
+ }
+ },
+ {
+ 'name': 'no_feedback',
+ 'options': {
+ 'feedback_state': 'False',
+ }
+ }
+ ]
+
+ res = xproto_optioned_fields_to_list(fields, 'feedback_state', 'True')
+ self.assertEqual(res, ["has_feedback_1", "has_feedback_2"])
+
+if __name__ == '__main__':
+ unittest.main()
+
+
diff --git a/lib/xos-genx/xos-genx-tests/test_translator.py b/lib/xos-genx/xos-genx-tests/test_translator.py
index 2f8cc8a..5923bc7 100644
--- a/lib/xos-genx/xos-genx-tests/test_translator.py
+++ b/lib/xos-genx/xos-genx-tests/test_translator.py
@@ -315,6 +315,28 @@
output = XOSProcessor.process(args)
self.assertIn('verbose_name: "Verbose Foo Name"', output)
+ def test_feedback_field(self):
+ xproto = \
+"""
+option app_label = "test";
+
+message ParentFoo {
+ required string parent_name = 1 [null = False, blank = False, feedback_state = True];
+}
+
+message Foo (ParentFoo) {
+ required string name = 1 [null = False, blank = False, feedback_state = True];
+}
+"""
+
+ args = FakeArgs()
+ args.inputs = xproto
+ args.target = 'modeldefs.xtarget'
+ output = XOSProcessor.process(args)
+
+ read_only = filter(lambda s: 'read_only: True' in s, output.splitlines())
+ self.assertEqual(len(read_only), 3) # readonly is 1 for ParentFoo and 2 for Foo
+
if __name__ == '__main__':
unittest.main()
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/django.py b/lib/xos-genx/xosgenx/jinja2_extensions/django.py
index 593fefb..2632c00 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/django.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/django.py
@@ -168,3 +168,27 @@
return [map(str.strip, validation.split(':')) for validation in unquote(options['validators']).split(',')]
except KeyError:
return []
+
+def xproto_optioned_fields_to_list(fields, option, val):
+ """
+ List all the field that have a particural option
+ :param fields: (list) an array of message fields
+ :param option: (string) the option to look for
+ :param val: (any) the value of the option
+ :return: list of strings, field names where option is set
+ """
+
+ optioned_fields = []
+ for f in fields:
+ option_names = []
+ for k, v in f['options'].items():
+ option_names.append(k)
+
+ if option in option_names and f['options'][option] == val:
+ optioned_fields.append(f['name'])
+
+ return optioned_fields
+
+# TODO
+# - in modeldefs add info about this fields
+# - update the gui to have this fields as readonly
diff --git a/lib/xos-genx/xosgenx/targets/django.xtarget b/lib/xos-genx/xosgenx/targets/django.xtarget
index 8ef8e5f..13d37f9 100644
--- a/lib/xos-genx/xosgenx/targets/django.xtarget
+++ b/lib/xos-genx/xosgenx/targets/django.xtarget
@@ -44,6 +44,11 @@
class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases) }}:
plural_name = "{{ xproto_pluralize(m) }}"
+ {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True') %}
+ {%- if feedback_state_fields|length > 0 %}
+ feedback_state_fields = {{ feedback_state_fields }}
+ {%- endif %}
+
{# {% if m.options.no_sync or m.options.no_policy %}#}
{# {% if m.options.no_sync -%}#}
{# # Removing synchronizer feedback state from model#}
diff --git a/lib/xos-genx/xosgenx/targets/modeldefs.xtarget b/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
index 3b0790a..15c43d7 100644
--- a/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
+++ b/lib/xos-genx/xosgenx/targets/modeldefs.xtarget
@@ -29,6 +29,7 @@
{% endfor %}
{%- endif %}
type: {{ xproto_type_to_ui_type(f) }}
+ read_only: {{ xproto_is_true(f.options.feedback_state) }}
{% set validators = xproto_validators(f) -%}
{% if validators -%}
validators:
diff --git a/lib/xos-genx/xosgenx/targets/service.xtarget b/lib/xos-genx/xosgenx/targets/service.xtarget
index a9ca934..ed78ff1 100644
--- a/lib/xos-genx/xosgenx/targets/service.xtarget
+++ b/lib/xos-genx/xosgenx/targets/service.xtarget
@@ -41,6 +41,11 @@
class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases, legacy_tag, proto.message_names) }}:
plural_name = "{{ xproto_pluralize(m) }}"
+ {%- set feedback_state_fields = xproto_optioned_fields_to_list(xproto_base_fields(m, proto.message_table) + m.fields, 'feedback_state', 'True') %}
+ {%- if feedback_state_fields|length > 0 %}
+ feedback_state_fields = {{ feedback_state_fields }}
+ {%- endif %}
+
KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }}
{% if m.options.owner_class_name %}