Browse Source

[FIX] Put classes in different files and apply small corrections

pull/139/head
Agathe Mollé 9 years ago
committed by Alejandro Santana
parent
commit
ec1045c900
  1. 4
      super_calendar/README.rst
  2. 6
      super_calendar/models/__init__.py
  3. 196
      super_calendar/models/super_calendar.py
  4. 157
      super_calendar/models/super_calendar_configurator.py
  5. 85
      super_calendar/models/super_calendar_configurator_line.py
  6. 15
      super_calendar/views/super_calendar_view.xml

4
super_calendar/README.rst

@ -8,7 +8,7 @@ to be merged in the super calendar. For each model, you have to define the
'description' and 'date_start' fields at least. Then you can define 'duration'
and the 'user_id' fields.
The 'super.calendar' object contains the the merged calendars. The
The 'super.calendar' object contains the merged calendars. The
'super.calendar' can be updated by 'ir.cron' or manually.
Configuration
@ -58,7 +58,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed feedback
`here <https://github.com/OCA/server-tools/issues/new?body=module:%20admin_technical_features%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`here <https://github.com/OCA/server-tools/issues/new?body=module:%20super_calendar%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Credits

6
super_calendar/models/__init__.py

@ -4,4 +4,8 @@
# __openerp__.py file at the root folder of this module. #
################################################################
from . import super_calendar
from . import (
super_calendar,
super_calendar_configurator,
super_calendar_configurator_line,
)

196
super_calendar/models/super_calendar.py

@ -8,6 +8,8 @@
# (c) 2012 Domsense srl (<http://www.domsense.com>)
# (c) 2015 Anubía, soluciones en la nube,SL (http://www.anubia.es)
# Alejandro Santana <alejandrosantana@anubia.es>
# (c) 2015 Savoir-faire Linux <http://www.savoirfairelinux.com>)
# Agathe Mollé <agathe.molle@savoirfairelinux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@ -24,190 +26,13 @@
#
##############################################################################
from openerp import models, fields, api
from openerp.tools.translate import _
from mako.template import Template
from datetime import datetime
from openerp import tools
from openerp.tools.safe_eval import safe_eval
import logging
from openerp import fields, models
def _models_get(self):
model_obj = self.env['ir.model']
model_ids = model_obj.search([])
return [(model.model, model.name) for model in model_ids]
class SuperCalendarConfigurator(models.Model):
_logger = logging.getLogger(__name__)
_name = 'super.calendar.configurator'
name = fields.Char(
string='Name',
size=64,
required=True,
)
line_ids = fields.One2many(
comodel_name='super.calendar.configurator.line',
inverse_name='configurator_id',
string='Lines',
)
def _clear_super_calendar_records(self):
""" Remove old super_calendar records """
super_calendar_pool = self.env['super.calendar']
super_calendar_ids = super_calendar_pool.search([])
super_calendar_ids.unlink()
@api.multi
def generate_calendar_records(self):
""" At every CRON execution, every 'super calendar' data is deleted
and regenerated again. """
# Remove old records
self._clear_super_calendar_records()
# Rebuild all calendar records
configurator_ids = self.search([])
for configurator in configurator_ids:
for line in configurator.line_ids:
self._generate_record_from_line(configurator, line)
self._logger.info('Calendar generated')
return True
@api.multi
def _generate_record_from_line(self, configurator, line):
super_calendar_pool = self.env['super.calendar']
values = self._get_record_values_from_line(configurator, line)
for record in values:
super_calendar_pool.create(values[record])
@api.multi
def _get_record_values_from_line(self, configurator, line):
res = {}
current_pool = self.env[line.name.model]
domain = line.domain and safe_eval(line.domain) or []
current_record_ids = current_pool.search(domain)
for cur_rec in current_record_ids:
f_user = line.user_field_id and line.user_field_id.name
f_descr = (line.description_field_id and
line.description_field_id.name)
f_date_start = (line.date_start_field_id and
line.date_start_field_id.name)
f_date_stop = (line.date_stop_field_id and
line.date_stop_field_id.name)
f_duration = (line.duration_field_id and
line.duration_field_id.name)
if (f_user and
cur_rec[f_user] and
cur_rec[f_user]._model._name != 'res.users'):
raise Exception(
_('Error'),
_("The 'User' field of record %s (%s) "
"does not refer to res.users")
% (cur_rec[f_descr], line.name.model))
if (((f_descr and cur_rec[f_descr]) or
line.description_code) and
cur_rec[f_date_start]):
duration = False
if (not line.duration_field_id and
line.date_stop_field_id and
cur_rec[f_date_start] and
cur_rec[f_date_stop]):
date_start = datetime.strptime(
cur_rec[f_date_start],
tools.DEFAULT_SERVER_DATETIME_FORMAT
)
date_stop = datetime.strptime(
cur_rec[f_date_stop],
tools.DEFAULT_SERVER_DATETIME_FORMAT
)
date_diff = (date_stop - date_start)
duration = date_diff.total_seconds() / 3600
elif line.duration_field_id:
duration = cur_rec[f_duration]
if line.description_type != 'code':
name = cur_rec[f_descr]
else:
parse_dict = {'o': cur_rec}
mytemplate = Template(line.description_code)
name = mytemplate.render(**parse_dict)
super_calendar_values = {
'name': name,
'date_start': cur_rec[f_date_start],
'duration': duration,
'user_id': (
f_user and
cur_rec[f_user] and
cur_rec[f_user].id or
False
),
'configurator_id': configurator.id,
'res_id': line.name.model+','+str(cur_rec['id']),
'model_id': line.name.id,
}
res[cur_rec] = super_calendar_values
return res
class SuperCalendarConfiguratorLine(models.Model):
_name = 'super.calendar.configurator.line'
name = fields.Many2one(
comodel_name='ir.model',
string='Model',
required=True,
)
domain = fields.Char(
string='Domain',
size=512,
)
configurator_id = fields.Many2one(
comodel_name='super.calendar.configurator',
string='Configurator',
)
description_type = fields.Selection(
[('field', 'Field'),
('code', 'Code')],
string="Description Type",
default='field',
)
description_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='Description field',
domain=("['&','|',('ttype', '=', 'char'),('ttype', '=', 'text'),"
"('model_id', '=', name)]"),
)
description_code = fields.Text(
string='Description field',
help=("Use '${o}' to refer to the involved object. "
"E.g.: '${o.project_id.name}'"),
)
date_start_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='Start date field',
domain=("['&','|',('ttype', '=', 'datetime'),('ttype', '=', 'date'),"
"('model_id', '=', name)]"),
required=True,
)
date_stop_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='End date field',
domain="['&',('ttype', '=', 'datetime'),('model_id', '=', name)]"
)
duration_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='Duration field',
domain="['&',('ttype', '=', 'float'), ('model_id', '=', name)]",
)
user_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='User field',
domain="['&', ('ttype', '=', 'many2one'), ('model_id', '=', name)]",
)
model_list = model_obj.search([])
return [(model.model, model.name) for model in model_list]
class SuperCalendar(models.Model):
@ -215,30 +40,35 @@ class SuperCalendar(models.Model):
name = fields.Char(
string='Description',
size=512,
required=True,
readonly=True,
)
date_start = fields.Datetime(
string='Start date',
required=True,
readonly=True,
)
duration = fields.Float(
string='Duration'
string='Duration',
readonly=True,
)
user_id = fields.Many2one(
comodel_name='res.users',
string='User',
readonly=True,
)
configurator_id = fields.Many2one(
comodel_name='super.calendar.configurator',
string='Configurator',
readonly=True,
)
res_id = fields.Reference(
selection=_models_get,
string='Resource',
size=128,
readonly=True,
)
model_id = fields.Many2one(
comodel_name='ir.model',
string='Model',
readonly=True,
)

