Merge pull request #1163 from expsa/benefit_security

Make computed fields searchable
This commit is contained in:
enagahh 2024-09-15 16:31:55 +03:00 committed by GitHub
commit bc9b54e027
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 2 deletions

View File

@ -36,7 +36,7 @@ class FamilyMemberProfile(models.Model):
('girls_home','Girls Home'),('university_housing','University Housing'),('with_husband','With_husband'),('work_inside_saudi_arabia','Work Inside Saudi Arabia')], string="Member Location")
# member_location = fields.Many2one('member.location', string="Member Location")
birth_date = fields.Date(string="Birth Date")
age = fields.Integer(string="Age", compute='_compute_get_age_date')
age = fields.Integer(string="Age", compute='_compute_get_age_date',search = '_search_age')
is_work = fields.Boolean('Is Work?')
is_dead = fields.Boolean('Is Dead?')
member_income = fields.Float('Member Income')
@ -153,7 +153,7 @@ class FamilyMemberProfile(models.Model):
member_status = fields.Selection(selection=[
('benefit', 'Benefit'),
('non_benefit', 'Non Benefit'),
], string='Benefit Status', compute="check_member_status",default = False)
], string='Benefit Status', compute="check_member_status",default = False,search ='_search_member_status')
suspend_reason = fields.Many2one('suspend.reason', string='Suspend Reason')
reason = fields.Text(string='Reason')
suspend_description = fields.Text(string='Suspend Description')
@ -163,6 +163,47 @@ class FamilyMemberProfile(models.Model):
suspend_method = fields.Selection(selection=[('manual', 'Manual'), ('auto', 'Auto')], string="Suspend Method",default='auto')
is_member_workflow = fields.Boolean('Is Member Workflow?')
def _search_age(self, operator, value):
today = date.today()
# Calculate the date `value` years ago from today
birthdate_exact = today.replace(year=today.year - value)
# Adjust for the leap year and the day/month when computing age
if operator == '=':
# Look for exact matches in terms of year, month, and day
birthdate_lower = birthdate_exact.replace(year=birthdate_exact.year - 1)
return [
('birth_date', '>=', birthdate_lower),
('birth_date', '<=', birthdate_exact),
]
elif operator == '>':
# Return records where birth_date would give an age greater than `value`
return [('birth_date', '<=', birthdate_exact)]
elif operator == '<':
# Return records where birth_date would give an age less than `value`
return [('birth_date', '>=', birthdate_exact)]
else:
return []
def _search_member_status(self, operator, value):
# Treat 'is' as equivalent to '='
if operator == 'is':
operator = '='
members = self.search([]) # Fetch all records
matching_ids = []
for rec in members:
rec.check_member_status() # Ensure the status is computed
if operator == '=' and rec.member_status == value:
matching_ids.append(rec.id)
elif operator == '!=' and rec.member_status != value:
matching_ids.append(rec.id)
return [('id', 'in', matching_ids)]
def create_member_partner(self):
self.partner_id.write({
'email': self.email,