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
"""
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>"
" - DO NOT FORGET TO MODIFIED THE REPORT CONFIGURATION.")
MSG_CVT = T(" - Conversion error: %s.")
def userModelConsistency():
""" A user data block is associated to each event.
It is configurable by the user and defined in the events table.
......@@ -56,23 +61,43 @@ def userModelConsistency():
update_keys[oldkey] = newkey
# 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:
return
# setup the default values
# string using set and reference need a special treatment
# set-up the default values dealing with data conversion
for key in model:
value = model[key]['value']
type = model[key]['type']
if value:
try:
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)
model[key]['value'] = (li[0] if li else None)
value = (li[0] if li else None)
elif type == "reference":
model[key]['value'] = None
value = None
except ValueError as e:
return MSG_CVT % e
model[key]['value'] = value
# scan and modify the history table
# the delete of old key is implicit since they are not copied
......
......@@ -48,8 +48,11 @@ Define an event
The event definitions are stored in the ``events`` table.
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
possibly a default value. Possible types are *boolean*, *date*, *integer*,
*float*, *reference* and *string*.
possibly a default value. Possible types are *boolean*, *date*, *float*,
*integer*, *reference* and *string*.
.. note::
For *date* type, the default value must be encoded as ``YYYY-MM-DD``.
.. note::
The *reference* type allows the user to select value within those
......@@ -58,7 +61,8 @@ possibly a default value. Possible types are *boolean*, *date*, *integer*,
``tablename.fieldname``.
.. 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
JSON_-type array, *e.g* `["foo", "faa"]`.
......
......@@ -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.",
' - Conversion error: %s.': ' - Erreur de conversion : %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 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