product_template.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request
  3. _MODEL = 'product.template'
  4. '''
  5. '''
  6. def get_products(image_type='small'):
  7. domain = [
  8. ('sale_ok', '=', True),
  9. ('list_price', '>', 0),
  10. ('active', '=', True)
  11. ]
  12. return [
  13. {
  14. 'id': product.id,
  15. 'name': product.display_name,
  16. 'ean13': product.ean13,
  17. 'defaultCode': product.default_code,
  18. 'image': product.image_small if image_type == 'small' else product.image_medium,
  19. 'listPrice': product.list_price,
  20. 'variantCount': product.product_variant_count,
  21. 'quantity': 1,
  22. 'price': product.list_price,
  23. 'minimumPrice': product.minimum_price,
  24. 'maximumPrice': product.maximum_price,
  25. 'discount': 0,
  26. 'variants': [{
  27. 'id': variant.id,
  28. 'name': variant.display_name,
  29. 'ean13': variant.ean13,
  30. 'defaultCode': product.default_code,
  31. 'image': variant.image_small if image_type == 'small' else variant.image_medium,
  32. 'listPrice': variant.list_price,
  33. 'quantity': 1,
  34. 'price': variant.list_price,
  35. 'minimumPrice': product.minimum_price,
  36. 'maximumPrice': product.maximum_price,
  37. 'discount': 0,
  38. } for variant in product.product_variant_ids if variant.active]
  39. } for product in request.env[_MODEL].search(domain)
  40. ]
  41. '''
  42. Create product and return
  43. '''
  44. def create_product(**kw):
  45. product = request.env[_MODEL].create({
  46. 'name': kw.get('name'),
  47. 'listPrice': float(kw.get('price')),
  48. 'ean13': kw.get('ean13')
  49. })
  50. return {
  51. 'id': product.id,
  52. 'name': product.display_name,
  53. 'ean13': product.ean13,
  54. 'image': product.image_small,
  55. 'listPrice': product.list_price,
  56. 'variantCount': product.product_variant_count,
  57. 'quantity': 1,
  58. 'price': product.list_price,
  59. 'discount': 0,
  60. 'variants': [{
  61. 'id': variant.id,
  62. 'name': variant.display_name,
  63. 'ean13': variant.ean13,
  64. 'image': variant.image_small,
  65. 'listPrice': variant.list_price,
  66. 'quantity': 1,
  67. 'price': variant.list_price,
  68. 'discount': 0,
  69. } for variant in product.product_variant_ids if variant.active]
  70. }