event_event.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. from odoo import Command
  5. class Event(models.Model):
  6. _inherit = 'event.event'
  7. event_booth_ids = fields.One2many(
  8. 'event.booth', 'event_id', string='Booths', copy=True,
  9. compute='_compute_event_booth_ids', readonly=False, store=True)
  10. event_booth_count = fields.Integer(
  11. string='Total Booths',
  12. compute='_compute_event_booth_count')
  13. event_booth_count_available = fields.Integer(
  14. string='Available Booths',
  15. compute='_compute_event_booth_count')
  16. event_booth_category_ids = fields.Many2many(
  17. 'event.booth.category', compute='_compute_event_booth_category_ids')
  18. event_booth_category_available_ids = fields.Many2many(
  19. 'event.booth.category', compute='_compute_event_booth_category_available_ids',
  20. help='Booth Category for which booths are still available. Used in frontend')
  21. @api.depends('event_type_id')
  22. def _compute_event_booth_ids(self):
  23. """ Update event configuration from its event type. Depends are set only
  24. on event_type_id itself, not its sub fields. Purpose is to emulate an
  25. onchange: if event type is changed, update event configuration. Changing
  26. event type content itself should not trigger this method.
  27. When synchronizing booths:
  28. * lines that are available are removed;
  29. * template lines are added;
  30. """
  31. for event in self:
  32. if not event.event_type_id and not event.event_booth_ids:
  33. event.event_booth_ids = False
  34. continue
  35. # booths to keep: those that are not available
  36. booths_to_remove = event.event_booth_ids.filtered(lambda booth: booth.is_available)
  37. command = [Command.unlink(booth.id) for booth in booths_to_remove]
  38. if event.event_type_id.event_type_booth_ids:
  39. command += [
  40. Command.create({
  41. attribute_name: line[attribute_name] if not isinstance(line[attribute_name], models.BaseModel) else line[attribute_name].id
  42. for attribute_name in self.env['event.type.booth']._get_event_booth_fields_whitelist()
  43. }) for line in event.event_type_id.event_type_booth_ids
  44. ]
  45. event.event_booth_ids = command
  46. def _get_booth_stat_count(self):
  47. elements = self.env['event.booth'].sudo()._read_group(
  48. [('event_id', 'in', self.ids)],
  49. ['event_id', 'state'], ['event_id', 'state'], lazy=False
  50. )
  51. elements_total_count = dict()
  52. elements_available_count = dict()
  53. for element in elements:
  54. event_id = element['event_id'][0]
  55. if element['state'] == 'available':
  56. elements_available_count[event_id] = element['__count']
  57. elements_total_count.setdefault(event_id, 0)
  58. elements_total_count[event_id] += element['__count']
  59. return elements_available_count, elements_total_count
  60. @api.depends('event_booth_ids', 'event_booth_ids.state')
  61. def _compute_event_booth_count(self):
  62. if self.ids and all(bool(event.id) for event in self): # no new/onchange mode -> optimized
  63. booths_available_count, booths_total_count = self._get_booth_stat_count()
  64. for event in self:
  65. event.event_booth_count_available = booths_available_count.get(event.id, 0)
  66. event.event_booth_count = booths_total_count.get(event.id, 0)
  67. else:
  68. for event in self:
  69. event.event_booth_count = len(event.event_booth_ids)
  70. event.event_booth_count_available = len(event.event_booth_ids.filtered(lambda booth: booth.is_available))
  71. @api.depends('event_booth_ids.booth_category_id')
  72. def _compute_event_booth_category_ids(self):
  73. for event in self:
  74. event.event_booth_category_ids = event.event_booth_ids.mapped('booth_category_id')
  75. @api.depends('event_booth_ids.is_available')
  76. def _compute_event_booth_category_available_ids(self):
  77. for event in self:
  78. event.event_booth_category_available_ids = event.event_booth_ids.filtered(lambda booth: booth.is_available).mapped('booth_category_id')