<commit_before># -*- coding: utf-8 -*-
# Copyright © 2016 <NAME> - AvanzOSC
# Copyright © 2017 <NAME> - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from openerp import models, fields, api, exceptions, _
from .._common import _convert_to_local_date, _convert_time_to_float
from dateutil.relativedelta import relativedelta

str2datetime = fields.Datetime.from_string
str2date = fields.Date.from_string
datetime2str = fields.Datetime.to_string
date2str = fields.Date.to_string


class EventEvent(models.Model):
    _inherit = 'event.event'

    claim_ids = fields.One2many(
        comodel_name='crm.claim', inverse_name='event_id', string='Claims')
    claim_count = fields.Integer(
        string='# Claims', compute='_compute_claim_count')

    @api.depends('claim_ids')
    def _compute_claim_count(self):
        for event in self:
            event.claim_count = len(event.claim_ids)

    @api.onchange('date_begin')
    def onchange_date_begin(self):
        self.ensure_one()
        res = {}
        if self.date_begin:
            tracks = self.track_ids.filtered(
                lambda x: x.date and x.date < self.date_begin)
            if tracks:
                track = min(tracks, key=lambda x: x.date)
                res = {'warning': {
                       'title': _('Error in date begin'),
                       'message':
                       (_('Session %s with lower date') % track.name)}}
                self.date_begin = track.date
        return res

    @api.onchange('date_end')
    def onchange_date_end(self):
        self.ensure_one()
        res = {}
        if self.date_end:
            tracks = self.track_ids.filtered(
                lambda x: x.date and x.date > self.date_end)
            if tracks:
                track = max(tracks, key=lambda x: x.date)
                res = {'warning': {
                       'title': _('Error in date end'),
                       'message':
                       (_('Session %s with greater date') % track.name)}}
                self.date_end = track.date
        return res


class EventTrack(models.Model):
    _inherit = 'event.track'

    @api.depends('date', 'duration')
    def _compute_estimated_date_end(self):
        for track in self.filtered(lambda t: t.date and t.duration):
            new_date = str2datetime(track.date) +\
                relativedelta(hours=track.duration)
            track.estimated_date_end = new_date

    @api.depends('event_id', 'event_id.registration_ids')
    def _compute_allowed_partner_ids(self):
        for track in self:
            partners = track.mapped('event_id.registration_ids.partner_id')
            track.allowed_partner_ids = [(6, 0, partners.ids)]

    @api.depends('presences', 'presences.real_duration')
    def _compute_real_duration(self):
        for track in self:
            track.real_duration = sum(track.mapped('presences.real_duration'))

    @api.depends('date', 'real_duration')
    def _compute_real_date_end(self):
        for track in self.filtered(lambda t: t.date and t.real_duration):
            new_date = str2datetime(track.date) +\
                relativedelta(hours=track.real_duration)
            track.real_date_end = new_date

    @api.depends('date')
    def _compute_session_date(self):
        for track in self.filtered('date'):
            from_date = _convert_to_local_date(track.date, self.env.user.tz)
            track.session_date = from_date.date()

    @api.depends('estimated_date_end')
    def _compute_session_end_date(self):
        for track in self.filtered('estimated_date_end'):
            from_date = _convert_to_local_date(
                track.estimated_date_end, self.env.user.tz)
            track.session_end_date = from_date.date()

    @api.depends('presences')
    def _compute_num_presences(self):
        for track in self:
            track.lit_presences = _('Presences: ') + str(len(track.presences))

    estimated_date_end = fields.Datetime(
        string='Estimated date end', compute='_compute_estimated_date_end',
        store=True)
    allowed_partner_ids = fields.Many2many(
        comodel_name="res.partner", relation="rel_partner_event_track",
        column1="event_track_id", column2="partner_id", string="Partners",
        copy=False, compute='_compute_allowed_partner_ids', store=True)
    presences = fields.One2many(
        comodel_name='event.track.presence', inverse_name='session',
        string='Presences')
    duration = fields.Float(string='Estimated duration', digits=(12, 4))
    real_duration = fields.Float(
        compute='_compute_real_duration', string='Real duration', store=True)
    real_date_end = fields.Datetime(
        string='Real date end', compute='_compute_real_date_end',
        store=True)
    session_date = fields.Date(
        string='Session date', compute='_compute_session_date', store=True)
    session_end_date = fields.Date(
        string='Session end date', compute='_compute_session_end_date',
        store=True)
    lit_presences = fields.Char(
        string='Num. presences', compute='_compute_num_presences',
        store=True)
    claim_ids = fields.One2many(
        comodel_name='crm.claim', inverse_name='session_id', string='Claims')
    claim_count = fields.Integer(
        string='# Claims', compute='_compute_claim_count')

    @api.depends('claim_ids')
    def _compute_claim_count(self):
        for track in self:
            track.claim_count = len(track.claim_ids)

    @api.constrains('date')
    def _check_session_date(self):
        tz = self.env.user.tz
        date_begin = _convert_to_local_date(self.event_id.date_begin, tz)
        session_date = str2datetime(self.session_date)\
            if self.session_date else False
        if session_date and session_date.date() < date_begin.date():
            raise exceptions.Warning(
                _('Session %s with date lower than the event start date')
                % self.name)
        date_end = _convert_to_local_date(self.event_id.date_end, tz)
        if session_date and session_date.date() > date_end.date():
            raise exceptions.Warning(
                _('Session %s with date greater than the event start date')
                % self.name)


