-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathcatalog.py
886 lines (707 loc) · 27.1 KB
/
catalog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# -*- coding: UTF-8 -*-
'''
magento.catalog
Product Catalog API for magento
:license: BSD, see LICENSE for more details
'''
import warnings
from magento.api import API
class Category(API):
"""
Product Category API
"""
__slots__ = ()
def currentStore(self, store_view=None):
"""
Set/Get current store view
:param store_view: Store view ID or Code
:return: int
"""
args = [store_view] if store_view else []
return int(self.call('catalog_category.currentStore', args))
def tree(self, parent_id=None, store_view=None):
"""
Retrieve hierarchical tree of categories.
:param parent_id: Integer ID of parent category (optional)
:param store_view: Store View (optional)
:return: dictionary of values
"""
return self.call('catalog_category.tree', [parent_id, store_view])
def level(self, website=None, store_view=None, parent_category=None):
"""
Retrieve one level of categories by website/store view/parent category
:param website: Website code or ID
:param store_view: storeview code or ID
:param parent_category: Parent Category ID
:return: Dictionary
"""
return self.call(
'catalog_category.level', [website, store_view, parent_category]
)
def info(self, category_id, store_view=None, attributes=None):
"""
Retrieve Category details
:param category_id: ID of category to retrieve
:param store_view: Store view ID or code
:param attributes: Return the fields specified
:return: Dictionary of data
"""
return self.call(
'catalog_category.info', [category_id, store_view, attributes]
)
def create(self, parent_id, data, store_view=None):
"""
Create new category and return its ID
:param parent_id: ID of parent
:param data: Data for category
:param store_view: Store view ID or Code
:return: Integer ID
"""
return int(self.call(
'catalog_category.create', [parent_id, data, store_view])
)
def update(self, category_id, data, store_view=None):
"""
Update Category
:param category_id: ID of category
:param data: Category Data
:param store_view: Store view ID or code
:return: Boolean
"""
return bool(
self.call(
'catalog_category.update', [category_id, data, store_view]
)
)
def move(self, category_id, parent_id, after_id=None):
"""
Move category in tree
:param category_id: ID of category to move
:param parent_id: New parent of the category
:param after_id: Category ID after what position it will be moved
:return: Boolean
"""
return bool(self.call(
'catalog_category.move', [category_id, parent_id, after_id])
)
def delete(self, category_id):
"""
Delete category
:param category_id: ID of category
:return: Boolean
"""
return bool(self.call('catalog_category.delete', [category_id]))
def assignedproducts(self, category_id, store):
"""
Retrieve list of assigned products
:param category_id: Category ID
:param store: Store ID or Code
:return: Dictionary
"""
return self.call(
'catalog_category.assignedProducts', [category_id, store]
)
#: A proxy for :meth:`assignedproducts`
assigned_products = assignedproducts
def assignproduct(self, category_id, product, position=None):
"""
Assign product to a category
:param category_id: ID of a category
:param product: ID or Code of the product
:param position: Position of product in category
:return: boolean
"""
return bool(self.call(
'catalog_category.assignProduct', [category_id, product, position])
)
#: A proxy for :meth:`assignproduct`
assign_product = assignproduct
def updateproduct(self, category_id, product, position=None):
"""
Update assigned product
:param category_id: ID of a category
:param product: ID or Code of the product
:param position: Position of product in category
:return: boolean
"""
return bool(self.call(
'catalog_category.updateProduct', [category_id, product, position])
)
#: A proxy for :meth:`updateproduct`
update_product = updateproduct
def removeproduct(self, category_id, product):
"""
Remove product from category
:param category_id: ID of a category
:param product: ID or Code of the product
:return: boolean
"""
return bool(self.call(
'catalog_category.removeProduct', [category_id, product])
)
#: A proxy for :meth:`removeproduct`
remove_product = removeproduct
class CategoryAttribute(API):
"""
Product Category Attribute API to connect to magento
Allows to get attributes and options for category.
"""
__slots__ = ()
def currentStore(self, store_view=None):
"""
Set/Get current store view
:param store_view: Store view ID or Code
:return: int
"""
args = [store_view] if store_view else []
return int(self.call('catalog_category_attribute.currentStore', args))
def list(self):
"""
Retrieve Category attrbutes
"""
return self.call('category_attribute.list', [])
def options(self, attribute_id, store_view=None):
"""
Retrieve attribute options
:param attribute_id: ID of the attribute whose options are reqd
:param store_view: ID or Code of the store view
:return: list of dictionary
"""
return self.call(
'category_attribute.options', [attribute_id, store_view]
)
class Product(API):
"""
Product API for magento
"""
__slots__ = ()
def currentStore(self, store_view=None):
"""
Set/Get current store view
:param store_view: Store view ID or Code
:return: int
"""
args = [store_view] if store_view else []
return int(self.call('catalog_product.currentStore', args))
def list(self, filters=None, store_view=None):
"""
Retrieve product list by filters
:param filters: Dictionary of filters.
Format :
`{<attribute>:{<operator>:<value>}}`
Example :
`{'firstname':{'ilike':'sharoon'}}`
:param store_view: Code or ID of store view
:return: `list` of `dict`
"""
return self.call('catalog_product.list', [filters, store_view])
def info(self, product, store_view=None, attributes=None,
identifierType=None):
"""
Retrieve product data
:param product: ID or SKU of product
:param store_view: ID or Code of store view
:param attributes: List of fields required
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: `dict` of values
"""
return self.call(
'catalog_product.info', [
product, store_view, attributes, identifierType
]
)
def create(self, product_type, attribute_set_id, sku, data):
"""
Create Product and return ID
:param product_type: String type of product
:param attribute_set_id: ID of attribute set
:param sku: SKU of the product
:param data: Dictionary of data
:return: INT id of product created
"""
return int(self.call(
'catalog_product.create',
[product_type, attribute_set_id, sku, data]
)
)
def update(self, product, data, store_view=None, identifierType=None):
"""
Update product Information
:param product: ID or SKU of product
:param data: Dictionary of attributes to update
:param store_view: ID or Code of store view
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: Boolean
"""
return bool(self.call(
'catalog_product.update',
[product, data, store_view, identifierType]
))
def setSpecialPrice(self, product, special_price=None,
from_date=None, to_date=None, store_view=None,
identifierType=None):
"""
Update product's special price
:param product: ID or SKU of product
:param special_price: Special Price
:param from_date: From date
:param to_date: To Date
:param store_view: ID or Code of Store View
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: Boolean
"""
return bool(self.call(
'catalog_product.setSpecialPrice', [
product, special_price, from_date, to_date, store_view,
identifierType
]
))
def getSpecialPrice(self, product, store_view=None, identifierType=None):
"""
Get product special price data
:param product: ID or SKU of product
:param store_view: ID or Code of Store view
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: Dictionary
"""
return self.call(
'catalog_product.getSpecialPrice', [
product, store_view, identifierType
]
)
def delete(self, product, identifierType=None):
"""
Delete a product
:param product: ID or SKU of product
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: Boolean
"""
return bool(self.call('catalog_product.delete', [
product, identifierType
]))
class ProductAttribute(API):
"""
Product Attribute API
"""
__slots__ = ()
def currentStore(self, store_view=None):
"""
Set/Get current store view
:param store_view: Store view ID or Code
:return: int
"""
args = [store_view] if store_view else []
return int(self.call('catalog_product_attribute.currentStore', args))
def list(self, attribute_set_id):
"""
Retrieve product attribute list
:param attribute_set_id: ID of attribute set
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute.list', [attribute_set_id])
def info(self, attribute):
"""
Retrieve product attribute info
:param attribute: ID or Code of the attribute
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute.info', [attribute])
def options(self, attribute, store_view=None):
"""
Retrieve product attribute options
:param attribute: ID or Code of the attribute
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute.options',
[attribute, store_view])
def addOption(self, attribute, data):
"""
Create new options to attribute (Magento > 1.7.0)
:param attribute: ID or Code of the attribute.
:param data: Dictionary of Data.
{'label':[{'store_id':[0,1], 'value':'Value'},], 'order':1, 'is_default':1}
:return: True if created.
"""
return bool(self.call('product_attribute.addOption',
[attribute, data]))
def createOption(self, *a, **kw):
warnings.warn(
"ProductAttribute: createOption is deprecated, use addOption instead."
)
return self.addOption(*a, **kw)
def removeOption(self, attribute, option):
"""
Remove option to attribute (Magento > 1.7.0)
:param attribute: ID or Code of the attribute.
:param option: Option ID.
:return: True if the option is removed.
"""
return bool(self.call('product_attribute.removeOption',
[attribute, option]))
def create(self, data):
"""
Create attribute entity.
:param data: Dictionary of entity data to create attribute with.
:return: Integer ID of attribute created
"""
return self.call('catalog_product_attribute.create', [data])
def update(self, attribute, data):
"""
Update attribute entity data.
:param attribute: ID or Code of the attribute.
:param data: Dictionary of entity data to update on attribute.
:return: Boolean
"""
return self.call('catalog_product_attribute.update', [attribute, data])
class ProductAttributeSet(API):
"""
Product Attribute Set API
"""
__slots__ = ()
def list(self):
"""
Retrieve list of product attribute sets
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute_set.list', [])
def create(self, attribute_set_name, skeleton_set_id):
"""
Create a new attribute set based on a "skeleton" attribute set.
If unsure, use the "Default" attribute set as a skeleton.
:param attribute_set_name: name of the new attribute set
:param skeleton_set_id: id of the skeleton attribute set to base this set on.
:return: Integer ID of new attribute set
"""
return self.call('catalog_product_attribute_set.create', [attribute_set_name, skeleton_set_id])
def attributeAdd(self, attribute_id, attribute_set_id):
"""
Add an existing attribute to an attribute set.
:param attribute_id: ID of the attribute to add
:param attribute_set_id: ID of the attribute set to add to
:return: Boolean
"""
return self.call('catalog_product_attribute_set.attributeAdd', [attribute_id, attribute_set_id])
def attributeRemove(self, attribute_id, attribute_set_id):
"""
Remove an existing attribute to an attribute set.
:param attribute_id: ID of the attribute to remove
:param attribute_set_id: ID of the attribute set to remove from
:return: Boolean
"""
return self.call('catalog_product_attribute_set.attributeRemove', [attribute_id, attribute_set_id])
class ProductTypes(API):
"""
Product Types API
"""
__slots__ = ()
def list(self):
"""
Retrieve list of product types
:return: `list` of `dict`
"""
return self.call('catalog_product_type.list', [])
class ProductImages(API):
"""
Product Images API
"""
__slots__ = ()
def currentStore(self, store_view=None):
"""
Set/Get current store view
:param store_view: Store view ID or Code
:return: int
"""
args = []
if store_view:
args = [store_view]
return int(self.call('catalog_product_attribute_media.currentStore',
args))
def list(self, product, store_view=None, identifierType=None):
"""
Retrieve product image list
:param product: ID or SKU of product
:param store_view: Code or ID of store view
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute_media.list',
[product, store_view, identifierType])
def info(self, product, image_file, store_view=None, identifierType=None):
"""
Retrieve product image data
:param product: ID or SKU of product
:param store_view: ID or Code of store view
:param attributes: List of fields required
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute_media.info',
[product, image_file, store_view, identifierType])
def types(self, attribute_set_id):
"""
Retrieve product image types (image, small_image, thumbnail, etc.)
:param attribute_set_id: ID of attribute set
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute_media.types',
[attribute_set_id])
def create(self, product, data, store_view=None, identifierType=None):
"""
Upload a new product image.
:param product: ID or SKU of product
:param data: `dict` of image data (label, position, exclude, types)
Example: { 'label': 'description of photo',
'position': '1', 'exclude': '0',
'types': ['image', 'small_image', 'thumbnail']}
:param store_view: Store view ID or Code
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: string - image file name
"""
return self.call('catalog_product_attribute_media.create',
[product, data, store_view, identifierType])
def update(self, product, img_file_name, data, store_view=None,
identifierType=None):
"""
Update a product image.
:param product: ID or SKU of product
:param img_file_name: The image file name
Example: '/m/y/my_image_thumb.jpg'
:param data: `dict` of image data (label, position, exclude, types)
Example: { 'label': 'description of photo',
'position': '1', 'exclude': '0',
'types': ['image', 'small_image', 'thumbnail']}
:param store_view: Store view ID or Code
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: string - image file name
"""
return self.call('catalog_product_attribute_media.update',
[product, img_file_name, data, store_view, identifierType])
def remove(self, product, img_file_name, identifierType=None):
"""
Remove a product image.
:param product: ID or SKU of product
:param img_file_name: The image file name
Example: '/m/y/my_image_thumb.jpg'
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: boolean
"""
return self.call('catalog_product_attribute_media.remove',
[product, img_file_name, identifierType])
class ProductTierPrice(API):
"""
Product Tier Price API
"""
__slots__ = ()
def info(self, product, identifierType=None):
"""
Retrieve product data
:param product: ID or SKU of product
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: `list` of `dict`
"""
return self.call('catalog_product_attribute_tier_price.info',
[product, identifierType])
def update(self, product, data, identifierType=None):
"""
Update product tier prices.
Note: All existing tier prices for the product are replaced by the tier
prices provided in data.
:param product: ID or SKU of product
:param data: List of dictionaries of tier price information
Example:
[{
'website': 'all',
'customer_group_id': '1',
'qty': '99.0000',
'price': '123.9900'
},
{
'website': 'all',
...
},...]
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: Boolean
"""
return bool(self.call('catalog_product_attribute_tier_price.update',
[product, data, identifierType]))
class ProductLinks(API):
"""
Product links API (related, cross sells, up sells, grouped)
"""
__slots__ = ()
def list(self, link_type, product, identifierType=None):
"""
Retrieve list of linked products
:param link_type: type of link, one of 'cross_sell', 'up_sell',
'related' or 'grouped'
:param product: ID or SKU of product
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: `list` of `dict`
"""
return self.call('catalog_product_link.list',
[link_type, product, identifierType])
def assign(self, link_type, product, linked_product, data=None,
identifierType=None):
"""
Assign a product link
:param link_type: type of link, one of 'cross_sell', 'up_sell',
'related' or 'grouped'
:param product: ID or SKU of product
:param linked_product: ID or SKU of linked product
:param data: dictionary of link data, (position, qty, etc.)
Example: { 'position': '0', 'qty': 1}
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: boolean
"""
return bool(self.call('catalog_product_link.assign',
[link_type, product, linked_product, data, identifierType]))
def update(self, link_type, product, linked_product, data=None,
identifierType=None):
"""
Update a product link
:param link_type: type of link, one of 'cross_sell', 'up_sell',
'related' or 'grouped'
:param product: ID or SKU of product
:param linked_product: ID or SKU of linked product
:param data: dictionary of link data, (position, qty, etc.)
Example: { 'position': '0', 'qty': 1}
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: boolean
"""
return bool(self.call('catalog_product_link.update',
[link_type, product, linked_product, data, identifierType]))
def remove(self, link_type, product, linked_product, identifierType=None):
"""
Remove a product link
:param link_type: type of link, one of 'cross_sell', 'up_sell',
'related' or 'grouped'
:param product: ID or SKU of product
:param linked_product: ID or SKU of linked product to unlink
:param identifierType: Defines whether the product or SKU value is
passed in the "product" parameter.
:return: boolean
"""
return bool(self.call('catalog_product_link.remove',
[link_type, product, linked_product, identifierType]))
def types(self):
"""
Retrieve a list of product link types
:return: `list` of types
"""
return self.call('catalog_product_link.types', [])
def attributes(self, link_type):
"""
Retrieve a list of attributes of a product link type
:param link_type: type of link, one of 'cross_sell', 'up_sell',
'related' or 'grouped'
:return: `list` of `dict`
Format :
`[{'code': <attribute code>, 'type': <attribute type>}, ...]`
Example :
`[{'code': 'position', 'type': 'int'},
{'code': 'qty', 'type': 'decimal'}]`
"""
return self.call('catalog_product_link.attributes', [link_type])
class ProductConfigurable(API):
"""
Product Configurable API for magento.
These API endpoints only work if you have zikzakmedia's
magento_webservices Magento plugin installed.
"""
__slots__ = ()
def info(self, product):
"""
Configurable product Info
:param product: ID or SKU of product
:return: List
"""
return self.call('ol_catalog_product_link.list', [product])
def getSuperAttributes(self, product):
"""
Configurable Attributes product
:param product: ID or SKU of product
:return: List
"""
return self.call('ol_catalog_product_link.listSuperAttributes',
[product])
def setSuperAttributeValues(self, product, attribute):
"""
Configurable Attributes product
:param product: ID or SKU of product
:param attribute: ID attribute
:return: List
"""
return self.call('ol_catalog_product_link.setSuperAttributeValues',
[product, attribute])
def update(self, product, linked_products, attributes):
"""
Configurable Update product
:param product: ID or SKU of product
:param linked_products: List ID or SKU of linked product to link
:param attributes: dicc
:return: True/False
"""
return bool(self.call('ol_catalog_product_link.assign',
[product, linked_products, attributes]))
def remove(self, product, linked_products):
"""
Remove a product link configurable
:param product: ID or SKU of product
:param linked_products: List ID or SKU of linked product to unlink
"""
return bool(self.call('ol_catalog_product_link.remove',
[product, linked_products]))
class Inventory(API):
"""
Allows to update stock attributes (status, quantity)
"""
__slots__ = ()
def list(self, products):
"""
Retrieve inventory stock data by product ids
:param products: list of IDs or SKUs of products
:return: `list` of `dict`
"""
return self.call('cataloginventory_stock_item.list', [products])
def update(self, product, data):
"""
Update inventory stock data
:param product: ID or SKU of product
:param data: Dictionary of data to change,
eg dict(qty=99, is_in_stock='1')
:return: boolean
"""
return bool(
self.call(
'cataloginventory_stock_item.update',
[product, data]
)
)
def update_multi(self, product_data_pairs):
"""
It is usually expensive to update inventory on magento and this
uses the multi call api to make it faster. The expected argument is
a list of pairs of product and data dictionaries.
"""
return self.multiCall([
[
'cataloginventory_stock_item.update',
product_data_pair
]
for product_data_pair in product_data_pairs
])