Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use a unique cron job for all subscriptions system ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 321
    Comment on it

    If you want to use a unique cron job for all subscriptions system then follow the following code in your subscription module in .py file.

    
     def set_process(self, cr, uid, ids, context=None):
            for row in self.read(cr, uid, ids, context=context):
                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
    
        def model_copy(self, cr, uid, ids, context=None):
            for row in self.read(cr, uid, ids, context=context):
                if not row.get('cron_id',False):
                    continue
                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)
                    model = self.pool.get(model_name)
                except:
                    raise osv.except_osv(_('Wrong Source Document!'), _('Please provide another source document.\nThis one does not exist!'))
    
                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
    
                state = 'running'
    
                # 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'
                id = self.pool.get(model_name).copy(cr, uid, id, default, context)
                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
    
        def unlink(self, cr, uid, ids, context=None):
            for record in self.browse(cr, uid, ids, context or {}):
                if record.state=="running":
                    raise osv.except_osv(_('Error!'),_('You cannot delete an active subscription!'))
            return super(subscription_subscription, self).unlink(cr, uid, ids, context)
    
        def set_done(self, cr, uid, ids, context=None):
            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
    
        def set_draft(self, cr, uid, ids, context=None):
            self.write(cr, uid, ids, {'state':'draft'})
            return True
    subscription_subscription()
    
    class subscription_subscription_history(osv.osv):
        _name = "subscription.subscription.history"
        _description = "Subscription history"
        _rec_name = 'date'
        _columns = {
            'date': fields.datetime('Date'),
            'subscription_id': fields.many2one('subscription.subscription', 'Subscription', ondelete='cascade'),
            'document_id': fields.reference('Source Document', required=True, selection=_get_document_types, size=128),
        }
    subscription_subscription_history()
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: