Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
2661 มุมมอง

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)

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ธ.ค. 22
2719
2
พ.ย. 22
2288
1
มี.ค. 22
3337
2
มี.ค. 22
3277
0
ม.ค. 24
1216