def tags(self, forum, tag_char=None, **post): # build the list of tag first char, with their value as tag_char param Ex : [('All', 'all'), ('C', 'c'), ('G', 'g'), ('Z', z)] first_char_tag = forum.get_tags_first_char() first_char_list = [(t, t.lower()) for t in first_char_tag if t.isalnum()] first_char_list.insert(0, (_('All'), 'all')) # get active first char tag active_char_tag = first_char_list[1][1] if len(first_char_list) > 1 else 'all' if tag_char: active_char_tag = tag_char.lower() # generate domain for searched tags domain = [('forum_id', '=', forum.id), ('posts_count', '>', 0)] order_by = 'name' if active_char_tag and active_char_tag != 'all': domain.append(('name', '=ilike', tools.escape_psql(active_char_tag)+'%')) order_by = 'posts_count DESC' tags = request.env['forum.tag'].search(domain, limit=None, order=order_by) # prepare values and render template values = self._prepare_forum_values(forum=forum, searches={'tags': True}, **post) values.update({ 'tags': tags, 'pager_tag_chars': first_char_list, 'active_char_tag': active_char_tag, }) return request.render("website_forum.tag", values)
def tags(self, forum, tag_char=None, **post): # build the list of tag first char, with their value as tag_char param Ex : [('All', 'all'), ('C', 'c'), ('G', 'g'), ('Z', z)] first_char_tag = forum.get_tags_first_char() first_char_list = [(t, t.lower()) for t in first_char_tag if t.isalnum()] first_char_list.insert(0, (_('All'), 'all')) # get active first char tag active_char_tag = first_char_list[1][1] if len( first_char_list) > 1 else 'all' if tag_char: active_char_tag = tag_char.lower() # generate domain for searched tags domain = [('forum_id', '=', forum.id), ('posts_count', '>', 0)] order_by = 'name' if active_char_tag and active_char_tag != 'all': domain.append( ('name', '=ilike', tools.escape_psql(active_char_tag) + '%')) order_by = 'posts_count DESC' tags = request.env['forum.tag'].search(domain, limit=None, order=order_by) # prepare values and render template values = self._prepare_forum_values(forum=forum, searches={'tags': True}, **post) values.update({ 'tags': tags, 'pager_tag_chars': first_char_list, 'active_char_tag': active_char_tag, }) return request.render("website_forum.tag", values)
def get_dynamic_snippet_templates(self, filter_name=False): domain = [['key', 'ilike', '.dynamic_filter_template_'], ['type', '=', 'qweb']] if filter_name: domain.append(['key', 'ilike', escape_psql('_%s_' % filter_name)]) templates = request.env['ir.ui.view'].sudo().search_read( domain, ['key', 'name']) return templates
def _check_email_list_ids(self): for contact in self: other_contact = self.search([ ('email', '=ilike', tools.escape_psql(contact.email)), ('id', '!=', contact.id) ]) if contact.list_ids & other_contact.mapped('list_ids'): raise ValidationError(_("Cannot have the same email more " "than once in the same list"))
def get_dynamic_snippet_templates(self, filter_name=False): domain = [['key', 'ilike', '.dynamic_filter_template_'], ['type', '=', 'qweb']] if filter_name: domain.append(['key', 'ilike', escape_psql('_%s_' % filter_name)]) templates = request.env['ir.ui.view'].sudo().search_read(domain, ['key', 'name', 'arch_db']) for t in templates: children = etree.fromstring(t.pop('arch_db')).getchildren() attribs = children and children[0].attrib or {} t['numOfEl'] = attribs.get('data-number-of-elements') t['numOfElSm'] = attribs.get('data-number-of-elements-sm') t['numOfElFetch'] = attribs.get('data-number-of-elements-fetch') return templates
def _search_build_domain(self, domain_list, search, fields, extra=None): """ Builds a search domain AND-combining a base domain with partial matches of each term in the search expression in any of the fields. :param domain_list: base domain list combined in the search expression :param search: search expression string :param fields: list of field names to match the terms of the search expression with :param extra: function that returns an additional subdomain for a search term :return: domain limited to the matches of the search expression """ domains = domain_list.copy() if search: for search_term in search.split(' '): subdomains = [[(field, 'ilike', escape_psql(search_term))] for field in fields] if extra: subdomains.append(extra(self.env, search_term)) domains.append(expression.OR(subdomains)) return expression.AND(domains)
def tags(self, forum, tag_char=None, **post): # build the list of tag first char, with their value as tag_char param Ex : [('All', 'all'), ('C', 'c'), ('G', 'g'), ('Z', z)] first_char_tag = forum.get_tags_first_char() first_char_list = [(t, t.lower()) for t in first_char_tag if t.isalnum()] first_char_list.insert(0, (_("All"), "all")) # get active first char tag active_char_tag = first_char_list[1][1] if len(first_char_list) > 1 else "all" if tag_char: active_char_tag = tag_char.lower() # generate domain for searched tags domain = [("forum_id", "=", forum.id), ("posts_count", ">", 0)] order_by = "name" if active_char_tag and active_char_tag != "all": domain.append(("name", "=ilike", tools.escape_psql(active_char_tag) + "%")) order_by = "posts_count DESC" tags = request.env["forum.tag"].search(domain, limit=None, order=order_by) # prepare values and render template values = self._prepare_forum_values(forum=forum, searches={"tags": True}, **post) values.update({"tags": tags, "pager_tag_chars": first_char_list, "active_char_tag": active_char_tag}) return request.render("website_forum.tag", values)
def _search_fetch(self, search_detail, search, limit, order): with_description = 'description' in search_detail['mapping'] results, count = super()._search_fetch(search_detail, search, limit, order) if with_description and search: # Perform search in translations # TODO Remove when domains will support xml_translate fields query = sql.SQL(""" SELECT {table}.{id} FROM {table} LEFT JOIN ir_ui_view v ON {table}.{view_id} = v.{id} LEFT JOIN ir_translation t ON v.{id} = t.{res_id} WHERE t.lang = {lang} AND t.name = ANY({names}) AND t.type = 'model_terms' AND t.value ilike {search} LIMIT {limit} """).format( table=sql.Identifier(self._table), id=sql.Identifier('id'), view_id=sql.Identifier('view_id'), res_id=sql.Identifier('res_id'), lang=sql.Placeholder('lang'), names=sql.Placeholder('names'), search=sql.Placeholder('search'), limit=sql.Placeholder('limit'), ) self.env.cr.execute( query, { 'lang': self.env.lang, 'names': ['ir.ui.view,arch_db', 'ir.ui.view,name'], 'search': '%%%s%%' % escape_psql(search), 'limit': limit, }) ids = {row[0] for row in self.env.cr.fetchall()} ids.update(results.ids) domains = search_detail['base_domain'].copy() domains.append([('id', 'in', list(ids))]) domain = expression.AND(domains) model = self.sudo() if search_detail.get('requires_sudo') else self results = model.search(domain, limit=limit, order=search_detail.get('order', order)) count = max(count, len(results)) def filter_page(search, page, all_pages): # Search might have matched words in the xml tags and parameters therefore we make # sure the terms actually appear inside the text. text = '%s %s %s' % (page.name, page.url, text_from_html( page.arch)) pattern = '|'.join( [re.escape(search_term) for search_term in search.split()]) return re.findall('(%s)' % pattern, text, flags=re.I) if pattern else False if 'url' not in order: results = results._get_most_specific_pages() if search and with_description: results = results.filtered( lambda result: filter_page(search, result, results)) return results, count