class EventTrackPresence(models.Model):
    _name = 'event.track.presence'
    _description = 'Session assistants'

    @api.depends('session', 'session.allowed_partner_ids')
    def _compute_allowed_partner_ids(self):
        for presence in self:
            presence.allowed_partner_ids = (
                [(6, 0, presence.session.allowed_partner_ids.ids)])
            if self.env['marketing.config.settings']._get_parameter(
               'show.all.customers.in.presences'):
                cond = [('customer', '=', True)]
                customers = self.env['res.partner'].search(cond)
                for customer in customers:
                    presence.allowed_partner_ids = [(4, customer.id)]

    @api.depends('session_date', 'real_duration')
    def _compute_real_date_end(self):
        for presence in self.filtered('real_duration'):
            start_date = str2datetime(presence.session_date)
            end_date = start_date + relativedelta(
                hours=presence.real_duration)<commit_msg>[IMP] event_track_assistant: Remove condition to search partner allowed in presences.<commit_after># -*- coding: utf-8 -*-
# Copyright © 2016 <NAME> - AvanzOSC
# Copyright © 2017 <NAME> - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from openerp import models, fields, api, exceptions, _
from .._common import _convert_to_local_date, _convert_time_to_float
from dateutil.relativedelta import relativedelta

str2datetime = fields.Datetime.from_string
str2date = fields.Date.from_string
datetime2str = fields.Datetime.to_string
date2str = fields.Date.to_string


class EventEvent(models.Model):
    _inherit = 'event.event'

    claim_ids = fields.One2many(
        comodel_name='crm.claim', inverse_name='event_id', string='Claims')
    claim_count = fields.Integer(
        string='# Claims', compute='_compute_claim_count')

    @api.depends('claim_ids')
    def _compute_claim_count(self):
        for event in self:
            event.claim_count = len(event.claim_ids)

    @api.onchange('date_begin')
    def onchange_date_begin(self):
        self.ensure_one()
        res = {}
        if self.date_begin:
            tracks = self.track_ids.filtered(
                lambda x: x.date and x.date < self.date_begin)
            if tracks:
                track = min(tracks, key=lambda x: x.date)
                res = {'warning': {
                       'title': _('Error in date begin'),
                       'message':
                       (_('Session %s with lower date') % track.name)}}
                self.date_begin = track.date
        return res

    @api.onchange('date_end')
    def onchange_date_end(self):
        self.ensure_one()
        res = {}
        if self.date_end:
            tracks = self.track_ids.filtered(
                lambda x: x.date and x.date > self.date_end)
            if tracks:
                track = max(tracks, key=lambda x: x.date)
                res = {'warning': {
                       'title': _('Error in date end'),
                       'message':
                       (_('Session %s with greater date') % track.name)}}
                self.date_end = track.date
        return res


