product_template.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. from docutils.nodes import entry
  3. from openerp.http import request as r
  4. _MODEL = 'product.template'
  5. def get_products(image_type='small', location_ids=None):
  6. domain = [
  7. ('sale_ok', '=', True),
  8. ('list_price', '>', 0),
  9. ('active', '=', True)
  10. ]
  11. in_locations = {}
  12. if location_ids:
  13. for q in r.env['stock.quant'].search([('location_id', 'in', location_ids)], order='product_id'):
  14. entry = in_locations.get(q.product_id.id, None)
  15. if entry:
  16. if q.location_id.id not in map(lambda x: x.get('id'), entry):
  17. entry.append({
  18. 'id': q.location_id.id,
  19. 'name': q.location_id.display_name
  20. })
  21. continue
  22. in_locations[q.product_id.id] = [
  23. {
  24. 'id': q.location_id.id,
  25. 'name': q.location_id.display_name
  26. }
  27. ]
  28. return [
  29. {
  30. 'id': p.id,
  31. 'name': p.display_name,
  32. 'ean13': p.ean13,
  33. 'defaultCode': p.default_code,
  34. 'image': p.image_small if image_type == 'small' else p.image_medium,
  35. 'listPrice': p.list_price,
  36. 'variantCount': p.product_variant_count,
  37. 'quantity': 1,
  38. 'price': p.list_price,
  39. 'minimumPrice': p.minimum_price,
  40. 'maximumPrice': p.maximum_price,
  41. 'discount': 0,
  42. 'variants': [{
  43. 'id': v.id,
  44. 'name': v.display_name,
  45. 'ean13': v.ean13,
  46. 'defaultCode': p.default_code,
  47. 'image': v.image_small if image_type == 'small' else v.image_medium,
  48. 'listPrice': v.list_price,
  49. 'quantity': 1,
  50. 'price': v.list_price,
  51. 'minimumPrice': p.minimum_price,
  52. 'maximumPrice': p.maximum_price,
  53. 'discount': 0,
  54. 'locations': in_locations.get(v.id, [])
  55. } for v in p.product_variant_ids]
  56. } for p in r.env[_MODEL].search(domain)
  57. ]
  58. def create_product(**kw):
  59. product = r.env[_MODEL].create({
  60. 'name': kw.get('name'),
  61. 'listPrice': float(kw.get('price')),
  62. 'ean13': kw.get('ean13')
  63. })
  64. return {
  65. 'id': product.id,
  66. 'name': product.display_name,
  67. 'ean13': product.ean13,
  68. 'image': product.image_small,
  69. 'listPrice': product.list_price,
  70. 'variantCount': product.product_variant_count,
  71. 'quantity': 1,
  72. 'price': product.list_price,
  73. 'discount': 0,
  74. 'variants': [{
  75. 'id': variant.id,
  76. 'name': variant.display_name,
  77. 'ean13': variant.ean13,
  78. 'image': variant.image_small,
  79. 'listPrice': variant.list_price,
  80. 'quantity': 1,
  81. 'price': variant.list_price,
  82. 'discount': 0,
  83. } for variant in product.product_variant_ids if variant.active]
  84. }