Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
2663 Visualizzazioni

i have written a code to import contacts to third party database and implemented shceduled action. Created new class, wrote a method to sync such that initial sync would import all contacts data and subsequent sync only syncs data in a way if write date is greater than last scheduled action date.

i have implemented a button as well becaus while running schedule action, self is always empty but running button it works, i have applied sql contraints as well. still scheduled action would not work as intended because it is not using existing recordsets and is always empty.

Any help.

/class my_class(models.Model):
_name = 'model.name'
# _description = ''

last_cron_update = fields.Datetime(string='Sync Time')
phone_id = fields.Integer(string='Unique Phonebook ID', required=True )
company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True,
default=lambda self: self\.env\.company\)

\ \ \ \ \ _sql_constraints\ =\ \[\
\ \ \ \ \ \ \ \ \('phone_id_company_uniq',\ 'unique\ \(phone_id\ ,company_id\)',\ 'The\ phone\ id\ must\ be\ unique\ for\ a\ company!'\)\
\ \ \ \ \]


/@api\.model
\ \ \ \ def\ sync_phone_book\(self\):

\ \ \ \ \ \ \ \ print\("\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+\+",\ self\)

\ \ \ \ \ \ \ \ url_id\ =\ '\.\.\.\.\.\.\.'
\ \ \ \ \ \ \ \ headers\ =\ \{'\.\.\.\.\.\.'\}

\ \ \ \ \ \ \ \ date\ =\ self\.last_cron_update
\ \ \ \ \ \ \ \ now\ =\ datetime\.datetime\.now\(\)

\ \ \ \ \ \ \ \ \#\ if\ not\ date:
\ \ \ \ \ \ \ \ \#\ \ \ \ \ partner_lines\ =\ self\.env\['res\.partner'\]\.search\(\[\('write_date',\ '\ \ \ \ \ \ \ \ \#\ else:
\ \ \ \ \ \ \ \ \#\ \ \ \ \ partner_lines\ =\ self\.env\['res\.partner'\]\.search\(\[\('write_date',\ '>',\ date\)\]\)



\ \ \ \ \ \ \ \ if\ self\.last_cron_update:
\ \ \ \ \ \ \ \ \ \ \ \ partner_lines\ =\ self\.env\['res\.partner'\]\.search\(\[\('write_date',\ '>',\ date\)\]\)
\ \ \ \ \ \ \ \ else:
\ \ \ \ \ \ \ \ \ \ \ \ partner_lines\ =\ self\.env\['res\.partner'\]\.search\(\[\('write_date',\ '
\ \ \ \ \ \ \ \ for\ line\ in\ partner_lines:
\ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \ updates\ =\ requests\.post\(url_id,\ headers=headers,\ data=\{'name':line\.name,\ 'office':line\.phone,\ 'mobile':line\.mobile\}\)

\ \ \ \ \ \ \ \ \ \ \ \ print\(updates\.status_code\)
\ \ \ \ \ \ \ \ \ \ \ \ print\(updates\.text\)
\ \ \ \ \ \ \ \ \ \ \ \ print\("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\)

\ \ \ \ \ \ \ \ \#\ last_cron_update\ =\ fields\.Date\.context_today\(self,\ timestamp=None\)

\ \ \ \ \ \ \ \ self\.last_cron_update\ =\ \ fields\.Datetime\.now\(\)

\ \ \ \ \ \ \ \ \#\ self\.last_cron_update\ =\ self\.write\(\{'last_cron_update':\ fields\.Datetime\.now\(\)\}\)
\ \ \ \
\ \ \ \ \ \ \ \ \#\ if\ not\ date:
\ \ \ \ \ \ \ \ \#\ \ \ \ \ self\.create\(\{'last_cron_update':\ fields\.Datetime\.now\(\)\}\)
\ \ \ \ \ \ \ \ \#\ else:
\ \ \ \ \ \ \ \ \#\ \ \ \ \ self\.write\(\{'last_cron_update':\ fields.Datetime.now()})

print("==================================================================================")
print(self.last_cron_update)

Avatar
Abbandona
Risposta migliore

Hello!

If you want to use that method with a cron, you must use a search to get the records.
When the conjob run odoo does't know the records that you want to run it on
it works on your button because you are running the method directly in that record

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
dic 22
2719
2
nov 22
2289
1
mar 22
3337
2
mar 22
3277
0
gen 24
1216