kpi.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Onestein (<http://www.onestein.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from lxml import etree
  5. from odoo import api, fields, models, tools, SUPERUSER_ID, _
  6. from odoo.exceptions import UserError, ValidationError
  7. from odoo.tools.safe_eval import safe_eval
  8. from odoo.tools.translate import _
  9. class kpi_type(models.Model):
  10. _name = 'kpi.type'
  11. _description = 'KPI Type'
  12. name = fields.Char('Name', required=True)
  13. code = fields.Char('Code')
  14. class Kpi(models.Model):
  15. _name = 'kpi'
  16. _description = 'Key Performance Indicators - KPI'
  17. name = fields.Char('Name', required=True)
  18. acronym = fields.Char('Denomination / Acronym')
  19. definition = fields.Char('Definition')
  20. description = fields.Text('Description')
  21. goal = fields.Float('Goal')
  22. calcule = fields.Char('Calcule')
  23. view = fields.Html('View')
  24. periodicity = fields.Selection([('monthly', 'Monthly'),
  25. ('quarterly', 'Quarterly'),('semester', 'Semester'),('annualy', 'Annualy')], 'Periodicity', default='monthly')
  26. polarity = fields.Selection([('-', '(-)'),
  27. ('+', '(+)')], 'Polarity')
  28. #polarity = fields.Float('Polarity')
  29. source = fields.Char('Source')
  30. reason = fields.Char('Reason')
  31. dimension_variables = fields.Char('Dimension Variables')
  32. statistical_analysis = fields.Char('Statistical analysis')
  33. publication_periodicity = fields.Selection([('monthly', 'Monthly'),
  34. ('quarterly', 'Quarterly'),('semester', 'Semester'),('annualy', 'Annualy')], 'Periodicity of publication', default='monthly')
  35. degree_confidentiality = fields.Char('Degree of confidentiality')
  36. complementary_indicators = fields.Char('Complementary indicators')
  37. additional_notes = fields.Text('Additional Notes')
  38. kpi_type_id = fields.Many2one('kpi.type', 'KPI Type')
  39. input_ids = fields.Many2many('kpi.input', 'kpi_input_kpi_rel',
  40. 'input_id', 'kpi_id', string='Inputs')
  41. values_query = fields.Text(string='Query')
  42. last_snapshot=fields.Datetime('Last SnapShot',readonly=True)
  43. @api.model
  44. def create(self, vals):
  45. print "CREATE KPI: ", vals
  46. """
  47. Overrides orm create method.
  48. @param self: The object pointer
  49. @param vals: dictionary of fields value.
  50. """
  51. value = 0
  52. kpi_id = None
  53. if not vals:
  54. vals = {}
  55. if self._context is None:
  56. self._context = {}
  57. result = self.env['kpi'].search([('name', '=', vals['name'])])
  58. print "RESULT : ", result
  59. if (result):
  60. raise ValidationError('This Kpi Already Exist')
  61. if 'values_query' in vals:
  62. exceptions = ['delete', 'create']
  63. for x in exceptions:
  64. if vals['values_query'].lower().find(x) != -1:
  65. raise ValidationError('You must insert a valid SQL query')
  66. try:
  67. self._cr.execute(vals['values_query'])
  68. res = self._cr.fetchone()
  69. except Exception, e:
  70. raise ValidationError(e)
  71. if len(res) < 2:
  72. raise ValidationError('The query result must return 2 values, [0]="value" and [1]="max_value"')
  73. print "RES: ", len(res)
  74. return super(Kpi, self).create(vals)
  75. class kpi_value(models.Model):
  76. _name = 'kpi.value'
  77. _rec_name = 'kpi_id'
  78. _description = 'KPI Gauge'
  79. kpi_id = fields.Many2one('kpi', 'KPI', required=True)
  80. date = fields.Datetime('Date')
  81. min_value = fields.Float('Min Value')
  82. max_value = fields.Float('Max Value')
  83. value = fields.Float('Value')
  84. polarity=fields.Selection([('-1', '(-)'), ('1', '(+)')], 'Polarity')
  85. class kpi_domain(models.Model):
  86. _name = 'kpi.domain'
  87. _description = 'Domain'
  88. name = fields.Char(string="Name", required=True)
  89. domain = fields.Char(string="Domain", required=True)
  90. value = fields.Char(string="Value", required=True)