Browse Source

Add default validation logic

The goal was to force developers to write validation logic.
Now it's becoming useless with keychain.backend
And it adds boilerplate code.

In this commit, we set a permissive default validation logic.
It will reduce code in consumers module and still allow fine grained validation.
pull/1240/head
hparfr 7 years ago
parent
commit
e6d0fc897a
  1. 2
      keychain/__manifest__.py
  2. 47
      keychain/models/keychain.py
  3. 21
      keychain/tests/test_keychain.py

2
keychain/__manifest__.py

@ -4,7 +4,7 @@
{ {
"name": "Keychain", "name": "Keychain",
"summary": "Store accounts and credentials", "summary": "Store accounts and credentials",
"version": "10.0.2.0.0",
"version": "10.0.2.0.1",
"category": "Uncategorized", "category": "Uncategorized",
"website": "https://akretion.com/", "website": "https://akretion.com/",
"author": "Akretion, Odoo Community Association (OCA)", "author": "Akretion, Odoo Community Association (OCA)",

47
keychain/models/keychain.py

@ -116,12 +116,59 @@ class KeychainAccount(models.Model):
@implemented_by_keychain @implemented_by_keychain
def _validate_data(self, data): def _validate_data(self, data):
"""Ensure data is valid according to the namespace.
How to use:
- Create a method prefixed with your namespace
- Put your validation logic inside
- Return true if data is valid for your usage
This method will be called on write().
If false is returned an user error will be raised.
Example:
def _hereismynamspace_validate_data():
return len(data.get('some_param', '') > 6)
@params data dict
@returns boolean
"""
pass pass
def _default_validate_data(self, data):
"""Default validation.
By default says data is always valid.
See _validata_data() for more information.
"""
return True
@implemented_by_keychain @implemented_by_keychain
def _init_data(self): def _init_data(self):
"""Initialize data field.
How to use:
- Create a method prefixed with your namespace
- Return a dict with the keys and may be default
values your expect.
This method will be called on write().
Example:
def _hereismynamspace_init_data():
return { 'some_param': 'default_value' }
@returns dict
"""
pass pass
def _default_init_data(self):
"""Default initialization.
See _init_data() for more information.
"""
return {}
@staticmethod @staticmethod
def _retrieve_env(): def _retrieve_env():
"""Return the current environments. """Return the current environments.

21
keychain/tests/test_keychain.py

@ -221,3 +221,24 @@ class TestKeychain(TransactionCase):
self.assertTrue(True, 'Should validate json') self.assertTrue(True, 'Should validate json')
except: except:
self.assertTrue(False, 'It should validate a good json') self.assertTrue(False, 'It should validate a good json')
def test_default_init_and_valid(self):
"""."""
self.keychain._fields['namespace'].selection.append(
('keychain_test_default', 'test')
)
account = self.keychain.create({
"name": "test",
"namespace": "keychain_test_default",
"login": "test",
"technical_name": "keychain.test"
})
try:
account.write({"login": "test default"})
except ValidationError:
self.assertTrue(False, 'It should validate any json in default')
self.assertEqual(
account.data, account._serialize_data(
account._default_init_data()),
'Data should be default value')
Loading…
Cancel
Save