157
super_calendar/models/super_calendar_configurator.py

@ -0,0 +1,157 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Odoo, Open Source Management Solution
#
# Copyright (c) All rights reserved:
# (c) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
# (c) 2012 Domsense srl (<http://www.domsense.com>)
# (c) 2015 Anubía, soluciones en la nube,SL (http://www.anubia.es)
# Alejandro Santana <alejandrosantana@anubia.es>
# (c) 2015 Savoir-faire Linux <http://www.savoirfairelinux.com>)
# Agathe Mollé <agathe.molle@savoirfairelinux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses
#
##############################################################################
import logging
from datetime import datetime
from mako.template import Template
from openerp import _, api, exceptions, fields, models, tools
from openerp.tools.safe_eval import safe_eval
_logger = logging.getLogger(__name__)
class SuperCalendarConfigurator(models.Model):
_name = 'super.calendar.configurator'
name = fields.Char(
string='Name',
required=True,
)
line_ids = fields.One2many(
comodel_name='super.calendar.configurator.line',
inverse_name='configurator_id',
string='Lines',
)
def _clear_super_calendar_records(self):
"""
Remove old super_calendar records
"""
super_calendar_pool = self.env['super.calendar']
super_calendar_list = super_calendar_pool.search([])
super_calendar_list.unlink()
@api.multi
def generate_calendar_records(self):
"""
At every CRON execution, every 'super calendar' data is deleted and
regenerated again.
"""
# Remove old records
self._clear_super_calendar_records()
# Rebuild all calendar records
configurator_list = self.search([])
for configurator in configurator_list:
for line in configurator.line_ids:
configurator._generate_record_from_line(line)
_logger.info('Calendar generated')
return True
@api.multi
def _generate_record_from_line(self, line):
"""
Create super_calendar records from super_calendar_configurator_line
objects.
"""
super_calendar_pool = self.env['super.calendar']
values = self._get_record_values_from_line(line)
for record in values:
super_calendar_pool.create(values[record])
@api.multi
def _get_record_values_from_line(self, line):
"""
Get super_calendar fields values from super_calendar_configurator_line
objects.
Check if the User value is a res.users.
"""
res = {}
current_pool = self.env[line.name.model]
domain = line.domain and safe_eval(line.domain) or []
current_record_list = current_pool.search(domain)
for cur_rec in current_record_list:
f_user = line.user_field_id.name
f_descr = line.description_field_id.name
f_date_start = line.date_start_field_id.name
f_date_stop = line.date_stop_field_id.name
f_duration = line.duration_field_id.name
if (f_user and
cur_rec[f_user] and
cur_rec[f_user]._model._name != 'res.users'):
raise exceptions.ValidationError(
_("The 'User' field of record %s (%s) "
"does not refer to res.users")
% (cur_rec[f_descr], line.name.model))
if (((f_descr and cur_rec[f_descr]) or
line.description_code) and
cur_rec[f_date_start]):
duration = False
if (not line.duration_field_id and
line.date_stop_field_id and
cur_rec[f_date_start] and
cur_rec[f_date_stop]):
if line.date_start_field_id.ttype == 'date':
date_format = tools.DEFAULT_SERVER_DATE_FORMAT
else:
date_format = tools.DEFAULT_SERVER_DATETIME_FORMAT
date_start = datetime.strptime(
cur_rec[f_date_start], date_format
)
if line.date_stop_field_id.ttype == 'date':
date_format = tools.DEFAULT_SERVER_DATE_FORMAT
else:
date_format = tools.DEFAULT_SERVER_DATETIME_FORMAT
date_stop = datetime.strptime(
cur_rec[f_date_stop], date_format
)
date_diff = (date_stop - date_start)
duration = date_diff.total_seconds() / 3600
elif line.duration_field_id:
duration = cur_rec[f_duration]
if line.description_type != 'code':
name = cur_rec[f_descr]
else:
parse_dict = {'o': cur_rec}
mytemplate = Template(line.description_code)
name = mytemplate.render(**parse_dict)
super_calendar_values = {
'name': name,
'date_start': cur_rec[f_date_start],
'duration': duration,
'user_id': (f_user and cur_rec[f_user].id),
'configurator_id': self.id,
'res_id': line.name.model + ',' + str(cur_rec['id']),
'model_id': line.name.id,
}
res[cur_rec] = super_calendar_values
return res

