resource_calendar.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import random
  4. from odoo import models
  5. from odoo.tools import populate
  6. class ResourceCalendar(models.Model):
  7. _inherit = "resource.calendar"
  8. _populate_dependencies = ["res.company"] # multi-company setup
  9. _populate_sizes = {
  10. "small": 10, # 1-2 per company
  11. "medium": 30, # 3 per company
  12. "large": 250 # 5 per company
  13. }
  14. def _populate_factories(self):
  15. company_ids = self.env.registry.populated_models["res.company"]
  16. return [
  17. ("company_id", populate.iterate(company_ids)),
  18. ("name", populate.iterate(["A little {counter}", "A lot {counter}"])),
  19. ]
  20. def _populate(self, size):
  21. records = super()._populate(size)
  22. # Randomly remove 1 half day from schedule
  23. a_lot = records.filtered_domain([("name", "like", "A lot")])
  24. for record in a_lot:
  25. att_id = record.attendance_ids[random.randint(0, 9)]
  26. record.write({
  27. 'attendance_ids': [(3, att_id.id)],
  28. })
  29. # Randomly remove 3 to 5 half days from schedule
  30. a_little = records - a_lot
  31. for record in a_little:
  32. to_pop = random.sample(range(10), random.randint(3, 5))
  33. record.write({
  34. 'attendance_ids': [(3, record.attendance_ids[idx].id) for idx in to_pop],
  35. })
  36. return records