2016-09-15 16:28:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-05-01 23:20:04 +02:00
|
|
|
# copyright 2017 fabien bourgeois <fabien@yaltik.com>
|
2016-09-15 16:28:27 +02:00
|
|
|
#
|
|
|
|
# this program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the gnu affero general public license as
|
|
|
|
# published by the free software foundation, either version 3 of the
|
|
|
|
# license, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# this program is distributed in the hope that it will be useful,
|
|
|
|
# but without any warranty; without even the implied warranty of
|
|
|
|
# merchantability or fitness for a particular purpose. see the
|
|
|
|
# gnu affero general public license for more details.
|
|
|
|
#
|
|
|
|
# you should have received a copy of the gnu affero general public license
|
|
|
|
# along with this program. if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-05-01 23:20:04 +02:00
|
|
|
""" GOLEM Activity Registration """
|
2016-09-15 16:28:27 +02:00
|
|
|
|
2017-05-01 23:20:04 +02:00
|
|
|
from odoo import models, fields, api, _
|
2016-09-15 16:28:27 +02:00
|
|
|
|
|
|
|
class GolemMember(models.Model):
|
2017-05-01 23:20:04 +02:00
|
|
|
""" GOLEM Member adaptations """
|
2016-09-15 16:28:27 +02:00
|
|
|
_inherit = 'golem.member'
|
|
|
|
|
2016-10-10 17:51:42 +02:00
|
|
|
activity_registration_ids = fields.One2many('golem.activity.registration',
|
2017-05-01 23:20:04 +02:00
|
|
|
'member_id', 'Activities',
|
|
|
|
index=True)
|
2016-10-09 11:01:09 +02:00
|
|
|
|
2016-09-15 16:28:27 +02:00
|
|
|
|
2016-10-10 17:51:42 +02:00
|
|
|
class GolemActivity(models.Model):
|
2017-05-01 23:20:04 +02:00
|
|
|
""" GOLEM Activity adaptations """
|
2016-10-10 17:51:42 +02:00
|
|
|
_inherit = 'golem.activity'
|
|
|
|
_sql_constraints = [('golem_activity_places_signed',
|
2016-09-21 18:22:01 +02:00
|
|
|
'CHECK (places >= 0)',
|
|
|
|
_('Number of places cannot be negative.'))]
|
|
|
|
|
2016-10-10 17:51:42 +02:00
|
|
|
activity_registration_ids = fields.One2many('golem.activity.registration',
|
2017-05-01 23:20:04 +02:00
|
|
|
'activity_id', 'Members',
|
|
|
|
index=True)
|
2016-10-09 12:43:23 +02:00
|
|
|
places_used = fields.Integer('Places used', compute='_compute_places_used',
|
|
|
|
store=True)
|
2016-09-21 18:22:01 +02:00
|
|
|
|
2017-05-01 23:20:04 +02:00
|
|
|
@api.multi
|
2016-10-10 17:51:42 +02:00
|
|
|
@api.depends('activity_registration_ids')
|
2016-09-21 18:22:01 +02:00
|
|
|
def _compute_places_used(self):
|
2017-05-01 23:20:04 +02:00
|
|
|
""" Computes used places """
|
|
|
|
for activity in self:
|
|
|
|
activity.places_used = len(activity.activity_registration_ids)
|
2016-09-21 18:22:01 +02:00
|
|
|
|
|
|
|
places = fields.Integer('Places', default=20)
|
|
|
|
places_remain = fields.Integer('Remaining places', store=True,
|
|
|
|
compute='_compute_places_remain')
|
|
|
|
|
2017-05-01 23:20:04 +02:00
|
|
|
@api.multi
|
2016-10-09 12:43:23 +02:00
|
|
|
@api.depends('places', 'places_used')
|
2016-09-21 18:22:01 +02:00
|
|
|
def _compute_places_remain(self):
|
2017-05-01 23:20:04 +02:00
|
|
|
""" Computes remaining places """
|
|
|
|
for activity in self:
|
|
|
|
activity.places_remain = activity.places - activity.places_used
|
2016-09-21 18:22:01 +02:00
|
|
|
|
|
|
|
@api.constrains('places_remain')
|
|
|
|
def _check_remaining_places(self):
|
|
|
|
""" Forbid inscription when there is no more place """
|
2017-05-01 23:20:04 +02:00
|
|
|
for activity in self:
|
|
|
|
if activity.places_remain < 0:
|
2016-09-21 18:22:01 +02:00
|
|
|
emsg = _('Sorry, there is no more place !')
|
|
|
|
raise models.ValidationError(emsg)
|
|
|
|
|
|
|
|
|
2016-10-10 17:51:42 +02:00
|
|
|
class GolemActivityRegistration(models.Model):
|
2017-05-01 23:20:04 +02:00
|
|
|
""" GOLEM Activity Registration """
|
2016-10-10 17:51:42 +02:00
|
|
|
_name = 'golem.activity.registration'
|
|
|
|
_description = 'GOLEM Activity Registration'
|
2016-09-15 16:28:27 +02:00
|
|
|
|
|
|
|
member_id = fields.Many2one('golem.member', string='Member', required=True,
|
2017-05-01 23:20:04 +02:00
|
|
|
ondelete='cascade', index=True)
|
|
|
|
activity_id = fields.Many2one('golem.activity', required=True, index=True,
|
2016-10-10 17:51:42 +02:00
|
|
|
string='Activity', ondelete='cascade')
|
2016-09-15 16:28:27 +02:00
|
|
|
season_id = fields.Many2one(string='Season',
|
2016-10-10 17:51:42 +02:00
|
|
|
related='activity_id.season_id')
|
2016-09-22 10:15:02 +02:00
|
|
|
is_current = fields.Boolean('Current season?',
|
2016-10-10 17:51:42 +02:00
|
|
|
related='activity_id.is_current')
|
2016-09-22 11:08:03 +02:00
|
|
|
|
2016-09-22 13:53:33 +02:00
|
|
|
_sql_constraints = [
|
2016-10-10 17:51:42 +02:00
|
|
|
('registration_uniq', 'UNIQUE (member_id, activity_id)',
|
|
|
|
_('This member has already been registered for this activity.'))]
|
2016-09-22 11:08:03 +02:00
|
|
|
|
2016-10-10 17:51:42 +02:00
|
|
|
@api.constrains('member_id', 'activity_id')
|
2016-09-22 11:08:03 +02:00
|
|
|
def _check_season_reliability(self):
|
|
|
|
""" Forbid registration when member season if not coherent with
|
2016-10-10 17:51:42 +02:00
|
|
|
activity season or are duplicates """
|
2017-05-01 23:20:04 +02:00
|
|
|
for reg in self:
|
|
|
|
if reg.activity_id.season_id not in reg.member_id.season_ids:
|
2016-09-22 11:08:03 +02:00
|
|
|
emsg = _('Subscription can not be executed : the targeted '
|
2016-10-10 17:51:42 +02:00
|
|
|
'member is not on the same season as the activity.')
|
2016-09-22 11:08:03 +02:00
|
|
|
raise models.ValidationError(emsg)
|