i have project on odoo17 and i wanna make one functionality, when res_user creates some data(announcement, news, questionnaires) it gets users localization data based on users role and if type is moderator, dont touch to other types, and sets automaticly their localization data relevant to my content creation process, like if user is district, content will be created to all mahallas in this district and all these processes should be automatic when user just creates data
announcement.py:from odoo import models, fields, api , _
from odoo.exceptions import ValidationError
class Announcement(models.Model):
_name = 'announcement'
_description = 'Yangiliklar , Elonlar va Mahhallada nima gap'
code
Assistant
name = fields.Char(string=_('Name'), translate=True)
description = fields.Text(string=_("Description"), translate=True)
content = fields.Html(string=_('Content (HTML)'), translate=True)
image = fields.Binary(string=_('Image'))
type = fields.Selection(
[("mahallada_nima_gap", _('Mahallada Nima Gap')), ('announcement', _('Announcement')), ('news', _('News'))])
views_count = fields.Integer(string=_("Views"), readonly=True, compute='_compute_views_count', store=True)
is_country_based = fields.Boolean(default=False)
user_is_read = fields.Many2many('res.partner', string=_('Read Users'))
created_at = fields.Datetime(default=fields.Datetime.now, string=_('Created Date'))
location_type = fields.Selection([
('all', _('All')),
('region', _('Region')),
('district', _('District')),
('mahalla', _('Mahalla'))
], string=_('Location Type'), required=True, default='all')
to_all = fields.Boolean(string=_('Is Republic'))
region_id = fields.Many2many('region', string=_("Region"), required=False)
to_region = fields.Boolean()
district_id = fields.Many2many('district', string=_("District"), required=False)
to_district = fields.Boolean()
mahalla_id = fields.Many2many('mahalla', string=_("Mahalla"), required=False)
to_mahalla = fields.Boolean()
_sql_constraints = [
('unique_home_utility_type', 'unique(home_id, type)',
'Each home can have only one utility of each type.')
]
def create(self, vals):
record = super().create(vals)
partners = self.env['res.partner'].search([('mahalla_id', 'in', record.mahalla_id.ids)]).ids
self.env['mobile.app.push.notification'].create({
'name': f"New {record.type}",
'body': record.name,
'notification_type': 'announcement',
'model_name': 'announcement',
'object_id': record.id,
'send_notification_to': 'to_partner',
'partner_ids': [(6, 0, partners)]
})
return record
@api.depends('user_is_read')
def _compute_views_count(self):
for rec in self:
rec.views_count = len(rec.user_is_read)
@api.onchange('to_all')
def _onchange_to_all(self):
if self.to_all:
domain = []
mahallas = self.env['mahalla'].search(domain)
self.mahalla_id = [(6, 0, mahallas.ids)]
self.region_id = False
self.district_id = False
else:
self.mahalla_id = False
@api.onchange('region_id', 'region_id')
def _onchange_region_selection(self):
if self.to_region and self.region_id:
districts = self.env['district'].search([('region_id', 'in', self.region_id.ids)])
mahallas = self.env['mahalla'].search([('district_id', 'in', districts.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.district_id = False
elif not self.to_region:
if not self.to_all and not self.to_district:
self.mahalla_id = False
@api.onchange('to_district', 'district_id')
def _onchange_district_selection(self):
if not self.to_district and self.district_id:
mahallas = self.env['mahalla'].search([('district_id', 'in', self.district_id.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
elif not self.to_district:
if not self.to_all and not self.to_region:
self.mahalla_id = False
@api.onchange('location_type')
def on_change_location_type(self):
if self.location_type == 'all':
self.to_all = True
self.to_region = True
self.to_district = True
self.to_mahalla = True
self._onchange_to_all()
elif self.location_type == 'region':
self.to_all = True
self.to_region = False
self.to_district = True
self.to_mahalla = True
self._onchange_region_selection()
elif self.location_type == 'district':
self.to_all = True
self.to_region = False
self.to_district = False
self.to_mahalla = True
self._onchange_district_selection()
elif self.location_type == 'mahalla':
self.to_all = True
self.to_region = False
self.to_district = False
self.to_mahalla = False
@api.constrains('location_type', 'region_id', 'district_id', 'mahalla_id')
def _check_location_requirements(self):
for record in self:
if record.location_type == 'region' and not record.region_id:
raise ValidationError("Specify the region for the announcement.")
elif record.location_type == 'district':
if not record.region_id:
raise ValidationError("Specify the region for the district announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the announcement.")
elif record.location_type == 'mahalla':
if not record.region_id:
raise ValidationError("Specify the region for the mahalla announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the mahalla announcement.")
if not record.mahalla_id:
raise ValidationError("Specify the mahalla for the announcement.")
questionnaire.py: from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
class Questionnaire(models.Model):
_name = 'questionnaire'
_description = "So'rovnomalar"
code
name = fields.Char(string=_('Name'), translate=True)
is_finished = fields.Boolean(default=False, store=True)
user_id = fields.Many2one('res.partner', string=_('User'))
image = fields.Binary(string=_('Image'))
question_ids = fields.One2many('question', 'questionnaire_id', string=_('Questions'))
start_date = fields.Date(string=_('Start Date'), default=fields.Date.today)
end_date = fields.Date(string=_('End Date'), default=fields.Date.today)
location_type = fields.Selection([
('all', _('All')),
('region', _('Region')),
('district', _('District')),
('mahalla', _('Mahalla'))
], string=_('Location Type'), required=True, default='all')
to_all = fields.Boolean(string=_('Is Republic'))
region_id = fields.Many2many('region', string=_("Region"), required=False)
to_region = fields.Boolean()
district_id = fields.Many2many('district', string=_("District"), required=False)
to_district = fields.Boolean()
mahalla_id = fields.Many2many('mahalla', string=_("Mahalla"), required=False)
to_mahalla = fields.Boolean()
@api.onchange('to_all')
def _onchange_to_all(self):
if self.to_all:
domain = []
mahallas = self.env['mahalla'].search(domain)
self.mahalla_id = [(6, 0, mahallas.ids)]
self.region_id = False
self.district_id = False
else:
self.mahalla_id = False
@api.onchange('to_region', 'region_id')
def _onchange_region_selection(self):
if self.to_region and self.region_id:
districts = self.env['district'].search([('region_id', 'in', self.region_id.ids)])
mahallas = self.env['mahalla'].search([('district_id', 'in', districts.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.district_id = False
elif not self.to_region:
if not self.to_all and not self.to_district:
self.mahalla_id = False
@api.onchange('to_district', 'district_id')
def _onchange_district_selection(self):
if self.to_district and self.district_id:
mahallas = self.env['mahalla'].search([('district_id', 'in', self.district_id.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
elif not self.to_district:
if not self.to_all and not self.to_region:
self.mahalla_id = False
@api.onchange('location_type')
def on_change_location_type(self):
if self.location_type == 'all':
self.to_all = True
self.to_region = True
self.to_district = True
self.to_mahalla = True
self._onchange_to_all()
elif self.location_type == 'region':
self.to_all = True
self.to_region = False
self.to_district = True
self.to_mahalla = True
self._onchange_region_selection()
elif self.location_type == 'district':
self.to_all = False
self.to_region = False
self.to_district = False
self.to_mahalla = True
self._onchange_district_selection()
elif self.location_type == 'mahalla':
self.to_all = False
self.to_region = False
self.to_district = False
self.to_mahalla = False
@api.constrains('location_type', 'region_id', 'district_id', 'mahalla_id')
def _check_location_requirements(self):
for record in self:
if record.location_type == 'region' and not record.region_id:
raise ValidationError("Specify the region for the announcement.")
elif record.location_type == 'district':
if not record.region_id:
raise ValidationError("Specify the region for the district announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the announcement.")
elif record.location_type == 'mahalla':
if not record.region_id:
raise ValidationError("Specify the region for the mahalla announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the mahalla announcement.")
if not record.mahalla_id:
raise ValidationError("Specify the mahalla for the announcement.")
@api.constrains('start_date', 'end_date')
def _check_date_range(self):
for record in self:
if record.start_date > record.end_date:
raise ValidationError("Start date cannot be higher than end date.")
my res_user model:
from odoo import fields, models, api
from odoo.exceptions import AccessError
To implement the desired functionality in Odoo 17—automatically setting localization data based on the user's role when creating records (such as Announcements, News, and Questionnaires)—you can follow a structured approach. This involves customizing the create and possibly write methods in your models to enforce localization rules based on the current user's role. Below is a comprehensive guide to achieve this:
Overview
Determine User Role and Localization: Identify the current user's type and role to set the appropriate localization fields.
Override create and write Methods: Ensure that when a user creates or updates a record, the localization data is automatically set based on their role.
Restrict Manual Changes: Prevent moderators from manually altering localization fields if they should be set automatically.
Modularize the Logic: Implement helper methods or mixins to avoid code duplication across different models.
Step-by-Step Implementation
1. Update the res.users Model
Ensure that your res.users model correctly captures the user's type and role. From your provided code, it seems well-defined. However, to streamline access to the role and related localization fields, you might add some helper methods or properties.
python
# models/res_users.py
from odoo import fields, models, api
class Users(models.Model):
_inherit = "res.users"
position = fields.Char(string='Position')
type = fields.Selection([
('admin', 'Admin'),
('moderator', 'Moderator'),
('reader', 'Reader'),
], string='User Type', required=True, default='reader')
role = fields.Selection([
('republic', 'Republic'),
('region', 'Region'),
('district', 'District'),
('mahalla', 'Mahalla'),
], string='Role', default='mahalla')
region_id = fields.Many2one('region', string="Region")
district_id = fields.Many2one('district', string="District")
mahalla_id = fields.Many2one('mahalla', string="Mahalla")
# Existing onchange and group assignment methods...
2. Create a Mixin for Localization Logic
To avoid duplicating code across multiple models (e.g., announcement, questionnaire), create a mixin that handles setting localization fields based on the current user's role.
Integrate the LocalizationMixin into your announcement model and override the create and write methods to apply localization based on the user's role.
python
4. Update the Questionnaire Model
Similarly, integrate the LocalizationMixin into your questionnaire model.
python
5. (Optional) Update Other Models
If you have other models like news that require similar localization logic, integrate the LocalizationMixin and override the necessary methods following the pattern shown above.
6. Restrict Access to Localization Fields for Moderators
To ensure that moderators cannot manually alter localization fields (since these should be set automatically based on their role), you can adjust the view definitions to make these fields readonly for moderators.
Note: Adjust the attrs accordingly based on how you determine a user's type in the view. If type is a field on res.users, you might need to use a related field or compute a field on the model to reflect the current user's type.
7. Testing the Implementation
After implementing the above changes, perform thorough testing to ensure that:
Moderators: When creating a record, localization fields are automatically set based on their role, and they cannot alter these fields manually.
Admins and Other Users: Can set localization fields manually as needed.
Notifications: Are sent appropriately based on the user’s type and the created record's localization.
8. Additional Considerations
Security: Ensure that access rights and record rules are appropriately set so that users can only see and create records within their localization scope.
User Interface Enhancements: Provide clear indicators in the UI to show users when localization fields are auto-set versus manually set.
Error Handling: Ensure that validations are robust to prevent inconsistent localization data.
Complete Example
Here's a consolidated example incorporating the above steps for the announcement model.
Moderators: When creating Announcements or Questionnaires, their localization fields are set automatically based on their role (republic, region, district, mahalla). They cannot manually alter these fields, ensuring consistency and adherence to their scope.
Admins and Other Users: Retain the ability to set localization fields manually, providing flexibility where needed.
Modularity: The LocalizationMixin ensures that any future models requiring similar localization logic can easily inherit and apply the same rules, promoting DRY (Don't Repeat Yourself) principles.
User Experience: Restricting access to localization fields for moderators prevents accidental or intentional overrides that could lead to inconsistent data.
Ensure you test thoroughly in a development environment before deploying to production to verify that the localization logic behaves as expected across different user types and roles.
Not yet included in a published release.
# models/announcement.py
from odoo import models, fields, api , _
from odoo.exceptions import ValidationError
from .mixins import LocalizationMixin
class Announcement(LocalizationMixin, models.Model):
_name = 'announcement'
_description = 'Yangiliklar , Elonlar va Mahhallada nima gap'
name = fields.Char(string=_('Name'), translate=True)
description = fields.Text(string=_("Description"), translate=True)
content = fields.Html(string=_('Content (HTML)'), translate=True)
image = fields.Binary(string=_('Image'))
type = fields.Selection(
[("mahallada_nima_gap", _('Mahallada Nima Gap')),
('announcement', _('Announcement')),
('news', _('News'))], default='announcement')
views_count = fields.Integer(string=_("Views"), readonly=True, compute='_compute_views_count', store=True)
is_country_based = fields.Boolean(default=False)
user_is_read = fields.Many2many('res.partner', string=_('Read Users'))
created_at = fields.Datetime(default=fields.Datetime.now, string=_('Created Date'))
location_type = fields.Selection([
('all', _('All')),
('region', _('Region')),
('district', _('District')),
('mahalla', _('Mahalla'))
], string=_('Location Type'), required=True, default='all')
to_all = fields.Boolean(string=_('Is Republic'))
region_id = fields.Many2many('region', string=_("Region"), required=False)
to_region = fields.Boolean()
district_id = fields.Many2many('district', string=_("District"), required=False)
to_district = fields.Boolean()
mahalla_id = fields.Many2many('mahalla', string=_("Mahalla"), required=False)
to_mahalla = fields.Boolean()
_sql_constraints = [
('unique_home_utility_type', 'unique(home_id, type)',
'Each home can have only one utility of each type.')
]
@api.model
def create(self, vals):
# Apply localization based on user role
vals = self._get_user_localization_vals(vals)
record = super(Announcement, self).create(vals)
# Send notifications only if the user is not a moderator
if self.env.user.type != 'moderator':
partners = self.env['res.partner'].search([('mahalla_id', 'in', record.mahalla_id.ids)]).ids
self.env['mobile.app.push.notification'].create({
'name': f"New {record.type}",
'body': record.name,
'notification_type': 'announcement',
'model_name': 'announcement',
'object_id': record.id,
'send_notification_to': 'to_partner',
'partner_ids': [(6, 0, partners)]
})
return record
def write(self, vals):
# Apply localization based on user role if creating as a moderator
if self.env.user.type == 'moderator':
vals = self._get_user_localization_vals(vals)
return super(Announcement, self).write(vals)
@api.depends('user_is_read')
def _compute_views_count(self):
for rec in self:
rec.views_count = len(rec.user_is_read)
@api.onchange('to_all')
def _onchange_to_all(self):
if self.to_all:
mahallas = self.env['mahalla'].search([])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.region_id = False
self.district_id = False
else:
self.mahalla_id = False
@api.onchange('region_id')
def _onchange_region_selection(self):
if self.to_region and self.region_id:
districts = self.env['district'].search([('region_id', 'in', self.region_id.ids)])
mahallas = self.env['mahalla'].search([('district_id', 'in', districts.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.district_id = False
elif not self.to_region and not self.to_all and not self.to_district:
self.mahalla_id = False
@api.onchange('district_id')
def _onchange_district_selection(self):
if self.to_district and self.district_id:
mahallas = self.env['mahalla'].search([('district_id', 'in', self.district_id.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
elif not self.to_district and not self.to_all and not self.to_region:
self.mahalla_id = False
@api.onchange('location_type')
def on_change_location_type(self):
if self.location_type == 'all':
self.to_all = True
self.to_region = False
self.to_district = False
self.to_mahalla = False
self._onchange_to_all()
elif self.location_type == 'region':
self.to_all = False
self.to_region = True
self.to_district = False
self.to_mahalla = False
self._onchange_region_selection()
elif self.location_type == 'district':
self.to_all = False
self.to_region = False
self.to_district = True
self.to_mahalla = False
self._onchange_district_selection()
elif self.location_type == 'mahalla':
self.to_all = False
self.to_region = False
self.to_district = False
self.to_mahalla = True
@api.constrains('location_type', 'region_id', 'district_id', 'mahalla_id')
def _check_location_requirements(self):
for record in self:
if record.location_type == 'region' and not record.region_id:
raise ValidationError("Specify the region for the announcement.")
elif record.location_type == 'district':
if not record.region_id:
raise ValidationError("Specify the region for the district announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the announcement.")
elif record.location_type == 'mahalla':
if not record.region_id:
raise ValidationError("Specify the region for the mahalla announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the mahalla announcement.")
if not record.mahalla_id:
raise ValidationError("Specify the mahalla for the announcement.")
# models/questionnaire.py
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
from .mixins import LocalizationMixin
class Questionnaire(LocalizationMixin, models.Model):
_name = 'questionnaire'
_description = "So'rovnomalar"
name = fields.Char(string=_('Name'), translate=True)
is_finished = fields.Boolean(default=False, store=True)
user_id = fields.Many2one('res.partner', string=_('User'))
image = fields.Binary(string=_('Image'))
question_ids = fields.One2many('question', 'questionnaire_id', string=_('Questions'))
start_date = fields.Date(string=_('Start Date'), default=fields.Date.today)
end_date = fields.Date(string=_('End Date'), default=fields.Date.today)
location_type = fields.Selection([
('all', _('All')),
('region', _('Region')),
('district', _('District')),
('mahalla', _('Mahalla'))
], string=_('Location Type'), required=True, default='all')
to_all = fields.Boolean(string=_('Is Republic'))
region_id = fields.Many2many('region', string=_("Region"), required=False)
to_region = fields.Boolean()
district_id = fields.Many2many('district', string=_("District"), required=False)
to_district = fields.Boolean()
mahalla_id = fields.Many2many('mahalla', string=_("Mahalla"), required=False)
to_mahalla = fields.Boolean()
@api.model
def create(self, vals):
# Apply localization based on user role
vals = self._get_user_localization_vals(vals)
record = super(Questionnaire, self).create(vals)
# Additional logic if needed
return record
def write(self, vals):
# Apply localization based on user role if updating as a moderator
if self.env.user.type == 'moderator':
vals = self._get_user_localization_vals(vals)
return super(Questionnaire, self).write(vals)
@api.onchange('to_all')
def _onchange_to_all(self):
if self.to_all:
mahallas = self.env['mahalla'].search([])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.region_id = False
self.district_id = False
else:
self.mahalla_id = False
@api.onchange('region_id')
def _onchange_region_selection(self):
if self.to_region and self.region_id:
districts = self.env['district'].search([('region_id', 'in', self.region_id.ids)])
mahallas = self.env['mahalla'].search([('district_id', 'in', districts.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.district_id = False
elif not self.to_region and not self.to_all and not self.to_district:
self.mahalla_id = False
@api.onchange('district_id')
def _onchange_district_selection(self):
if self.to_district and self.district_id:
mahallas = self.env['mahalla'].search([('district_id', 'in', self.district_id.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
elif not self.to_district and not self.to_all and not self.to_region:
self.mahalla_id = False
@api.onchange('location_type')
def on_change_location_type(self):
if self.location_type == 'all':
self.to_all = True
self.to_region = False
self.to_district = False
self.to_mahalla = False
self._onchange_to_all()
elif self.location_type == 'region':
self.to_all = False
self.to_region = True
self.to_district = False
self.to_mahalla = False
self._onchange_region_selection()
elif self.location_type == 'district':
self.to_all = False
self.to_region = False
self.to_district = True
self.to_mahalla = False
self._onchange_district_selection()
elif self.location_type == 'mahalla':
self.to_all = False
self.to_region = False
self.to_district = False
self.to_mahalla = True
@api.constrains('location_type', 'region_id', 'district_id', 'mahalla_id')
def _check_location_requirements(self):
for record in self:
if record.location_type == 'region' and not record.region_id:
raise ValidationError("Specify the region for the questionnaire.")
elif record.location_type == 'district':
if not record.region_id:
raise ValidationError("Specify the region for the district questionnaire.")
if not record.district_id:
raise ValidationError("Specify the district for the questionnaire.")
elif record.location_type == 'mahalla':
if not record.region_id:
raise ValidationError("Specify the region for the mahalla questionnaire.")
if not record.district_id:
raise ValidationError("Specify the district for the mahalla questionnaire.")
if not record.mahalla_id:
raise ValidationError("Specify the mahalla for the questionnaire.")
@api.constrains('start_date', 'end_date')
def _check_date_range(self):
for record in self:
if record.start_date > record.end_date:
raise ValidationError("Start date cannot be higher than end date.")
# models/announcement.py
from odoo import models, fields, api , _
from odoo.exceptions import ValidationError
from .mixins import LocalizationMixin
class Announcement(LocalizationMixin, models.Model):
_name = 'announcement'
_description = 'Yangiliklar , Elonlar va Mahhallada nima gap'
name = fields.Char(string=_('Name'), translate=True)
description = fields.Text(string=_("Description"), translate=True)
content = fields.Html(string=_('Content (HTML)'), translate=True)
image = fields.Binary(string=_('Image'))
type = fields.Selection(
[("mahallada_nima_gap", _('Mahallada Nima Gap')),
('announcement', _('Announcement')),
('news', _('News'))], default='announcement')
views_count = fields.Integer(string=_("Views"), readonly=True, compute='_compute_views_count', store=True)
is_country_based = fields.Boolean(default=False)
user_is_read = fields.Many2many('res.partner', string=_('Read Users'))
created_at = fields.Datetime(default=fields.Datetime.now, string=_('Created Date'))
location_type = fields.Selection([
('all', _('All')),
('region', _('Region')),
('district', _('District')),
('mahalla', _('Mahalla'))
], string=_('Location Type'), required=True, default='all')
to_all = fields.Boolean(string=_('Is Republic'))
region_id = fields.Many2many('region', string=_("Region"), required=False)
to_region = fields.Boolean()
district_id = fields.Many2many('district', string=_("District"), required=False)
to_district = fields.Boolean()
mahalla_id = fields.Many2many('mahalla', string=_("Mahalla"), required=False)
to_mahalla = fields.Boolean()
_sql_constraints = [
('unique_home_utility_type', 'unique(home_id, type)',
'Each home can have only one utility of each type.')
]
@api.model
def create(self, vals):
# Apply localization based on user role
vals = self._get_user_localization_vals(vals)
record = super(Announcement, self).create(vals)
# Send notifications only if the user is not a moderator
if self.env.user.type != 'moderator':
partners = self.env['res.partner'].search([('mahalla_id', 'in', record.mahalla_id.ids)]).ids
self.env['mobile.app.push.notification'].create({
'name': f"New {record.type}",
'body': record.name,
'notification_type': 'announcement',
'model_name': 'announcement',
'object_id': record.id,
'send_notification_to': 'to_partner',
'partner_ids': [(6, 0, partners)]
})
return record
def write(self, vals):
# Apply localization based on user role if updating as a moderator
if self.env.user.type == 'moderator':
vals = self._get_user_localization_vals(vals)
return super(Announcement, self).write(vals)
@api.depends('user_is_read')
def _compute_views_count(self):
for rec in self:
rec.views_count = len(rec.user_is_read)
@api.onchange('to_all')
def _onchange_to_all(self):
if self.to_all:
mahallas = self.env['mahalla'].search([])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.region_id = False
self.district_id = False
else:
self.mahalla_id = False
@api.onchange('region_id')
def _onchange_region_selection(self):
if self.to_region and self.region_id:
districts = self.env['district'].search([('region_id', 'in', self.region_id.ids)])
mahallas = self.env['mahalla'].search([('district_id', 'in', districts.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
self.district_id = False
elif not self.to_region and not self.to_all and not self.to_district:
self.mahalla_id = False
@api.onchange('district_id')
def _onchange_district_selection(self):
if self.to_district and self.district_id:
mahallas = self.env['mahalla'].search([('district_id', 'in', self.district_id.ids)])
self.mahalla_id = [(6, 0, mahallas.ids)]
elif not self.to_district and not self.to_all and not self.to_region:
self.mahalla_id = False
@api.onchange('location_type')
def on_change_location_type(self):
if self.location_type == 'all':
self.to_all = True
self.to_region = False
self.to_district = False
self.to_mahalla = False
self._onchange_to_all()
elif self.location_type == 'region':
self.to_all = False
self.to_region = True
self.to_district = False
self.to_mahalla = False
self._onchange_region_selection()
elif self.location_type == 'district':
self.to_all = False
self.to_region = False
self.to_district = True
self.to_mahalla = False
self._onchange_district_selection()
elif self.location_type == 'mahalla':
self.to_all = False
self.to_region = False
self.to_district = False
self.to_mahalla = True
@api.constrains('location_type', 'region_id', 'district_id', 'mahalla_id')
def _check_location_requirements(self):
for record in self:
if record.location_type == 'region' and not record.region_id:
raise ValidationError("Specify the region for the announcement.")
elif record.location_type == 'district':
if not record.region_id:
raise ValidationError("Specify the region for the district announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the announcement.")
elif record.location_type == 'mahalla':
if not record.region_id:
raise ValidationError("Specify the region for the mahalla announcement.")
if not record.district_id:
raise ValidationError("Specify the district for the mahalla announcement.")
if not record.mahalla_id:
raise ValidationError("Specify the mahalla for the announcement.")