class EventTrack(models.Model):
    _inherit = 'event.track'

    @api.depends('date', 'duration')
    def _compute_estimated_date_end(self):
        for track in self.filtered(lambda t: t.date and t.duration):
            new_date = str2datetime(track.date) +\
                relativedelta(hours=track.duration)
            track.estimated_date_end = new_date

    @api.depends('event_id', 'event_id.registration_ids')
    def _compute_allowed_partner_ids(self):
        for track in self:
            partners = track.mapped('event_id.registration_ids.partner_id')
            track.allowed_partner_ids = [(6, 0, partners.ids)]

    @api.depends('presences', 'presences.real_duration')
    def _compute_real_duration(self):
        for track in self:
            track.real_duration = sum(track.mapped('presences.real_duration'))

    @api.depends('date', 'real_duration')
    def _compute_real_date_end(self):
        for track in self.filtered(lambda t: t.date and t.real_duration):
            new_date = str2datetime(track.date) +\
                relativedelta(hours=track.real_duration)
            track.real_date_end = new_date

    @api.depends('date')
    def _compute_session_date(self):
        for track in self.filtered('date'):
            from_date = _convert_to_local_date(track.date, self.env.user.tz)
            track.session_date = from_date.date()

    @api.depends('estimated_date_end')
    def _compute_session_end_date(self):
        for track in self.filtered('estimated_date_end'):
            from_date = _convert_to_local_date(
                track.estimated_date_end, self.env.user.tz)
            track.session_end_date = from_date.date()

    @api.depends('presences')
    def _compute_num_presences(self):
        for track in self:
            track.lit_presences = _('Presences: ') + str(len(track.presences))

    estimated_date_end = fields.Datetime(
        string='Estimated date end', compute='_compute_estimated_date_end',
        store=True)
    allowed_partner_ids = fields.Many2many(
        comodel_name="res.partner", relation="rel_partner_event_track",
        column1="event_track_id", column2="partner_id", string="Partners",
        copy=False, compute='_compute_allowed_partner_ids', store=True)
    presences = fields.One2many(
        comodel_name='event.track.presence', inverse_name='session',
        string='Presences')
    duration = fields.Float(string='Estimated duration', digits=(12, 4))
    real_duration = fields.Float(
        compute='_compute_real_duration', string='Real duration', store=True)
    real_date_end = fields.Datetime(
        string='Real date end', compute='_compute_real_date_end',
        store=True)
    session_date = fields.Date(
        string='Session date', compute='_compute_session_date', store=True)
    session_end_date = fields.Date(
        string='Session end date', compute='_compute_session_end_date',
        store=True)
    lit_presences = fields.Char(
        string='Num. presences', compute='_compute_num_presences',
        store=True)
    claim_ids = fields.One2many(
        comodel_name='crm.claim', inverse_name='session_id', string='Claims')
    claim_count = fields.Integer(
        string='# Claims', compute='_compute_claim_count')

    @api.depends('claim_ids')
    def _compute_claim_count(self):
        for track in self:
            track.claim_count = len(track.claim_ids)

    @api.constrains('date')
    def _check_session_date(self):
        tz = self.env.user.tz
        date_begin = _convert_to_local_date(self.event_id.date_begin, tz)
        session_date = str2datetime(self.session_date)\
            if self.session_date else False
        if session_date and session_date.date() < date_begin.date():
            raise exceptions.Warning(
                _('Session %s with date lower than the event start date')
                % self.name)
        date_end = _convert_to_local_date(self.event_id.date_end, tz)
        if session_date and session_date.date() > date_end.date():
            raise exceptions.Warning(
                _('Session %s with date greater than the event start date')
                % self.name)


class EventTrackPresence(models.Model):
    _name = 'event.track.presence'
    _description = 'Session assistants'

    @api.depends('session', 'session.allowed_partner_ids')
    def _compute_allowed_partner_ids(self):
        for presence in self:
            presence.allowed_partner_ids = (
                [(6, 0, presence.session.allowed_partner_ids.ids)])
            if self.env['marketing.config.settings']._get_parameter(
               'show.all.customers.in.presences'):
                customers = self.env['res.partner'].search([])
                for customer in customers:
                    presence.allowed_partner_ids = [(4, customer.id)]

    @api.depends('session_date', 'real_duration')
    def _compute_real_date_end(self):
        for presence in self.filtered('real_duration'):
            start_date = str2datetime(presence.session_date)
            end_date = start_date + relativedelta(
                hours=presence.real_duration)
{'fraction_non_alphanumeric': 0.1103713363992038, 'fraction_numerical': 0.004667444574095682, 'mean_word_length': 2.6082218920257554, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1770701', 'n_tokens_mistral': 4474, 'n_tokens_neox': 4308, 'n_words': 845}