Completed
Pull Request — master (#346)
by
unknown
17:31
created

WeChatMerchant.get_by_status()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
ccs 1
cts 2
cp 0.5
crap 1.125
rs 9.4285
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3 10
from wechatpy.client.api.base import BaseWeChatAPI
4
5 10
from wechatpy.client.api.merchant.category import MerchantCategory
6 10
from wechatpy.client.api.merchant.stock import MerchantStock
7 10
from wechatpy.client.api.merchant.express import MerchantExpress
8 10
from wechatpy.client.api.merchant.group import MerchantGroup
9 10
from wechatpy.client.api.merchant.shelf import MerchantShelf
10 10
from wechatpy.client.api.merchant.order import MerchantOrder
11 10
from wechatpy.client.api.merchant.common import MerchantCommon
12
13
14 10
class WeChatMerchant(BaseWeChatAPI):
15
16 10
    API_BASE_URL = 'https://api.weixin.qq.com/'
17
18 10
    def __init__(self, *args, **kwargs):
19 10
        super(WeChatMerchant, self).__init__(*args, **kwargs)
20
21
        # sub APIs
22 10
        self.category = MerchantCategory(self._client)
23 10
        self.stock = MerchantStock(self._client)
24 10
        self.express = MerchantExpress(self._client)
25 10
        self.group = MerchantGroup(self._client)
26 10
        self.shelf = MerchantShelf(self._client)
27 10
        self.order = MerchantOrder(self._client)
28 10
        self.common = MerchantCommon(self._client)
29
30 10
    def create(self, product_data):
31
        """增加商品"""
32
        return self._post(
33
            'merchant/create',
34
            data=product_data
35
        )
36 10
37
    def delete(self, product_id):
38
        """删除商品"""
39
        return self._post(
40
            'merchant/del',
41
            data={
42
                'product_id': product_id
43
            }
44 10
        )
45
46
    def update(self, product_id, product_data):
47
        """修改商品"""
48
        product_data['product_id'] = product_id
49
        return self._post(
50
            'merchant/update',
51 10
            data=product_data
52
        )
53
54
    def get(self, product_id):
55
        """查询商品"""
56
        return self._post(
57
            'merchant/get',
58
            data={
59 10
                'product_id': product_id
60
            }
61
        )
62
63
    def get_by_status(self, status):
64
        """获取指定状态的所有商品"""
65
        return self._post(
66
            'merchant/getbystatus',
67 10
            data={
68
                'status': status
69
            }
70
        )
71
72
    def update_product_status(self, product_id, status):
73
        """商品上下架"""
74
        return self._post(
75
            'merchant/modproductstatus',
76
            data={
77
                'product_id': product_id,
78
                'status': status
79
            }
80
        )
81
82
    def get_category_getsub(self, cate_id):
83
        """
84
        获取指定分类的所有子分类
85
        :param cate_id: 大分类ID(根节点分类id为1)
86
        :return: 返回的 JSON 数据包
87
        """
88
        return self._post(
89
            'merchant/category/getsub',
90
            data={
91
                'cate_id': cate_id
92
            }
93
        )
94
95
    def get_category_getsku(self, cate_id):
96
        """
97
        获取指定子分类的所有SKU
98
        :param cate_id: 商品子分类ID
99
        :return: 返回的 JSON 数据包
100
        """
101
        return self._post(
102
            'merchant/category/getsku',
103
            data={
104
                'cate_id': cate_id
105
            }
106
        )
107
108
    def get_category_getproperty(self, cate_id):
109
        """
110
        获取指定分类的所有属性
111
        :param cate_id: 商品子分类ID
112
        :return: 返回的 JSON 数据包
113
        """
114
        return self._post(
115
            'merchant/category/getproperty',
116
            data={
117
                'cate_id': cate_id
118
            }
119
        )
120
121
    def add_stock(self, product_id, sku_info, quantity):
122
        """
123
        增加库存
124
        :param product_id: 商品ID
125
        :param sku_info: sku信息,格式"id1:vid1;id2:vid2",如商品为统一规格,则此处赋值为空字符串即可
126
        :param quantity: 增加的库存数量
127
        :return: 返回的 JSON 数据包
128
        """
129
        return self._post(
130
            'merchant/stock/add',
131
            data={
132
                "product_id": product_id, 
133
                "sku_info": sku_info, 
134
                "quantity": quantity
135
            }
136
        )
137
138
    def reduce_stock(self, product_id, sku_info, quantity):
139
        """
140
        减少库存
141
        :param product_id: 商品ID
142
        :param sku_info: sku信息,格式"id1:vid1;id2:vid2",如商品为统一规格,则此处赋值为空字符串即可
143
        :param quantity: 减少的库存数量
144
        :return: 返回的 JSON 数据包
145
        """
146
        return self._post(
147
            'merchant/stock/reduce',
148
            data={
149
                "product_id": product_id, 
150
                "sku_info": sku_info, 
151
                "quantity": quantity
152
            }
153
        )
154
155
    def add_express(self, product_data):
156
        """
157
        增加邮费模板
158
        :param product_data: 邮费信息
159
        :return: 返回的 JSON 数据包
160
        """
161
        return self._post(
162
            'merchant/express/add',
163
            data=product_data
164
        )
165
    
166
    def del_express(self, template_id):
167
        """
168
        增加邮费模板
169
        :param template_id: 邮费模板ID
170
        :return: 返回的 JSON 数据包
171
        """
172
        return self._post(
173
            'merchant/express/del',
174
            data={
175
                'template_id':template_id
176
            }
177
        )
178
179
    def update_express(self, template_id, delivery_template):
180
        """
181
        增加邮费模板
182
        :param template_id: 邮费模板ID
183
        :param delivery_template: 邮费模板信息(字段说明详见增加邮费模板)
184
        :return: 返回的 JSON 数据包
185
        """
186
        delivery_template['template_id'] = template_id
187
        return self._post(
188
            'merchant/express/update',
189
            data=delivery_template
190
        )
191
192
    def get_express(self, template_id):
193
        """
194
        获取指定ID的邮费模板
195
        :param template_id: 邮费模板ID
196
        :return: 返回的 JSON 数据包
197
        """
198
        return self._post(
199
            'merchant/express/getbyid',
200
            data={
201
                'template_id': template_id
202
            }
203
        )
204
205
    def get_all_express(self):
206
        """
207
        获取所有邮费模板
208
        :param template_id: 邮费模板ID
209
        :return: 返回的 JSON 数据包
210
        """
211
        return self._get(
212
            'merchant/express/getall'
213
        )
214
215
    def add_group(self, group_detail):
216
        """
217
        增加分组
218
        :param group_detail: 商品分组信息
219
        :return: 返回的 JSON 数据包
220
        """
221
        return self._post(
222
            'merchant/group/add',
223
            data=group_detail
224
        )
225
    
226
    def del_group(self, group_id):
227
        """
228
        删除分组
229
        :param group_id: 商品分组ID
230
        :return: 返回的 JSON 数据包
231
        """
232
        return self._post(
233
            'merchant/group/del',
234
            data={
235
                'group_id': group_id
236
            }
237
        )
238
239
    def update_group_name(self, group_id, group_name):
240
        """
241
        修改分组属性
242
        :param group_id: 商品分组ID
243
        :param group_name: 商品分组名称
244
        :return: 返回的 JSON 数据包
245
        """
246
        return self._post(
247
            'merchant/group/propertymod',
248
            data={
249
                'group_id': group_id,
250
                'group_name': group_name
251
            }
252
        )
253
    
254
    def update_group_product(self, group_id, product_data):
255
        """
256
        修改分组商品
257
        :param group_id: 商品分组ID
258
        :param product_data: 分组商品信息
259
        :return: 返回的 JSON 数据包
260
        """
261
        product_data['group_id'] = group_id
262
        return self._post(
263
            'merchant/group/productmod',
264
            data=product_data
265
        )
266
267
    def get_all_group(self):
268
        """
269
        获取所有分组
270
        :return: 返回的 JSON 数据包
271
        """
272
        return self._get(
273
            'merchant/group/getall'
274
        )
275
276
    def get_group(self, group_id):
277
        """
278
        根据分组ID获取分组信息
279
        :param group_id: 商品分组ID
280
        :return: 返回的 JSON 数据包
281
        """
282
        return self._post(
283
            'merchant/group/getbyid',
284
            data={
285
                'group_id': group_id
286
            }
287
        )
288
289
    def add_shelf(self, shelf_data):
290
        """
291
        增加货架
292
        :param shelf_data: 货架详情信息
293
        :return: 返回的 JSON 数据包
294
        """
295
        return self._post(
296
            'merchant/shelf/add',
297
            data=shelf_data
298
        )
299
300
    def del_shelf(self, shelf_id):
301
        """
302
        删除货架
303
        :param shelf_id: 货架ID
304
        :return: 返回的 JSON 数据包
305
        """
306
        return self._post(
307
            'merchant/shelf/del',
308
            data={
309
                'shelf_id': shelf_id
310
            }
311
        )
312
313
    def update_shelf(self, shelf_id, shelf_data):
314
        """
315
        修改货架
316
        :param shelf_id: 货架ID
317
        :param shelf_data: 货架详情
318
        :return: 返回的 JSON 数据包
319
        """
320
        shelf_data['shelf_id'] = shelf_id
321
        return self._post(
322
            'merchant/shelf/mod',
323
            data=shelf_data
324
        )
325
326
    def get_all_shelf(self):
327
        """
328
        获取所有货架
329
        :return: 返回的 JSON 数据包
330
        """
331
        return self._get(
332
            'merchant/shelf/getall'
333
        )
334
335
    def get_shelf(self, shelf_id):
336
        """
337
        根据货架ID获取货架信息
338
        :param shelf_id: 货架ID
339
        :return: 返回的 JSON 数据包
340
        """
341
        return self._post(
342
            'merchant/shelf/getbyid',
343
            data={
344
                'shelf_id': shelf_id
345
            }
346
        )
347
348
    def get_order(self, order_id):
349
        """
350
        根据订单ID获取订单详情
351
        :param order_id: 订单ID
352
        :return: 返回的 JSON 数据包
353
        """
354
        return self._post(
355
            'merchant/order/getbyid',
356
            data={
357
                'order_id': order_id
358
            }
359
        )
360
361
    def query_order(self, status=None, begintime=None, endtime=None):
362
        """
363
        根据订单状态/创建时间获取订单详情
364
        :param status: 订单状态(不带该字段-全部状态, 2-待发货, 3-已发货, 5-已完成, 8-维权中, )
365
        :param begintime: 订单创建时间起始时间(不带该字段则不按照时间做筛选)
366
        :param endtime: 订单创建时间终止时间(不带该字段则不按照时间做筛选)
367
        :return: 返回的 JSON 数据包
368
        """
369
        return self._post(
370
            'merchant/order/getbyfilter',
371
            data={
372
                'status': status,
373
                'begintime': begintime,
374
                'endtime': endtime
375
            }
376
        )
377
378
    def set_delivery(self, order_id, delivery_data):
379
        """
380
        修改货架
381
        :param order_id: 订单ID
382
        :param delivery_data: 商品物流信息
383
        :return: 返回的 JSON 数据包
384
        """
385
        delivery_data['order_id'] = order_id
386
        return self._post(
387
            'merchant/shelf/setdeliverymod',
388
            data=delivery_data
389
        )
390
391
    def upload_image(self, media_file):
392
        """
393
        上传图片
394
        :param media_file: 要上传的文件,一个 File-object
395
        :return: 上传成功时返回图片 URL
396
        """
397
        res = self._post(
398
            url='merchant/common/upload_img',
399
            files={
400
                'media': media_file
401
            },
402
            result_processor=lambda x: x['url']
403
        )
404
        return res