Completed
Pull Request — master (#346)
by
unknown
31:42 queued 11:48
created

WeChatMerchant.create()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

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