85
super_calendar/models/super_calendar_configurator_line.py

@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Odoo, Open Source Management Solution
#
# Copyright (c) All rights reserved:
# (c) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
# (c) 2012 Domsense srl (<http://www.domsense.com>)
# (c) 2015 Anubía, soluciones en la nube,SL (http://www.anubia.es)
# Alejandro Santana <alejandrosantana@anubia.es>
# (c) 2015 Savoir-faire Linux <http://www.savoirfairelinux.com>)
# Agathe Mollé <agathe.molle@savoirfairelinux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses
#
##############################################################################
from openerp import fields, models
class SuperCalendarConfiguratorLine(models.Model):
_name = 'super.calendar.configurator.line'
name = fields.Many2one(
comodel_name='ir.model',
string='Model',
required=True,
)
domain = fields.Char(
string='Domain',
)
configurator_id = fields.Many2one(
comodel_name='super.calendar.configurator',
string='Configurator',
)
description_type = fields.Selection(
[('field', 'Field'),
('code', 'Code')],
string="Description Type",
default='field',
)
description_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='Description field',
domain="[('ttype', 'in', ('char', 'text')), ('model_id', '=', name)]",
)
description_code = fields.Text(
string='Description field',
help=("""Use '${o}' to refer to the involved object.
E.g.: '${o.project_id.name}'"""),
)
date_start_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='Start date field',
domain="[('ttype', 'in', ('datetime', 'date')), "
"('model_id', '=', name)]",
required=True,
)
date_stop_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='End date field',
domain="[('ttype', 'in', ('datetime', 'date')), "
"('model_id', '=', name)]",
)
duration_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='Duration field',
domain="[('ttype', '=', 'float'), ('model_id', '=', name)]",
)
user_field_id = fields.Many2one(
comodel_name='ir.model.fields',
string='User field',
domain="[('ttype', '=', 'many2one'), ('model_id', '=', name)]",
)

15
super_calendar/views/super_calendar_view.xml

@ -93,13 +93,13 @@
<form string="Calendar">
<sheet>
<group>
<field name="name" readonly="1"/>
<field name="date_start" readonly="1"/>
<field name="duration" readonly="1"/>
<field name="user_id" readonly="1"/>
<field name="configurator_id" readonly="1"/>
<field name="model_id" readonly="1"/>
<field name="res_id" readonly="1"/>
<field name="name"/>
<field name="date_start"/>
<field name="duration"/>
<field name="user_id"/>
<field name="configurator_id"/>
<field name="model_id"/>
<field name="res_id"/>
</group>
</sheet>
</form>
@ -132,7 +132,6 @@
<newline/>
<group expand="0" string="Extended Filters..." colspan="4" col="8">
<field name="date_start" />
<!--<field name="date_stop" />-->
<field name="duration" />
</group>
</search>

Loading…
Cancel
Save