-
Notifications
You must be signed in to change notification settings - Fork 24
/
objects.py
735 lines (556 loc) · 23.9 KB
/
objects.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
# -*- coding: utf-8 -*-
# Copyright 2021 ODOOGAP/PROMPTEQUATION LDA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
import graphene
from graphene.types import generic
from graphql import GraphQLError
from odoo import SUPERUSER_ID, _
from odoo.addons.graphql_base import OdooObjectType
from odoo.exceptions import AccessError
from odoo.http import request
# --------------------- #
# ENUMS #
# --------------------- #
AddressType = graphene.Enum('AddressType', [('Contact', 'contact'), ('InvoiceAddress', 'invoice'),
('DeliveryAddress', 'delivery'), ('OtherAddress', 'other'),
('PrivateAddress', 'private')])
OrderStage = graphene.Enum('OrderStage', [('Quotation', 'draft'), ('QuotationSent', 'sent'),
('SalesOrder', 'sale'), ('Locked', 'done'), ('Cancelled', 'cancel')])
InvoiceStatus = graphene.Enum('InvoiceStatus', [('UpsellingOpportunity', 'upselling'), ('FullyInvoiced', 'invoiced'),
('ToInvoice', 'to invoice'), ('NothingtoInvoice', 'no')])
InvoiceState = graphene.Enum('InvoiceState', [('Draft', 'draft'), ('Posted', 'posted'), ('Cancelled', 'cancel')])
InventoryAvailability = graphene.Enum('InventoryAvailability', [
('SellRegardlessOfInventory', 'never'), ('ShowInventoryOnWebsiteAndPreventSalesIfNotEnoughStock', 'always'),
('ShowInventoryBelowAThresholdAndPreventSalesIfNotEnoughStock', 'threshold'),
('ShowProductSpecificNotifications', 'custom')
])
PaymentTransactionState = graphene.Enum('PaymentTransactionState', [('Draft', 'draft'), ('Pending', 'pending'),
('Authorized', 'authorized'), ('Confirmed', 'done'),
('Canceled', 'cancel'), ('Error', 'error')])
class SortEnum(graphene.Enum):
ASC = 'ASC'
DESC = 'DESC'
# --------------------- #
# Functions #
# --------------------- #
def get_document_with_check_access(model, domain=[], order=None, limit=20, offset=0, access_token=None,
error_msg='This document does not exist.'):
if access_token:
model = model.sudo()
domain = [('access_token', '=', access_token)]
document = model.search(domain, order=order, limit=limit, offset=offset)
document_sudo = document.with_user(SUPERUSER_ID).exists()
if document and not document_sudo:
raise GraphQLError(_(error_msg))
try:
document.check_access_rights('read')
document.check_access_rule('read')
except AccessError:
return []
return document_sudo
def get_document_count_with_check_access(model, domain):
try:
model.check_access_rights('read')
model.check_access_rule('read')
except AccessError:
return 0
return model.search_count(domain)
def get_product_pricing_info(env, product):
website = env['website'].get_current_website()
pricelist = website.get_current_pricelist()
return product._get_combination_info_variant(pricelist=pricelist)
def product_is_in_wishlist(env, product):
website = env['website'].get_current_website()
request.website = website
return product._is_in_wishlist()
# --------------------- #
# Objects #
# --------------------- #
class Lead(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
email = graphene.String()
phone = graphene.String()
company = graphene.String()
subject = graphene.String()
message = graphene.String()
def resolve_name(self, info):
return self.contact_name
def resolve_email(self, info):
return self.email_from
def resolve_company(self, info):
return self.partner_name
def resolve_subject(self, info):
return self.name
def resolve_message(self, info):
return self.description
class State(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String(required=True)
code = graphene.String(required=True)
class Country(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String(required=True)
code = graphene.String(required=True)
states = graphene.List(graphene.NonNull(lambda: State))
image_url = graphene.String()
def resolve_states(self, info):
return self.state_ids or None
class Pricelist(OdooObjectType):
id = graphene.Int()
name = graphene.String()
currency = graphene.Field(lambda: Currency)
def resolve_currency(self, info):
return self.currency_id or None
class Partner(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
street = graphene.String()
street2 = graphene.String()
city = graphene.String()
country = graphene.Field(lambda: Country)
state = graphene.Field(lambda: State)
zip = graphene.String()
email = graphene.String()
phone = graphene.String()
mobile = graphene.String()
address_type = AddressType()
billing_address = graphene.Field(lambda: Partner)
is_company = graphene.Boolean(required=True)
company = graphene.Field(lambda: Partner)
contacts = graphene.List(graphene.NonNull(lambda: Partner))
signup_token = graphene.String()
signup_valid = graphene.String()
parent_id = graphene.Field(lambda: Partner)
image = graphene.String()
public_pricelist = graphene.Field(lambda: Pricelist)
current_pricelist = graphene.Field(lambda: Pricelist)
def resolve_country(self, info):
return self.country_id or None
def resolve_state(self, info):
return self.state_id or None
def resolve_address_type(self, info):
return self.type or None
def resolve_billing_address(self, info):
billing_address = self.child_ids.filtered(lambda a: a.type and a.type == 'invoice')
return billing_address and billing_address[0] or None
def resolve_company(self, info):
return self.company_id.partner_id or None
def resolve_contacts(self, info):
return self.child_ids or None
def resolve_parent_id(self, info):
return self.parent_id or None
def resolve_image(self, info):
return '/web/image/res.partner/{}/image_1920'.format(self.id)
def resolve_public_pricelist(self, info):
website = self.env['website'].get_current_website()
partner = website.user_id.sudo().partner_id
return partner.last_website_so_id.pricelist_id or partner.property_product_pricelist
def resolve_current_pricelist(self, info):
website = self.env['website'].get_current_website()
return website.get_current_pricelist()
class User(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String(required=True)
email = graphene.String(required=True)
partner = graphene.Field(lambda: Partner)
def resolve_email(self, info):
return self.login or None
def resolve_partner(self, info):
return self.partner_id or None
class Currency(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
symbol = graphene.String()
class Category(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
parent = graphene.Field(lambda: Category)
childs = graphene.List(graphene.NonNull(lambda: Category))
slug = graphene.String()
products = graphene.List(graphene.NonNull(lambda: Product))
json_ld = generic.GenericScalar()
def resolve_parent(self, info):
return self.parent_id or None
def resolve_childs(self, info):
return self.child_id or None
def resolve_slug(self, info):
return self.website_slug
def resolve_products(self, info):
return self.product_tmpl_ids or None
def resolve_json_ld(self, info):
return self.get_json_ld()
class AttributeValue(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
display_type = graphene.String()
html_color = graphene.String()
search = graphene.String()
price_extra = graphene.Float(description='Not use in the return Attributes List of the Products Query')
attribute = graphene.Field(lambda: Attribute)
def resolve_id(self, info):
return self.id or None
def resolve_search(self, info):
attribute_id = self.attribute_id.id
attribute_value_id = self.id
return '{}-{}'.format(attribute_id, attribute_value_id) or None
def resolve_attribute(self, info):
return self.attribute_id or None
class Attribute(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
display_type = graphene.String()
values = graphene.List(graphene.NonNull(lambda: AttributeValue))
def resolve_values(self, info):
return self.value_ids or None
class ProductImage(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
image = graphene.String()
video = graphene.String()
def resolve_id(self, info):
return self.id or None
def resolve_image(self, info):
return '/web/image/product.image/{}/image_1920'.format(self.id)
def resolve_video(self, info):
return self.video_url or None
class Product(OdooObjectType):
id = graphene.Int(required=True)
type_id = graphene.String()
visibility = graphene.Int()
status = graphene.Int()
name = graphene.String()
display_name = graphene.String()
sku = graphene.String()
barcode = graphene.String()
description = graphene.String()
currency = graphene.Field(lambda: Currency)
weight = graphene.Float()
meta_title = graphene.String()
meta_keyword = graphene.String()
meta_description = graphene.String()
image = graphene.String()
small_image = graphene.String()
thumbnail = graphene.String()
categories = graphene.List(graphene.NonNull(lambda: Category))
inventory_availability = InventoryAvailability()
available_threshold = graphene.String(
description='Related W/Availability: Show inventory below a threshold and prevent sales if not enough stock'
)
custom_message = graphene.String(description='Related W/Availability: Show product-specific notifications')
is_in_stock = graphene.Boolean()
is_in_wishlist = graphene.Boolean()
media_gallery = graphene.List(graphene.NonNull(lambda: ProductImage))
qty = graphene.Float()
slug = graphene.String()
alternative_products = graphene.List(graphene.NonNull(lambda: Product))
accessory_products = graphene.List(graphene.NonNull(lambda: Product))
# Specific to use in Product Variant
combination_info_variant = generic.GenericScalar(description='Specific to Product Variant')
variant_price = graphene.Float(description='Specific to Product Variant')
variant_price_after_discount = graphene.Float(description='Specific to Product Variant')
variant_has_discounted_price = graphene.Boolean(description='Specific to Product Variant')
is_variant_possible = graphene.Boolean(description='Specific to Product Variant')
variant_attribute_values = graphene.List(graphene.NonNull(lambda: AttributeValue),
description='Specific to Product Variant')
product_template = graphene.Field((lambda: Product), description='Specific to Product Variant')
# Specific to use in Product Template
combination_info = generic.GenericScalar(description='Specific to Product Template')
price = graphene.Float(description='Specific to Product Template')
attribute_values = graphene.List(graphene.NonNull(lambda: AttributeValue),
description='Specific to Product Template')
product_variants = graphene.List(graphene.NonNull(lambda: Product), description='Specific to Product Template')
first_variant = graphene.Field(lambda: Product, description='Specific to use in Product Template')
json_ld = generic.GenericScalar()
def resolve_type_id(self, info):
if self.type == 'product':
return 'simple'
else:
return 'configurable'
def resolve_visibility(self, info):
if self.website_published:
return 1
else:
return 0
def resolve_status(self, info):
if self.free_qty > 0:
return 1
else:
return 0
def resolve_sku(self, info):
return self.default_code or None
def resolve_description(self, info):
return self.description_sale or None
def resolve_currency(self, info):
return self.currency_id or None
def resolve_meta_title(self, info):
return self.website_meta_title or None
def resolve_meta_keyword(self, info):
return self.website_meta_keywords or None
def resolve_meta_description(self, info):
return self.website_meta_description or None
def resolve_image(self, info):
return '/web/image/{}/{}/image_1920'.format(self._name, self.id)
def resolve_small_image(self, info):
return '/web/image/{}/{}/image_128'.format(self._name, self.id)
def resolve_thumbnail(self, info):
return '/web/image/{}/{}/image_512'.format(self._name, self.id)
def resolve_categories(self, info):
website = self.env['website'].get_current_website()
if website:
return self.public_categ_ids.filtered(
lambda c: not c.website_id or c.website_id and c.website_id.id == website.id) or None
return self.public_categ_ids or None
def resolve_available_threshold(self, info):
return self.available_threshold or None
def resolve_custom_message(self, info):
return self.custom_message or None
def resolve_is_in_stock(self, info):
return bool(self.free_qty > 0)
def resolve_is_in_wishlist(self, info):
env = info.context["env"]
is_in_wishlist = product_is_in_wishlist(env, self)
return bool(is_in_wishlist)
def resolve_media_gallery(self, info):
if self._name == 'product.template':
return self.product_template_image_ids or None
else:
return self.product_template_image_ids + self.product_variant_image_ids or None
def resolve_qty(self, info):
return self.free_qty
def resolve_slug(self, info):
return self.website_slug
def resolve_alternative_products(self, info):
return self.alternative_product_ids or None
def resolve_accessory_products(self, info):
return self.accessory_product_ids or None
# Specific to use in Product Variant
def resolve_combination_info_variant(self, info):
env = info.context["env"]
pricing_info = get_product_pricing_info(env, self)
return pricing_info or None
def resolve_variant_price(self, info):
env = info.context["env"]
pricing_info = get_product_pricing_info(env, self)
return pricing_info['list_price'] or None
def resolve_variant_price_after_discount(self, info):
env = info.context["env"]
pricing_info = get_product_pricing_info(env, self)
return pricing_info['price'] or None
def resolve_variant_has_discounted_price(self, info):
env = info.context["env"]
pricing_info = get_product_pricing_info(env, self)
return pricing_info['has_discounted_price']
def resolve_is_variant_possible(self, info):
return self._is_variant_possible()
def resolve_variant_attribute_values(self, info):
return self.product_template_attribute_value_ids or None
def resolve_product_template(self, info):
return self.product_tmpl_id or None
# Specific to use in Product Template
def resolve_combination_info(self, info):
env = info.context["env"]
pricing_info = get_product_pricing_info(env, self.product_variant_id)
return pricing_info or None
def resolve_price(self, info):
return self.list_price or None
def resolve_attribute_values(self, info):
return self.attribute_line_ids.product_template_value_ids or None
def resolve_product_variants(self, info):
return self.product_variant_ids or None
def resolve_first_variant(self, info):
return self.product_variant_id or None
def resolve_json_ld(self, info):
return self.get_json_ld()
class Payment(OdooObjectType):
id = graphene.Int()
name = graphene.String()
amount = graphene.Float()
payment_reference = graphene.String()
class PaymentTransaction(OdooObjectType):
id = graphene.Int()
reference = graphene.String()
payment = graphene.Field(lambda: Payment)
amount = graphene.Float()
currency = graphene.Field(lambda: Currency)
acquirer = graphene.String()
acquirer_reference = graphene.String()
company = graphene.Field(lambda: Partner)
customer = graphene.Field(lambda: Partner)
state = PaymentTransactionState()
def resolve_payment(self, info):
return self.payment_id or None
def resolve_currency(self, info):
return self.currency_id or None
def resolve_acquirer(self, info):
return self.acquirer_id.name or None
def resolve_company(self, info):
return self.acquirer_id.company_id or None
def resolve_customer(self, info):
return self.partner_id or None
class OrderLine(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
product = graphene.Field(lambda: Product)
quantity = graphene.Float()
price_unit = graphene.Float()
price_subtotal = graphene.Float()
price_total = graphene.Float()
price_tax = graphene.Float()
warning_stock = graphene.String()
def resolve_product(self, info):
return self.product_id or None
def resolve_quantity(self, info):
return self.product_uom_qty or None
class Coupon(OdooObjectType):
id = graphene.Int(required=True)
code = graphene.String()
class ShippingMethod(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
price = graphene.Float()
def resolve_price(self, info):
website = self.env['website'].get_current_website()
request.website = website
order = website.sale_get_order(force_create=True)
return self.rate_shipment(order)['price'] if self.free_over else self.fixed_price
class Order(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
partner = graphene.Field(lambda: Partner)
partner_shipping = graphene.Field(lambda: Partner)
partner_invoice = graphene.Field(lambda: Partner)
date_order = graphene.String()
amount_untaxed = graphene.Float()
amount_tax = graphene.Float()
amount_total = graphene.Float()
amount_delivery = graphene.Float()
currency_rate = graphene.String()
shipping_method = graphene.Field(lambda: ShippingMethod)
currency = graphene.Field(lambda: Currency)
order_lines = graphene.List(graphene.NonNull(lambda: OrderLine))
website_order_line = graphene.List(graphene.NonNull(lambda: OrderLine))
stage = OrderStage()
order_url = graphene.String()
transactions = graphene.List(graphene.NonNull(lambda: PaymentTransaction))
last_transaction = graphene.Field(lambda: PaymentTransaction)
client_order_ref = graphene.String()
invoice_status = InvoiceStatus()
invoice_count = graphene.Int()
coupons = graphene.List(graphene.NonNull(lambda: Coupon))
cart_quantity = graphene.Int()
def resolve_partner(self, info):
return self.partner_id or None
def resolve_partner_shipping(self, info):
return self.partner_shipping_id or None
def resolve_partner_invoice(self, info):
return self.partner_invoice_id or None
def resolve_date_order(self, info):
return self.date_order or None
def resolve_shipping_method(self, info):
return self.carrier_id or None
def resolve_currency(self, info):
return self.currency_id or None
def resolve_order_lines(self, info):
return self.order_line or None
def resolve_stage(self, info):
return self.state or None
def resolve_order_url(self, info):
return self.get_portal_url()
def resolve_transactions(self, info):
return self.transaction_ids or None
def resolve_last_transaction(self, info):
if self.transaction_ids:
return self.transaction_ids.sorted(key=lambda r: r.create_date, reverse=True)[0]
return None
def resolve_coupons(self, info):
return self.applied_coupon_ids or None
def resolve_cart_quantity(self, info):
return self.cart_quantity or None
class InvoiceLine(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
product = graphene.Field(lambda: Product)
quantity = graphene.Float()
price_unit = graphene.Float()
price_subtotal = graphene.Float()
price_total = graphene.Float()
def resolve_product(self, info):
return self.product_id or None
class Invoice(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
partner = graphene.Field(lambda: Partner)
partner_shipping = graphene.Field(lambda: Partner)
invoice_date = graphene.String()
invoice_date_due = graphene.String()
amount_untaxed = graphene.Float()
amount_tax = graphene.Float()
amount_total = graphene.Float()
amount_residual = graphene.Float()
currency = graphene.Field(lambda: Currency)
invoice_lines = graphene.List(graphene.NonNull(lambda: InvoiceLine))
state = InvoiceState()
invoice_url = graphene.String()
transactions = graphene.List(graphene.NonNull(lambda: PaymentTransaction))
def resolve_partner(self, info):
return self.partner_id or None
def resolve_partner_shipping(self, info):
return self.partner_shipping_id or None
def resolve_invoice_date(self, info):
return self.invoice_date or None
def resolve_invoice_date_due(self, info):
return self.invoice_date_due or None
def resolve_currency(self, info):
return self.currency_id or None
def resolve_invoice_lines(self, info):
return self.invoice_line_ids or None
def resolve_state(self, info):
return self.state or None
def resolve_invoice_url(self, info):
return self.get_portal_url()
def resolve_transactions(self, info):
return self.transaction_ids or None
class WishlistItem(OdooObjectType):
id = graphene.Int(required=True)
partner = graphene.Field(lambda: Partner)
product = graphene.Field(lambda: Product)
def resolve_partner(self, info):
return self.partner_id or None
def resolve_product(self, info):
return self.product_id or None
class PaymentIcon(OdooObjectType):
id = graphene.ID()
name = graphene.String(required=True)
image = graphene.String()
def resolve_image(self, info):
return '/web/image/payment.icon/{}/image'.format(self.id)
class PaymentAcquirer(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
display_as = graphene.String()
provider = graphene.String()
payment_icons = graphene.List(graphene.NonNull(lambda: PaymentIcon))
def resolve_payment_icons(self, info):
return self.payment_icon_ids or None
class MailingList(OdooObjectType):
id = graphene.Int(required=True)
name = graphene.String()
class MailingContactSubscription(OdooObjectType):
id = graphene.Int(required=True)
mailing_list = graphene.Field(lambda: MailingList)
opt_out = graphene.Boolean()
def resolve_mailing_list(self, info):
return self.list_id or None
class MailingContact(OdooObjectType):
id = graphene.Int()
name = graphene.String()
email = graphene.String()
company_name = graphene.String()
subscription_list = graphene.List(graphene.NonNull(lambda: MailingContactSubscription))
def resolve_country(self, info):
return self.country_id or None
def resolve_industry(self, info):
return self.industry_id or None
def resolve_subscription_list(self, info):
return self.subscription_list_ids or None