33 lines
980 B
Python
33 lines
980 B
Python
# -*- coding: utf-8 -*-
|
|
import re
|
|
from odoo import api, models, fields
|
|
|
|
|
|
class GameSensitiveWord(models.Model):
|
|
_name = 'game.sensitive.word'
|
|
_description = '聊天敏感词'
|
|
_order = 'name'
|
|
|
|
name = fields.Char(string='敏感词', required=True, index=True)
|
|
active = fields.Boolean(string='启用', default=True)
|
|
replacement = fields.Char(string='替换为', default='**')
|
|
|
|
_sql_constraints = [
|
|
('name_uniq', 'unique(name)', '敏感词不能重复'),
|
|
]
|
|
|
|
@api.model
|
|
def sanitize(self, text):
|
|
"""将文本中的敏感词替换为对应替换字符(默认 **)。"""
|
|
if not text:
|
|
return text
|
|
words = self.search([('active', '=', True)])
|
|
if not words:
|
|
return text
|
|
result = text
|
|
for w in words:
|
|
pat = re.escape(w.name)
|
|
repl = w.replacement or '**'
|
|
result = re.sub(pat, repl, result, flags=re.IGNORECASE)
|
|
return result
|