You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.2 KiB

  1. Actions must be configured with the following data in the context:
  2. * model: Model where we can find the method (required)
  3. * method: Method to execute (required)
  4. * res_id: Id as base (optional)
  5. The method must return an action. Installing this module with demo data will
  6. install a demo application that allows the system administrator to find a
  7. partner by the external reference encoded in a barcode.
  8. Go to *Settings / Find partners* and scan a barcode that contains the
  9. internal reference of an existing partner. As soon as you read the barcode
  10. the system will redirect you to that partner's form view.
  11. Technical implementation of this example:
  12. Action::
  13. <act_window id="res_partner_find"
  14. name="Find Partner"
  15. res_model="barcode.action"
  16. view_mode="form"
  17. view_type="form"
  18. context="{'default_model': 'res.partner', 'default_method': 'find_res_partner_by_ref_using_barcode'}"
  19. target="new"/>
  20. <menuitem id="menu_orders_customers" name="Find partners"
  21. action="res_partner_find"
  22. parent="base.menu_administration"/>
  23. Python code::
  24. import json
  25. from odoo import api, models, _
  26. from odoo.tools.safe_eval import safe_eval
  27. class ResPartner(models.Model):
  28. _inherit = 'res.partner'
  29. @api.multi
  30. def find_res_partner_by_ref_using_barcode(self, barcode):
  31. partner = self.search([('ref', '=', barcode)], limit=1)
  32. if not partner:
  33. action = self.env.ref('res_partner_find')
  34. result = action.read()[0]
  35. context = safe_eval(result['context'])
  36. context.update({
  37. 'default_state': 'warning',
  38. 'default_status': _('Partner with Internal Reference '
  39. '%s cannot be found') % barcode
  40. })
  41. result['context'] = json.dumps(context)
  42. return result
  43. action = self.env.ref('base.action_partner_form')
  44. result = action.read()[0]
  45. res = self.env.ref('base.view_partner_form', False)
  46. result['views'] = [(res and res.id or False, 'form')]
  47. result['res_id'] = partner.id
  48. return result