-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_server.py
424 lines (376 loc) · 13.2 KB
/
grpc_server.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
# coding: utf-8
import asyncio
import json
import grpc
import market_pb2
import market_pb2_grpc
from tortoise import Tortoise
from v1.shop.manager import (
get_categories,
get_brands,
get_product,
get_products,
get_product_items,
manage_category,
manage_brand_name,
manage_product,
create_product_items,
buy_rent_product_item,
delete_product_item,
delete_product,
edit_product_item,
)
from v1.util import (
export_stat_xls,
get_debtors_ids,
get_buyer_depts,
)
from statistic.handler import SailsStat as SailsStatObj
from settings import DATABASE
class GetCategoriesServer(market_pb2_grpc.GetCategoriesServicer):
async def GetCats(
self,
request: market_pb2.CategoriesRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.CategoriesResponse:
res = await get_categories(
limit=request.limit,
offset=request.offset,
)
return market_pb2.CategoriesResponse(
count=res["count"],
items=res["items"],
)
class ManageCategoryServer(market_pb2_grpc.ManageCategoryServicer):
async def ManageCats(
self,
request: market_pb2.CategoryManageRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.CategoryItemResponse:
res = await manage_category(
id=request.id, name=request.name
)
return market_pb2.CategoryItemResponse(
id=res["id"],
name=res["name"],
created_at=res["created_at"],
updated_at=res["updated_at"],
)
class GetBrandsServer(market_pb2_grpc.GetBrandsNamesServicer):
async def GetBrands(
self,
request: market_pb2.BrandRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.BrandResponse:
res = await get_brands(
limit=request.limit,
offset=request.offset,
)
return market_pb2.BrandResponse(
count=res["count"],
items=res["items"],
)
class ManageBrandServer(market_pb2_grpc.ManageBrandsNamesServicer):
async def ManageBrands(
self,
request: market_pb2.BrandManageRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.BrandItemResponse:
res = await manage_brand_name(
id=request.id,
name=request.name,
logo=request.logo,
ext=request.ext,
)
return market_pb2.BrandItemResponse(
id=res["id"],
name=res["name"],
logo=res["logo"],
created_at=res["created_at"],
updated_at=res["updated_at"],
)
class GetProductsServer(market_pb2_grpc.GetProductsServicer):
async def GetProds(
self,
request: market_pb2.ProductRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductListResponse:
if request.id:
res = await get_product(product_pk=request.id)
res = {
"count": 1,
"items": [res]
}
else:
if request.rent == -1:
rent = None
else:
rent = bool(request.rent)
if request.service == -1:
service = None
else:
service = bool(request.service)
res = await get_products(
limit=request.limit,
offset=request.offset,
brand_name=request.brand_name,
category=request.category,
sex=request.sex,
age=request.age,
name=request.name,
article_number=request.article_number,
rent=rent,
service=service,
)
return market_pb2.ProductListResponse(
count=res["count"],
items=res["items"],
)
class ManageProductsServer(market_pb2_grpc.GetProductsServicer):
async def ManageProds(
self,
request: market_pb2.ProductManageRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductResponse:
res = await manage_product(
id=request.id,
name=request.name,
description=request.description,
brand_name=request.brand_name,
category=request.category,
color=request.color,
sex=request.sex,
age=request.age,
article_number=request.article_number,
price=request.price,
currency=request.currency,
discount=request.discount,
rent=request.rent,
service=request.service,
gallery=[
{"ext": g.ext, "bytes": g.bytes}
for g in request.pgr
],
exist_gallery=request.eg,
size=request.size,
)
return market_pb2.ProductResponse(**res)
class GetProductItemsServer(market_pb2_grpc.GetProductsItemsServicer):
async def GetProdItems(
self,
request: market_pb2.ProductItemsRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductItemsListResponse:
res = await get_product_items(
product=request.product,
limit=request.limit,
offset=request.offset,
sold=request.sold,
rent_date_start=request.rent_date_start,
rent_date_stop=request.rent_date_stop,
)
return market_pb2.ProductItemsListResponse(
count=res["count"],
items=res["items"],
)
class CreateProductItemsServer(market_pb2_grpc.CreateProductsItemsServicer):
async def CreateProdItems(
self,
request: market_pb2.ProductItemCreateRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductItemsCreateResponse:
res = await create_product_items(
product=request.product,
items=[
{
"id": p.id if p.id else None,
"sold": p.sold,
"buyer": p.buyer,
"rent_time_start": p.rent_time_start if p.rent_time_start else None,
"rent_time_stop": p.rent_time_stop if p.rent_time_stop else None,
}
for p in request.items
]
)
return market_pb2.ProductItemsCreateResponse(arr=res)
class BuyProductItemServer(market_pb2_grpc.BuyProductItemServicer):
async def BuyProdItem(
self,
request: market_pb2.ProductItemBuyRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductItemBuyResponse:
res = await buy_rent_product_item(
item_pk=request.item_pk,
product_id=request.product_id,
article_number=request.article_number,
buyer=request.buyer,
rent_time_start=request.rent_time_start,
rent_time_stop=request.rent_time_stop,
salesman=request.salesman,
payed_amount=request.payed_amount,
payment_type=request.payment_type,
)
return market_pb2.ProductItemBuyResponse(message=res)
class RemoveProductItemServer(market_pb2_grpc.RemoveProductItemServicer):
async def RemoveProdItem(
self,
request: market_pb2.ProductItemRemoveRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductItemRemoveResponse:
res = await delete_product_item(item_pk=request.item_pk)
return market_pb2.ProductItemRemoveResponse(message=True)
class RemoveProductServer(market_pb2_grpc.RemoveProductServicer):
async def RemoveProduct(
self,
request: market_pb2.ProductRemoveRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductRemoveResponse:
res = await delete_product(product_pk=request.product_pk)
return market_pb2.ProductRemoveResponse(message=True)
class ProductItemsStatServer(
market_pb2_grpc.RemoveProductServicer
):
async def ProductItemsStat(
self,
request: market_pb2.ProductItemsStatRequest,
context: grpc.aio.ServicerContext
) -> market_pb2.ProductItemsStatResponse:
stat = await export_stat_xls(
date_start=request.date_start,
date_stop=request.date_stop,
)
return market_pb2.ProductItemsStatResponse(statistic=stat)
class SailsStatServer(market_pb2_grpc.SailsStatServicer):
async def SailsStat(
self,
request: market_pb2.SailsStatRequest,
context: grpc.aio.ServicerContext,
) -> market_pb2.SailsStatResponse:
stat_obj = SailsStatObj(
date_start=request.date_start,
date_stop=request.date_stop,
)
stat = await stat_obj.get_sails_stat()
return market_pb2.SailsStatResponse(statistic=stat)
class DetailSailsStatServer(market_pb2_grpc.SailsStatServicer):
async def DetailSailsStat(
self,
request: market_pb2.SailsStatRequest,
context: grpc.aio.ServicerContext,
) -> market_pb2.DetailSailsStatResponse:
stat_obj = SailsStatObj(
date_start=request.date_start,
date_stop=request.date_stop,
detail=True,
)
stat = await stat_obj.get_sails_stat()
return market_pb2.DetailSailsStatResponse(statistic=stat)
class DebtorsServer(market_pb2_grpc.DebtorsServicer):
async def Debtors(
self,
request: market_pb2.DebtorsRequest,
context: grpc.aio.ServicerContext,
) -> market_pb2.DebtorsResponse:
debtors = await get_debtors_ids()
return market_pb2.DebtorsResponse(debtors=debtors)
class BuyerDeptsServer(market_pb2_grpc.BuyerDeptsServicer):
async def BuyerDepts(
self,
request: market_pb2.BuyerDeptsRequest,
context: grpc.aio.ServicerContext,
) -> market_pb2.ProductItemsStatResponse:
debts = await get_buyer_depts(buyer=request.buyer)
return market_pb2.ProductItemsStatResponse(statistic=debts)
class ProductItemEditServer(market_pb2_grpc.ProductItemEditServicer):
async def ProductItemEdit(
self,
request: market_pb2.ProductItemRequest,
context: grpc.aio.ServicerContext,
) -> market_pb2.ProductItemRemoveResponse:
product_item = await edit_product_item(
product_item_pk=request.product_item_pk,
payed_amount=request.payed_amount,
)
return market_pb2.ProductItemResponse(**product_item)
async def serve() -> None:
await Tortoise.init(
config={
"connections": {
"default": {
"engine": "tortoise.backends.asyncpg",
"credentials": {
"database": DATABASE["name"],
"host": DATABASE["address"],
"password": DATABASE["password"],
"port": 5432,
"user": DATABASE["user"],
},
}
},
"apps": {
"models": {
"models": ["models.models", "aerich.models"],
"default_connection": "default",
}
},
},
)
server = grpc.aio.server()
market_pb2_grpc.add_GetCategoriesServicer_to_server(
GetCategoriesServer(), server
)
market_pb2_grpc.add_ManageCategoryServicer_to_server(
ManageCategoryServer(), server
)
market_pb2_grpc.add_GetBrandsNamesServicer_to_server(
GetBrandsServer(), server
)
market_pb2_grpc.add_ManageBrandsNamesServicer_to_server(
ManageBrandServer(), server
)
market_pb2_grpc.add_GetProductsServicer_to_server(
GetProductsServer(), server
)
market_pb2_grpc.add_ManageProductsServicer_to_server(
ManageProductsServer(), server
)
market_pb2_grpc.add_GetProductsItemsServicer_to_server(
GetProductItemsServer(), server
)
market_pb2_grpc.add_CreateProductsItemsServicer_to_server(
CreateProductItemsServer(), server
)
market_pb2_grpc.add_BuyProductItemServicer_to_server(
BuyProductItemServer(), server
)
market_pb2_grpc.add_RemoveProductItemServicer_to_server(
RemoveProductItemServer(), server
)
market_pb2_grpc.add_RemoveProductServicer_to_server(
RemoveProductServer(), server
)
market_pb2_grpc.add_ProductItemsStatServicer_to_server(
ProductItemsStatServer(), server
)
market_pb2_grpc.add_SailsStatServicer_to_server(
SailsStatServer(), server
)
market_pb2_grpc.add_DetailSailsStatServicer_to_server(
DetailSailsStatServer(), server
)
market_pb2_grpc.add_DebtorsServicer_to_server(
DebtorsServer(), server
)
market_pb2_grpc.add_BuyerDeptsServicer_to_server(
BuyerDeptsServer(), server
)
market_pb2_grpc.add_ProductItemEditServicer_to_server(
ProductItemEditServer(), server
)
listen_addr = '[::]:50051'
server.add_insecure_port(listen_addr)
print("Starting server on ", listen_addr)
await server.start()
await server.wait_for_termination()
if __name__ == "__main__":
asyncio.run(serve())