<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2019 Compassion CH (http://www.compassion.ch)
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import logging

from odoo.tools.safe_eval import safe_eval
from ..mappings.mobile_app_tile_mapping import TileMapping
from odoo import api, models, fields

_logger = logging.getLogger(__name__)


class AppTile(models.Model):
    # internal field Odoo
    _name = 'mobile.app.tile'
    _description = 'Tile'
    _order = 'view_order'

    # Fields of class
    priority = fields.Selection([
        ('High', 'High'),
        ('Normal', 'Normal'),
        ('Low', 'Low')
    ], 'Priority', default='Normal', required=True)
    name = fields.Char(required=True)
    display_name = fields.Char(
        'Name', compute='_compute_display_name', store=True, readonly=True)
    view_order = fields.Integer('View order', required=True, default=6000)
    is_automatic_ordering = fields.Boolean("Automatic ordering", default=True)
    start_date = fields.Datetime()
    end_date = fields.Datetime()
    active = fields.Boolean(oldname='is_active', default=True)
    visibility = fields.Selection(
        [('public', 'Public'), ('private', 'Private'), ('both', 'Both')],
        required=True,
        help='Choose private if the sponsor must be logged in to see the tile'
    )
    model_id = fields.Many2one('ir.model', "Associated records")
    model = fields.Char(related='model_id.model')
    records_filter = fields.Char(
        'Records filter function',
        help='will use the filtered function on the associated records'
    )
    mode = fields.Selection([
        ('one', 'Unique tile'),
        ('many', 'One tile per record')
    ], help='This defines how to process related data')
    title = fields.Text(
        translate=True,
        help="Mako template enabled."
             "Use ctx['objects'] to get associated records."
    )
    body = fields.Text(
        translate=True,
        help="Mako template enabled."
             "Use ctx['objects'] to get associated records."
    )
    action_text = fields.Text(
        translate=True,
        help="Mako template enabled."
             "Use ctx['objects'] to get associated records."
    )
    subtype_id = fields.Many2one(
        'mobile.app.tile.subtype', 'Type', required=True)
    code = fields.Char(related='subtype_id.code')
    preview = fields.Binary(related='subtype_id.tile_preview', readonly=True)
    action_destination = fields.Selection(
        lambda s: s.env['mobile.app.tile.subtype'].select_action_destination(),
        required=True)

    @api.depends('subtype_id', 'subtype_id.code', 'name')
    def _compute_display_name(self):
        for tile in self:
            tile.display_name = u'[{}] {}'.format(tile.code, tile.name)

    @api.onchange('subtype_id')
    def _onchange_subtype(self):
        if self.subtype_id:
            self.body = self.subtype_id.default_body
            self.title = self.subtype_id.default_title
            self.action_text = self.subtype_id.default_action_text
            self.action_destination =\
                self.subtype_id.default_action_destination
            self.model_id = self.subtype_id.default_model_id
            self.records_filter = self.subtype_id.default_records_filter

    @api.model
    def init_tiles(self, domain=None):
        """
        Use this to set all tiles to their default templates defined
        in the tile subtypes. Called at module init.
        :param domain: optional search domain to restrict the reinitialization
        :return: True
        """
        if domain is None:
            domain = []
        # We iterate on all language to force the translations to be applied
        # to the texts.
        for lang in self.env['res.lang'].search([('active', '=', True)]):
            for tile in self.search(domain).with_context(lang=lang.code):
                tile._onchange_subtype()
        return True

    @api.multi
    def render_tile(self, tile_data=None):
        """
        This is the main function rendering the JSON for the mobile app.
        :param tile_data: this is a dict containing all data needed for
                          the tiles. The keys are the odoo models and values
                          are the record ids (list). Example :
                          {
                            'compassion.child': <compassion.child> recordset,
                            'res.partner': <res.partner> recordset,
                          }
        :return: Tiles for mobile app (list of dict)
        """
        tile_mapping = TileMapping(self.env)
        res = []
        if tile_data is None:
            tile_data = {}
        for tile in self:
            tile_json = tile_mapping.get_connect_data(tile)
            records = tile._get_records(tile_data)
            if records:
                # Convert text templates
                if tile.mode == 'one':
                    text_data = tile._render_single_tile(records)
                    if text_data:
                        tile_json.update(text_data)
                        res.append(tile_json)
                elif tile.mode == 'many':
                    for record in records:
                        text_data = tile._render_single_tile(record)
                        if text_data:
                            tile_json.update(text_data)
                            res.append(tile_json.copy())
            else:
                # Some tiles shouldn't rendered when no records are associated
                module = 'mobile_app_connector.%s'
                no_render = self.env.ref(module % 'tile_type_donation') + \
                    self.env.ref(module % 'tile_type_letter') +\
                    self.env.ref(module % 'tile_type_child')
                if tile.subtype_id.type_id not in no_render:
                    res.append(tile_json)
        return res<commit_msg>FIX community tile not shown if no associated records<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2019 Compassion CH (http://www.compassion.ch)
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import logging

from odoo.tools.safe_eval import safe_eval
from ..mappings.mobile_app_tile_mapping import TileMapping
from odoo import api, models, fields

_logger = logging.getLogger(__name__)


class AppTile(models.Model):
    # internal field Odoo
    _name = 'mobile.app.tile'
    _description = 'Tile'
    _order = 'view_order'

    # Fields of class
    priority = fields.Selection([
        ('High', 'High'),
        ('Normal', 'Normal'),
        ('Low', 'Low')
    ], 'Priority', default='Normal', required=True)
    name = fields.Char(required=True)
    display_name = fields.Char(
        'Name', compute='_compute_display_name', store=True, readonly=True)
    view_order = fields.Integer('View order', required=True, default=6000)
    is_automatic_ordering = fields.Boolean("Automatic ordering", default=True)
    start_date = fields.Datetime()
    end_date = fields.Datetime()
    active = fields.Boolean(oldname='is_active', default=True)
    visibility = fields.Selection(
        [('public', 'Public'), ('private', 'Private'), ('both', 'Both')],
        required=True,
        help='Choose private if the sponsor must be logged in to see the tile'
    )
    model_id = fields.Many2one('ir.model', "Associated records")
    model = fields.Char(related='model_id.model')
    records_filter = fields.Char(
        'Records filter function',
        help='will use the filtered function on the associated records'
    )
    mode = fields.Selection([
        ('one', 'Unique tile'),
        ('many', 'One tile per record')
    ], help='This defines how to process related data')
    title = fields.Text(
        translate=True,
        help="Mako template enabled."
             "Use ctx['objects'] to get associated records."
    )
    body = fields.Text(
        translate=True,
        help="Mako template enabled."
             "Use ctx['objects'] to get associated records."
    )
    action_text = fields.Text(
        translate=True,
        help="Mako template enabled."
             "Use ctx['objects'] to get associated records."
    )
    subtype_id = fields.Many2one(
        'mobile.app.tile.subtype', 'Type', required=True)
    code = fields.Char(related='subtype_id.code')
    preview = fields.Binary(related='subtype_id.tile_preview', readonly=True)
    action_destination = fields.Selection(
        lambda s: s.env['mobile.app.tile.subtype'].select_action_destination(),
        required=True)

    @api.depends('subtype_id', 'subtype_id.code', 'name')
    def _compute_display_name(self):
        for tile in self:
            tile.display_name = u'[{}] {}'.format(tile.code, tile.name)

    @api.onchange('subtype_id')
    def _onchange_subtype(self):
        if self.subtype_id:
            self.body = self.subtype_id.default_body
            self.title = self.subtype_id.default_title
            self.action_text = self.subtype_id.default_action_text
            self.action_destination =\
                self.subtype_id.default_action_destination
            self.model_id = self.subtype_id.default_model_id
            self.records_filter = self.subtype_id.default_records_filter

    @api.model
    def init_tiles(self, domain=None):
        """
        Use this to set all tiles to their default templates defined
        in the tile subtypes. Called at module init.
        :param domain: optional search domain to restrict the reinitialization
        :return: True
        """
        if domain is None:
            domain = []
        # We iterate on all language to force the translations to be applied
        # to the texts.
        for lang in self.env['res.lang'].search([('active', '=', True)]):
            for tile in self.search(domain).with_context(lang=lang.code):
                tile._onchange_subtype()
        return True

    @api.multi
    def render_tile(self, tile_data=None):
        """
        This is the main function rendering the JSON for the mobile app.
        :param tile_data: this is a dict containing all data needed for
                          the tiles. The keys are the odoo models and values
                          are the record ids (list). Example :
                          {
                            'compassion.child': <compassion.child> recordset,
                            'res.partner': <res.partner> recordset,
                          }
        :return: Tiles for mobile app (list of dict)
        """
        tile_mapping = TileMapping(self.env)
        res = []
        if tile_data is None:
            tile_data = {}
        for tile in self:
            tile_json = tile_mapping.get_connect_data(tile)
            records = tile._get_records(tile_data)
            if records:
                # Convert text templates
                if tile.mode == 'one':
                    text_data = tile._render_single_tile(records)
                    if text_data:
                        tile_json.update(text_data)
                        res.append(tile_json)
                elif tile.mode == 'many':
                    for record in records:
                        text_data = tile._render_single_tile(record)
                        if text_data:
                            tile_json.update(text_data)
                            res.append(tile_json.copy())
            else:
                # Some tiles shouldn't rendered when no records are associated
                module = 'mobile_app_connector.%s'
                no_render = self.env.ref(module % 'tile_type_donation') + \
                    self.env.ref(module % 'tile_type_letter') +\
                    self.env.ref(module % 'tile_type_child') +\
                    self.env.ref(module % 'tile_type_community')
                if tile.subtype_id.type_id not in no_render:
                    res.append(tile_json)
        return res
{'fraction_non_alphanumeric': 0.126260162601626, 'fraction_numerical': 0.0017886178861788618, 'mean_word_length': 2.1508709016393444, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1773692', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3266, 'n_words': 849}