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'

    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"

    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


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")
    
    @api.onchange('type')
    def _onchange_type(self):
        if self.type == 'admin':
            self.role = False
            self.region_id = False
            self.district_id = False
            self.mahalla_id = False
        self._update_role_visibility()
    
    @api.onchange('role')
    def _onchange_role(self):
        if self.type != 'admin':
            self._update_role_visibility()
    
    def _update_role_visibility(self):
        if self.type == 'admin':
            pass
        elif self.role == 'republic':
            pass
        elif self.role == 'region':
            self.district_id = False
            self.mahalla_id = False
        elif self.role == 'district':
            self.mahalla_id = False
        elif self.role == 'mahalla':
            pass

    def action_change_password(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'change.password.wizard',
            'view_mode': 'form',
            'view_id': self.env.ref('base.change_password_wizard_view').id,
            'target': 'new',
            'context': {'default_user_id': self.id},
        }

    def assign_group_based_on_role(self):
        self.ensure_one()

        super_admin_group = self.env.ref('mahallam.group_super_admin')
        moderator_group = self.env.ref('mahallam.group_moderator')
        reader_group = self.env.ref('mahallam.group_reader')
        republic_moderator_group = self.env.ref('mahallam.group_republic_moderator')
        republic_reader_group = self.env.ref('mahallam.group_republic_reader')
        region_moderator_group = self.env.ref('mahallam.group_region_moderator')
        region_reader_group = self.env.ref('mahallam.group_region_reader')
        district_moderator_group = self.env.ref('mahallam.group_district_moderator')
        district_reader_group = self.env.ref('mahallam.group_district_reader')
        mahalla_moderator_group = self.env.ref('mahallam.group_mahalla_moderator')
        mahalla_reader_group = self.env.ref('mahallam.group_mahalla_reader')

        self.groups_id = [(3, super_admin_group.id), (3, moderator_group.id), (3, reader_group.id),
                            (3, republic_moderator_group.id), (3, republic_reader_group.id),
                            (3, region_moderator_group.id), (3, region_reader_group.id),
                            (3, district_moderator_group.id), (3, district_reader_group.id),
                            (3, mahalla_moderator_group.id), (3, mahalla_reader_group.id)]

        if self.type == 'admin':
            self.groups_id = [(4, super_admin_group.id)]
        elif self.type == 'moderator':
            self.groups_id = [(4, moderator_group.id)]
            if self.role == 'republic':
                self.groups_id = [(4, republic_moderator_group.id)]
            elif self.role == 'region':
                self.groups_id = [(4, region_moderator_group.id)]
            elif self.role == 'district':
                self.groups_id = [(4, district_moderator_group.id)]
            elif self.role == 'mahalla':
                self.groups_id = [(4, mahalla_moderator_group.id)]
        elif self.type == 'reader':
            self.groups_id = [(4, reader_group.id)]
            if self.role == 'republic':
                self.groups_id = [(4, republic_reader_group.id)]
            elif self.role == 'region':
                self.groups_id = [(4, region_reader_group.id)]
            elif self.role == 'district':
                self.groups_id = [(4, district_reader_group.id)]
            elif self.role == 'mahalla':
                self.groups_id = [(4, mahalla_reader_group.id)]

    @api.model
    def create(self, vals):
        user = super(Users, self).create(vals)
        user.assign_group_based_on_role()
        return user

    def write(self, vals):
        res = super(Users, self).write(vals)
        if 'type' in vals or 'role' in vals:
            self.assign_group_based_on_role()
        return res