2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-10-14 11:15:34 +00:00
#
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# 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.
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# 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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# 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/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
# TODO:
2008-07-22 15:11:28 +00:00
# Error treatment: exception, request, ... -> send request to user_id
2006-12-07 13:41:40 +00:00
import time
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
from openerp . tools . translate import _
2006-12-07 13:41:40 +00:00
class subscription_document ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " subscription.document "
2010-05-19 18:32:32 +00:00
_description = " Subscription Document "
2008-07-22 15:11:28 +00:00
_columns = {
2014-05-21 09:52:05 +00:00
' name ' : fields . char ( ' Name ' , required = True ) ,
2010-11-08 13:04:35 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to False, it will allow you to hide the subscription document without removing it. " ) ,
2008-08-21 13:50:55 +00:00
' model ' : fields . many2one ( ' ir.model ' , ' Object ' , required = True ) ,
2014-07-06 14:44:26 +00:00
' field_ids ' : fields . one2many ( ' subscription.document.fields ' , ' document_id ' , ' Fields ' , copy = True )
2008-07-22 15:11:28 +00:00
}
_defaults = {
' active ' : lambda * a : True ,
}
2006-12-07 13:41:40 +00:00
2007-06-26 09:44:12 +00:00
class subscription_document_fields ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " subscription.document.fields "
2010-05-19 18:32:32 +00:00
_description = " Subscription Document Fields "
2008-07-22 15:11:28 +00:00
_rec_name = ' field '
_columns = {
' field ' : fields . many2one ( ' ir.model.fields ' , ' Field ' , domain = " [( ' model_id ' , ' = ' , parent.model)] " , required = True ) ,
2010-09-21 07:07:36 +00:00
' value ' : fields . selection ( [ ( ' false ' , ' False ' ) , ( ' date ' , ' Current Date ' ) ] , ' Default Value ' , size = 40 , help = " Default value is considered for field when new document is generated. " ) ,
2008-07-22 15:11:28 +00:00
' document_id ' : fields . many2one ( ' subscription.document ' , ' Subscription Document ' , ondelete = ' cascade ' ) ,
}
_defaults = { }
2006-12-07 13:41:40 +00:00
2010-12-13 06:43:09 +00:00
def _get_document_types ( self , cr , uid , context = None ) :
2008-07-22 15:11:28 +00:00
cr . execute ( ' select m.model, s.name from subscription_document s, ir_model m WHERE s.model = m.id order by s.name ' )
return cr . fetchall ( )
2006-12-07 13:41:40 +00:00
class subscription_subscription ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " subscription.subscription "
_description = " Subscription "
_columns = {
2014-05-21 09:52:05 +00:00
' name ' : fields . char ( ' Name ' , required = True ) ,
2010-11-15 11:12:22 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to False, it will allow you to hide the subscription without removing it. " ) ,
2008-07-22 15:11:28 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' ) ,
2013-03-21 05:28:49 +00:00
' notes ' : fields . text ( ' Internal Notes ' ) ,
2008-07-22 15:11:28 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User ' , required = True ) ,
' interval_number ' : fields . integer ( ' Interval Qty ' ) ,
' interval_type ' : fields . selection ( [ ( ' days ' , ' Days ' ) , ( ' weeks ' , ' Weeks ' ) , ( ' months ' , ' Months ' ) ] , ' Interval Unit ' ) ,
' exec_init ' : fields . integer ( ' Number of documents ' ) ,
' date_init ' : fields . datetime ( ' First Date ' ) ,
2014-07-06 14:44:26 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' Draft ' ) , ( ' running ' , ' Running ' ) , ( ' done ' , ' Done ' ) ] , ' Status ' , copy = False ) ,
2010-09-06 12:34:40 +00:00
' doc_source ' : fields . reference ( ' Source Document ' , required = True , selection = _get_document_types , size = 128 , help = " User can choose the source document on which he wants to create documents " ) ,
2008-07-22 15:11:28 +00:00
' doc_lines ' : fields . one2many ( ' subscription.subscription.history ' , ' subscription_id ' , ' Documents created ' , readonly = True ) ,
2012-09-30 12:00:59 +00:00
' cron_id ' : fields . many2one ( ' ir.cron ' , ' Cron Job ' , help = " Scheduler which runs on subscription " , states = { ' running ' : [ ( ' readonly ' , True ) ] , ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2013-03-20 09:47:48 +00:00
' note ' : fields . text ( ' Notes ' , help = " Description or Summary of Subscription " ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' date_init ' : lambda * a : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
' user_id ' : lambda obj , cr , uid , context : uid ,
' active ' : lambda * a : True ,
' interval_number ' : lambda * a : 1 ,
' interval_type ' : lambda * a : ' months ' ,
' doc_source ' : lambda * a : False ,
' state ' : lambda * a : ' draft '
}
2006-12-07 13:41:40 +00:00
2013-03-20 15:34:30 +00:00
def _auto_end ( self , cr , context = None ) :
super ( subscription_subscription , self ) . _auto_end ( cr , context = context )
# drop the FK from subscription to ir.cron, as it would cause deadlocks
# during cron job execution. When model_copy() tries to write() on the subscription,
# it has to wait for an ExclusiveLock on the cron job record, but the latter
# is locked by the cron system for the duration of the job!
# FIXME: the subscription module should be reviewed to simplify the scheduling process
# and to use a unique cron job for all subscriptions, so that it never needs to
# be updated during its execution.
cr . execute ( " ALTER TABLE %s DROP CONSTRAINT %s " % ( self . _table , ' %s _cron_id_fkey ' % self . _table ) )
2010-11-22 10:37:53 +00:00
def set_process ( self , cr , uid , ids , context = None ) :
2010-12-13 06:43:09 +00:00
for row in self . read ( cr , uid , ids , context = context ) :
2008-07-22 15:11:28 +00:00
mapping = { ' name ' : ' name ' , ' interval_number ' : ' interval_number ' , ' interval_type ' : ' interval_type ' , ' exec_init ' : ' numbercall ' , ' date_init ' : ' nextcall ' }
res = { ' model ' : ' subscription.subscription ' , ' args ' : repr ( [ [ row [ ' id ' ] ] ] ) , ' function ' : ' model_copy ' , ' priority ' : 6 , ' user_id ' : row [ ' user_id ' ] and row [ ' user_id ' ] [ 0 ] }
for key , value in mapping . items ( ) :
res [ value ] = row [ key ]
id = self . pool . get ( ' ir.cron ' ) . create ( cr , uid , res )
self . write ( cr , uid , [ row [ ' id ' ] ] , { ' cron_id ' : id , ' state ' : ' running ' } )
return True
2006-12-07 13:41:40 +00:00
2010-11-22 10:37:53 +00:00
def model_copy ( self , cr , uid , ids , context = None ) :
2010-12-13 06:43:09 +00:00
for row in self . read ( cr , uid , ids , context = context ) :
2009-09-23 12:52:31 +00:00
if not row . get ( ' cron_id ' , False ) :
continue
2008-07-22 15:11:28 +00:00
cron_ids = [ row [ ' cron_id ' ] [ 0 ] ]
remaining = self . pool . get ( ' ir.cron ' ) . read ( cr , uid , cron_ids , [ ' numbercall ' ] ) [ 0 ] [ ' numbercall ' ]
try :
( model_name , id ) = row [ ' doc_source ' ] . split ( ' , ' )
id = int ( id )
2013-03-29 14:37:20 +00:00
model = self . pool [ model_name ]
2008-07-22 15:11:28 +00:00
except :
2013-04-29 07:15:57 +00:00
raise osv . except_osv ( _ ( ' Wrong Source Document! ' ) , _ ( ' Please provide another source document. \n This one does not exist! ' ) )
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
default = { ' state ' : ' draft ' }
doc_obj = self . pool . get ( ' subscription.document ' )
document_ids = doc_obj . search ( cr , uid , [ ( ' model.model ' , ' = ' , model_name ) ] )
doc = doc_obj . browse ( cr , uid , document_ids ) [ 0 ]
for f in doc . field_ids :
if f . value == ' date ' :
value = time . strftime ( ' % Y- % m- %d ' )
else :
value = False
default [ f . field . name ] = value
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
state = ' running '
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
# if there was only one remaining document to generate
# the subscription is over and we mark it as being done
if remaining == 1 :
state = ' done '
2013-03-29 14:37:20 +00:00
id = self . pool [ model_name ] . copy ( cr , uid , id , default , context )
2008-07-22 15:11:28 +00:00
self . pool . get ( ' subscription.subscription.history ' ) . create ( cr , uid , { ' subscription_id ' : row [ ' id ' ] , ' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) , ' document_id ' : model_name + ' , ' + str ( id ) } )
self . write ( cr , uid , [ row [ ' id ' ] ] , { ' state ' : state } )
return True
2006-12-07 13:41:40 +00:00
2011-09-17 12:18:17 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
for record in self . browse ( cr , uid , ids , context or { } ) :
if record . state == " running " :
2013-04-29 07:15:57 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' You cannot delete an active subscription! ' ) )
2011-09-17 12:18:17 +00:00
return super ( subscription_subscription , self ) . unlink ( cr , uid , ids , context )
2010-11-22 10:37:53 +00:00
def set_done ( self , cr , uid , ids , context = None ) :
2008-07-22 15:11:28 +00:00
res = self . read ( cr , uid , ids , [ ' cron_id ' ] )
ids2 = [ x [ ' cron_id ' ] [ 0 ] for x in res if x [ ' id ' ] ]
self . pool . get ( ' ir.cron ' ) . write ( cr , uid , ids2 , { ' active ' : False } )
self . write ( cr , uid , ids , { ' state ' : ' done ' } )
return True
2006-12-07 13:41:40 +00:00
2010-11-22 10:37:53 +00:00
def set_draft ( self , cr , uid , ids , context = None ) :
2008-07-22 15:11:28 +00:00
self . write ( cr , uid , ids , { ' state ' : ' draft ' } )
return True
2006-12-07 13:41:40 +00:00
class subscription_subscription_history ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " subscription.subscription.history "
_description = " Subscription history "
_rec_name = ' date '
_columns = {
' date ' : fields . datetime ( ' Date ' ) ,
' subscription_id ' : fields . many2one ( ' subscription.subscription ' , ' Subscription ' , ondelete = ' cascade ' ) ,
2012-02-02 11:32:16 +00:00
' document_id ' : fields . reference ( ' Source Document ' , required = True , selection = _get_document_types , size = 128 ) ,
2008-07-22 15:11:28 +00:00
}
2006-12-07 13:41:40 +00:00
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: