models.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. from odoo import models, fields, api
  3. MILKY_WAY_REGIONS = ['P3X', 'P4X', 'P2X', 'P5C']
  4. PEGASUS_REGIONS = ['M4R', 'P3Y', 'M6R']
  5. class Stargate(models.Model):
  6. _name = 'test_http.stargate'
  7. _description = 'Stargate'
  8. name = fields.Char(required=True, store=True, compute='_compute_name', readonly=False)
  9. address = fields.Char(required=True)
  10. sgc_designation = fields.Char(store=True, compute='_compute_sgc_designation')
  11. galaxy_id = fields.Many2one('test_http.galaxy', required=True)
  12. has_galaxy_crystal = fields.Boolean(store=True, compute='_compute_has_galaxy_crystal', readonly=False)
  13. glyph_attach = fields.Image(attachment=True)
  14. glyph_inline = fields.Image(attachment=False)
  15. _sql_constraints = [
  16. ('address_length', 'CHECK(LENGTH(address) = 6)', "Local addresses have 6 glyphs"),
  17. ]
  18. @api.depends('galaxy_id')
  19. def _compute_has_galaxy_crystal(self):
  20. milky_way = self.env.ref('test_http.milky_way')
  21. for gate in self:
  22. gate.has_galaxy_crystal = gate.galaxy_id == milky_way
  23. @api.depends('sgc_designation')
  24. def _compute_name(self):
  25. for gate in self:
  26. if not gate.name:
  27. gate.name = gate.sgc_designation
  28. @api.depends('address')
  29. def _compute_sgc_designation(self):
  30. """ Forge a sgc designation that looks like a real one. """
  31. for gate in self:
  32. if gate.galaxy_id.name not in ('Milky Way', 'Pegasus'):
  33. gate.sgc_designation = False
  34. continue
  35. region_part = (
  36. PEGASUS_REGIONS[gate.id % len(PEGASUS_REGIONS)]
  37. if gate.galaxy_id.name == 'Pegasus'
  38. else MILKY_WAY_REGIONS[gate.id % len(MILKY_WAY_REGIONS)]
  39. )
  40. local_part = str(int.from_bytes(gate.address.encode(), 'big'))[:3]
  41. gate.sgc_designation = f'{region_part}-{local_part}'
  42. class Galaxy(models.Model):
  43. _name = 'test_http.galaxy'
  44. _description = 'Galaxy'
  45. name = fields.Char(required=True, help='The galaxy common name.')
  46. @api.model
  47. def render(self, galaxy_id):
  48. return self.env['ir.qweb']._render('test_http.tmpl_galaxy', {
  49. 'galaxy': self.browse([galaxy_id])
  50. })