Files
repair/odoo/addons/dsrpt_address_book/models/dsrpt_contact_communication.py

58 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import ValidationError
class ContactCommunication(models.Model):
_name = 'dsrpt.contact.communication'
_description = 'Contact Communication'
_order = 'is_preferred desc, id'
contact_id = fields.Many2one(
'dsrpt.contact',
string='Contact',
required=True,
ondelete='cascade'
)
communication_type_id = fields.Many2one(
'dsrpt.communication.type',
string='Type',
required=True
)
value = fields.Char(string='Value', required=True)
is_preferred = fields.Boolean(string='Preferred', default=False)
@api.constrains('is_preferred')
def _check_single_preferred(self):
"""Ensure only one preferred communication per contact"""
for record in self:
if record.is_preferred:
# Проверяем есть ли другие preferred для этого контакта
domain = [
('contact_id', '=', record.contact_id.id),
('is_preferred', '=', True),
('id', '!=', record.id)
]
if self.search_count(domain) > 0:
raise ValidationError(
'Only one communication method can be marked as preferred per contact.'
)
@api.model
def create(self, vals):
# Если это первый способ связи для контакта, делаем его preferred
if 'contact_id' in vals and not vals.get('is_preferred'):
existing = self.search_count([('contact_id', '=', vals['contact_id'])])
if existing == 0:
vals['is_preferred'] = True
return super().create(vals)
def name_get(self):
result = []
for record in self:
name = f"{record.communication_type_id.name}: {record.value}"
if record.is_preferred:
name = f"{name}"
result.append((record.id, name))
return result