2010-11-22 12:01:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
2010-11-22 15:10:15 +00:00
|
|
|
"""
|
|
|
|
Module to handle publisher warranty contracts as well as notifications from
|
|
|
|
OpenERP.
|
|
|
|
"""
|
2010-11-22 12:01:48 +00:00
|
|
|
|
|
|
|
from osv import osv, fields
|
|
|
|
import logging
|
|
|
|
from tools.translate import _
|
|
|
|
import urllib
|
|
|
|
from tools.safe_eval import safe_eval
|
|
|
|
import pooler
|
|
|
|
from tools.config import config
|
|
|
|
import release
|
2010-11-24 08:41:27 +00:00
|
|
|
import datetime
|
|
|
|
from tools import misc
|
2010-11-22 12:01:48 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2010-11-24 08:41:27 +00:00
|
|
|
"""
|
|
|
|
Time interval that will be used to determine up to which date we will
|
|
|
|
check the logs to see if a message we just received was already logged.
|
|
|
|
@type: datetime.timedelta
|
|
|
|
"""
|
|
|
|
_PREVIOUS_LOG_CHECK = datetime.timedelta(days=365)
|
|
|
|
|
2010-11-22 12:01:48 +00:00
|
|
|
class publisher_warranty_contract(osv.osv):
|
2010-11-22 15:10:15 +00:00
|
|
|
"""
|
|
|
|
Osv representing a publisher warranty contract.
|
|
|
|
"""
|
2010-11-22 12:01:48 +00:00
|
|
|
_name = "publisher_warranty.contract"
|
|
|
|
|
|
|
|
def _get_valid_contracts(self, cr, uid):
|
2010-11-22 15:10:15 +00:00
|
|
|
"""
|
|
|
|
Return the list of the valid contracts encoded in the system.
|
|
|
|
|
|
|
|
@return: A list of contracts
|
|
|
|
@rtype: list of publisher_warranty.contract browse records
|
|
|
|
"""
|
2010-11-22 12:01:48 +00:00
|
|
|
return [contract for contract in self.browse(cr, uid, self.search(cr, uid, []))
|
|
|
|
if contract.state == 'valid']
|
|
|
|
|
|
|
|
def status(self, cr, uid):
|
|
|
|
""" Method called by the client to check availability of publisher warranty contract. """
|
|
|
|
|
|
|
|
contracts = self._get_valid_contracts(cr, uid)
|
|
|
|
return {
|
|
|
|
'status': "full" if contracts else "none" ,
|
|
|
|
'uncovered_modules': list(),
|
|
|
|
}
|
|
|
|
|
|
|
|
def send(self, cr, uid, tb, explanations, remarks=None):
|
|
|
|
""" Method called by the client to send a problem to the publisher warranty server. """
|
|
|
|
|
|
|
|
if not remarks:
|
|
|
|
remarks = ""
|
|
|
|
|
|
|
|
valid_contracts = self._get_valid_contracts(cr, uid)
|
|
|
|
valid_contract = valid_contracts[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
origin = 'client'
|
|
|
|
dbuuid = self.pool.get('ir.config_parameter').get_param(cr, uid, 'database.uuid')
|
2010-11-23 09:57:38 +00:00
|
|
|
db_create_date = self.pool.get('ir.config_parameter').get_param(cr, uid, 'database.create_date')
|
2010-11-22 15:10:15 +00:00
|
|
|
|
|
|
|
msg = {'contract_name': valid_contract.name,
|
2010-11-22 12:01:48 +00:00
|
|
|
'tb': tb,
|
|
|
|
'explanations': explanations,
|
|
|
|
'remarks': remarks,
|
|
|
|
'origin': origin,
|
|
|
|
'dbname': cr.dbname,
|
2010-11-23 09:57:38 +00:00
|
|
|
'dbuuid': dbuuid,
|
|
|
|
'db_create_date': db_create_date}
|
2010-11-22 15:10:15 +00:00
|
|
|
|
|
|
|
uo = urllib.urlopen(config.get("publisher_warranty_url"),
|
|
|
|
urllib.urlencode({'arg0': msg, "action": "send",}))
|
|
|
|
try:
|
|
|
|
submit_result = uo.read()
|
|
|
|
finally:
|
|
|
|
uo.close()
|
|
|
|
|
|
|
|
result = safe_eval(submit_result)
|
|
|
|
|
|
|
|
crm_case_id = result
|
2010-11-22 12:01:48 +00:00
|
|
|
|
|
|
|
if not crm_case_id:
|
|
|
|
return False
|
|
|
|
|
|
|
|
except osv.except_osv:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
_logger.warning("Error sending problem report", exc_info=1)
|
|
|
|
raise osv.except_osv("Connection error", "An error occured during the connection " +
|
|
|
|
"with the publisher warranty server.")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def check_validity(self, cr, uid, ids, context={}):
|
2010-11-22 15:10:15 +00:00
|
|
|
"""
|
|
|
|
Check the validity of a publisher warranty contract. This method just call send_ping() but checks
|
|
|
|
some more things, so it can be called from a user interface.
|
|
|
|
"""
|
2010-11-22 12:01:48 +00:00
|
|
|
contract_id = ids[0]
|
|
|
|
contract = self.browse(cr, uid, contract_id)
|
|
|
|
state = contract.state
|
|
|
|
validated = state != "unvalidated"
|
|
|
|
|
|
|
|
self.send_ping(cr, uid, ids, cron_mode=False, context=context)
|
|
|
|
|
|
|
|
contract = self.browse(cr, uid, contract_id)
|
|
|
|
validated2 = contract.state != "unvalidated"
|
|
|
|
if not validated and not validated2:
|
|
|
|
raise osv.except_osv(_("Contract validation error"),
|
|
|
|
_("Please check your publisher warranty contract name and validity."))
|
|
|
|
|
|
|
|
def send_ping(self, cr, uid, ids, cron_mode=True, context={}):
|
2010-11-22 15:10:15 +00:00
|
|
|
"""
|
|
|
|
Send a message to OpenERP's publisher warranty server to check the validity of
|
|
|
|
the contracts, get notifications, etc...
|
|
|
|
|
|
|
|
@param cron_mode: If true, catch all exceptions (appropriate for usage in a cron).
|
|
|
|
@type cron_mode: boolean
|
|
|
|
"""
|
2010-11-22 12:01:48 +00:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
result = send_ping(cr, uid)
|
|
|
|
except:
|
|
|
|
_logger.debug("Exception while sending a ping", exc_info=1)
|
|
|
|
raise osv.except_osv(_("Error"), _("Error during communication with the publisher warranty server."))
|
|
|
|
|
|
|
|
contracts = result["contracts"]
|
|
|
|
for contract in contracts:
|
|
|
|
c_id = self.search(cr, uid, [("name","=",contract)])[0]
|
|
|
|
date_from = contracts[contract][0]
|
|
|
|
date_to = contracts[contract][1]
|
|
|
|
state = contracts[contract][2]
|
|
|
|
self.write(cr, uid, c_id, {
|
|
|
|
"date_start": date_from,
|
|
|
|
"date_stop": date_to,
|
|
|
|
"state": state,
|
|
|
|
})
|
|
|
|
|
2010-11-24 08:41:27 +00:00
|
|
|
limit_date = (datetime.datetime.now() - _PREVIOUS_LOG_CHECK).strftime(misc.DEFAULT_SERVER_DATETIME_FORMAT)
|
2010-11-23 14:34:24 +00:00
|
|
|
for message in result["messages"]:
|
2010-11-24 08:41:27 +00:00
|
|
|
content = message if isinstance(message, str) or \
|
|
|
|
isinstance(message, unicode) else message[0]
|
|
|
|
ids = self.pool.get("res.log").search(cr, uid, [("res_model", "=", "publisher_warranty.contract"),
|
|
|
|
("create_date", ">=", limit_date),
|
|
|
|
("name", "=", content)])
|
|
|
|
if ids:
|
|
|
|
continue
|
2010-11-23 14:34:24 +00:00
|
|
|
self.pool.get('res.log').create(cr, uid,
|
|
|
|
{
|
2010-11-24 08:41:27 +00:00
|
|
|
'name': content,
|
2010-11-23 14:34:24 +00:00
|
|
|
'res_model': "publisher_warranty.contract",
|
|
|
|
"read": True,
|
|
|
|
"user_id": False,
|
|
|
|
},
|
|
|
|
context=context
|
|
|
|
)
|
2010-11-22 12:01:48 +00:00
|
|
|
except:
|
|
|
|
_logger.debug("Exception while interpreting the result of a ping", exc_info=1)
|
|
|
|
if cron_mode:
|
|
|
|
return False # same as before
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
return True
|
2010-11-23 09:57:38 +00:00
|
|
|
|
|
|
|
def get_last_user_message(self, cr, uid, context={}):
|
2010-11-23 10:27:06 +00:00
|
|
|
"""
|
|
|
|
Get the message to be written in the web client.
|
|
|
|
@return: An html message, can be False instead.
|
|
|
|
@rtype: string
|
|
|
|
"""
|
2010-11-23 14:34:24 +00:00
|
|
|
ids = self.pool.get('res.log').search(cr, uid, [("res_model", "=", "publisher_warranty.contract")]
|
2010-11-23 09:57:38 +00:00
|
|
|
, order="create_date desc", limit=1)
|
|
|
|
if not ids:
|
|
|
|
return False
|
2010-11-23 14:34:24 +00:00
|
|
|
message = self.pool.get('res.log').browse(cr, uid, ids[0]).name
|
2010-11-23 11:26:33 +00:00
|
|
|
|
2010-11-23 14:34:24 +00:00
|
|
|
return message
|
2010-11-22 12:01:48 +00:00
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'name' : fields.char('Contract Name', size=384, required=True),
|
|
|
|
'date_start' : fields.date('Starting Date', readonly=True),
|
|
|
|
'date_stop' : fields.date('Ending Date', readonly=True),
|
|
|
|
'state' : fields.selection([('unvalidated', 'Unvalidated'), ('valid', 'Valid')
|
|
|
|
, ('terminated', 'Terminated'), ('canceled', 'Canceled')], string="State", readonly=True),
|
|
|
|
'kind' : fields.char('Kind', size=64, readonly=True),
|
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
|
|
|
'state': 'unvalidated',
|
|
|
|
}
|
|
|
|
|
|
|
|
_sql_constraints = [
|
|
|
|
('uniq_name', 'unique(name)', "Your publisher warranty contract is already subscribed in the system !")
|
|
|
|
]
|
|
|
|
|
|
|
|
publisher_warranty_contract()
|
|
|
|
|
|
|
|
class maintenance_contract(osv.osv_memory):
|
|
|
|
""" Old osv we only keep for compatibility with the clients. """
|
|
|
|
|
|
|
|
_name = "maintenance.contract"
|
|
|
|
|
|
|
|
def status(self, cr, uid):
|
|
|
|
return self.pool.get("publisher_warranty.contract").status(cr, uid)
|
|
|
|
|
|
|
|
def send(self, cr, uid, tb, explanations, remarks=None):
|
|
|
|
return self.pool.get("publisher_warranty.contract").send(cr, uid, tb, explanations, remarks)
|
|
|
|
|
|
|
|
maintenance_contract()
|
|
|
|
|
|
|
|
class publisher_warranty_contract_wizard(osv.osv_memory):
|
2010-11-22 15:10:15 +00:00
|
|
|
"""
|
|
|
|
A wizard osv to help people entering a publisher warranty contract.
|
|
|
|
"""
|
2010-11-22 12:01:48 +00:00
|
|
|
_name = 'publisher_warranty.contract.wizard'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'name' : fields.char('Contract Name', size=256, required=True ),
|
2010-11-22 15:10:15 +00:00
|
|
|
'state' : fields.selection([("draft", "Draft"), ("finished", "Finished")])
|
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
|
|
|
"state": "draft",
|
2010-11-22 12:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def action_validate(self, cr, uid, ids, context=None):
|
|
|
|
if not ids:
|
|
|
|
return False
|
|
|
|
|
|
|
|
wiz = self.browse(cr, uid, ids[0])
|
|
|
|
c_name = wiz.name
|
|
|
|
|
|
|
|
contract_osv = self.pool.get("publisher_warranty.contract")
|
|
|
|
contracts = contract_osv.search(cr, uid, [("name","=",c_name)])
|
|
|
|
if contracts:
|
|
|
|
raise osv.except_osv(_("Error"), _("That contract is already registered in the system."))
|
|
|
|
|
|
|
|
contract_id = contract_osv.create(cr, uid, {
|
|
|
|
"name": c_name,
|
|
|
|
"state": "unvalidated",
|
|
|
|
})
|
|
|
|
|
|
|
|
contract_osv.check_validity(cr, uid, [contract_id])
|
|
|
|
|
2010-11-22 15:10:15 +00:00
|
|
|
self.write(cr, uid, ids, {"state": "finished"})
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2010-11-22 12:01:48 +00:00
|
|
|
|
|
|
|
publisher_warranty_contract_wizard()
|
|
|
|
|
|
|
|
def send_ping(cr, uid):
|
2010-11-22 15:10:15 +00:00
|
|
|
"""
|
|
|
|
Utility method to send a publisher warranty ping.
|
|
|
|
"""
|
2010-11-22 12:01:48 +00:00
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
|
|
|
|
dbuuid = pool.get('ir.config_parameter').get_param(cr, uid, 'database.uuid')
|
2010-11-23 09:57:38 +00:00
|
|
|
db_create_date = pool.get('ir.config_parameter').get_param(cr, uid, 'database.create_date')
|
2010-11-22 12:01:48 +00:00
|
|
|
nbr_users = pool.get("res.users").search(cr, uid, [], count=True)
|
|
|
|
contractosv = pool.get('publisher_warranty.contract')
|
|
|
|
contracts = contractosv.browse(cr, uid, contractosv.search(cr, uid, []))
|
|
|
|
msg = {
|
|
|
|
"dbuuid": dbuuid,
|
|
|
|
"nbr_users": nbr_users,
|
|
|
|
"dbname": cr.dbname,
|
2010-11-23 09:57:38 +00:00
|
|
|
"db_create_date": db_create_date,
|
2010-11-22 12:01:48 +00:00
|
|
|
"version": release.version,
|
|
|
|
"contracts": [c.name for c in contracts],
|
|
|
|
}
|
|
|
|
|
|
|
|
uo = urllib.urlopen(config.get("publisher_warranty_url"),
|
|
|
|
urllib.urlencode({'arg0': msg, "action": "update",}))
|
|
|
|
try:
|
|
|
|
submit_result = uo.read()
|
|
|
|
finally:
|
|
|
|
uo.close()
|
|
|
|
|
|
|
|
result = safe_eval(submit_result)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|