Commit 16e4dbd1 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Convert properly the default value of the user data block.

parent f4ffdc6b
...@@ -4,11 +4,16 @@ A collection of tools specific to the applications ...@@ -4,11 +4,16 @@ A collection of tools specific to the applications
""" """
import json import json
import plugin_dbui
from datetime import datetime
from plugin_dbui import get_id
MSG = T(" - %s entry(ies) modified in the history table. <br>" MSG = T(" - %s entry(ies) modified in the history table. <br>"
" - DO NOT FORGET TO MODIFIED THE REPORT CONFIGURATION.") " - DO NOT FORGET TO MODIFIED THE REPORT CONFIGURATION.")
MSG_CVT = T(" - Conversion error: %s.")
def userModelConsistency(): def userModelConsistency():
""" A user data block is associated to each event. """ A user data block is associated to each event.
It is configurable by the user and defined in the events table. It is configurable by the user and defined in the events table.
...@@ -56,23 +61,43 @@ def userModelConsistency(): ...@@ -56,23 +61,43 @@ def userModelConsistency():
update_keys[oldkey] = newkey update_keys[oldkey] = newkey
# get the event id # get the event id
id_event = plugin_dbui.get_id(db.events, event=event) id_event = get_id(db.events, event=event)
if id_event == None: if id_event == None:
return return
# setup the default values # set-up the default values dealing with data conversion
# string using set and reference need a special treatment
for key in model: for key in model:
value = model[key]['value'] value = model[key]['value']
type = model[key]['type'] type = model[key]['type']
if value: if value:
if type == "string" and value[0] == "[" and value[-1] == "]":
li = json.loads(value)
model[key]['value'] = (li[0] if li else None)
elif type == "reference": try:
model[key]['value'] = None if type == "boolean":
value = value.lower() in ('true', 'vrai', 'y', 'yes', '1')
elif type == "date":
value = datetime.strptime(value, "%Y-%m-%d")
value = value.isoformat()
elif type == "float":
value = float(value)
elif type == "integer":
value = int(value)
if type == "string" and value[0] == "[" and value[-1] == "]":
# reference to a set, pick the first value as default
li = json.loads(value)
value = (li[0] if li else None)
elif type == "reference":
value = None
except ValueError as e:
return MSG_CVT % e
model[key]['value'] = value
# scan and modify the history table # scan and modify the history table
# the delete of old key is implicit since they are not copied # the delete of old key is implicit since they are not copied
......
...@@ -48,9 +48,12 @@ Define an event ...@@ -48,9 +48,12 @@ Define an event
The event definitions are stored in the ``events`` table. The event definitions are stored in the ``events`` table.
It mainly contains the a model defining the structure of the user data block. It mainly contains the a model defining the structure of the user data block.
The model is a series of *property* defined by a name, a data type and The model is a series of *property* defined by a name, a data type and
possibly a default value. Possible types are *boolean*, *date*, *integer*, possibly a default value. Possible types are *boolean*, *date*, *float*,
*float*, *reference* and *string*. *integer*, *reference* and *string*.
.. note::
For *date* type, the default value must be encoded as ``YYYY-MM-DD``.
.. note:: .. note::
The *reference* type allows the user to select value within those The *reference* type allows the user to select value within those
available for a given database field. The address of the database available for a given database field. The address of the database
...@@ -58,7 +61,8 @@ possibly a default value. Possible types are *boolean*, *date*, *integer*, ...@@ -58,7 +61,8 @@ possibly a default value. Possible types are *boolean*, *date*, *integer*,
``tablename.fieldname``. ``tablename.fieldname``.
.. note:: .. note::
For string, the user can also select the value among a predefined set. For *string* type, the user can also select the value among a
predefined set.
The set is defined in the *value* column. It is equal to the The set is defined in the *value* column. It is equal to the
JSON_-type array, *e.g* `["foo", "faa"]`. JSON_-type array, *e.g* `["foo", "faa"]`.
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
{ {
'': '', '': '',
' - %s entry(ies) modified in the history table. <br> - DO NOT FORGET TO MODIFIED THE REPORT CONFIGURATION.': " - %s entrée(s) modifiée(s) dans l'historique. <br> - N'OUBLIEZ PAS DE MODIFIER LES RAPPORTS.", ' - %s entry(ies) modified in the history table. <br> - DO NOT FORGET TO MODIFIED THE REPORT CONFIGURATION.': " - %s entrée(s) modifiée(s) dans l'historique. <br> - N'OUBLIEZ PAS DE MODIFIER LES RAPPORTS.",
' - Conversion error: %s.': ' - Erreur de conversion : %s.',
'%s between %s and %s': '%s du %s au %s', '%s between %s and %s': '%s du %s au %s',
'%s entries modified in the history table.': "%s entrés ont été modifié dans l'historique.", '%s entries modified in the history table.': "%s entrés ont été modifié dans l'historique.",
'%s in %s': '%s en %s', '%s in %s': '%s en %s',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment