Skip to Content
Menu
This question has been flagged

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
Discard
Best Answer

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
Discard
Related Posts Replies Views Activity
1
Dec 22
1856
2
Nov 22
1270
1
Mar 22
1948
2
Mar 22
2235
0
Jan 24
376