Skip to content

Commit

Permalink
OM-137 Fixed subject, third party and line resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
malinowskikam committed Mar 7, 2024
1 parent 928459b commit b86d872
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 134 deletions.
120 changes: 52 additions & 68 deletions invoice/gql/gql_types/bill_types.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,65 @@
import graphene
import json
from django.contrib.contenttypes.models import ContentType
from django.core.serializers.json import DjangoJSONEncoder
from graphene_django import DjangoObjectType

from core import prefix_filterset, ExtendedConnection
from invoice.apps import InvoiceConfig
from invoice.gql.filter_mixin import GenericFilterGQLTypeMixin
from invoice.models import Bill, \
BillItem, BillEvent, BillPayment
from invoice.utils import underscore_to_camel
from location.models import Location
from product.models import Product
from django.core.exceptions import PermissionDenied
from django.utils.translation import gettext as _


class BillGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

subject = graphene.JSONString()
subject_type = graphene.Int()
subject_type_name = graphene.String()
thirdparty = graphene.JSONString()
thirdparty_type = graphene.Int()
thirdparty_type_name = graphene.String()

def resolve_subject_type(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.subject_type.id
if root.subject_type:
return root.subject_type.id

subject_type_name = graphene.String()
def resolve_subject_type_name(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.subject_type.name
if root.subject_type:
return root.subject_type.name

thirdparty_type = graphene.Int()
def resolve_thirdparty_type(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.thirdparty_type.id
if root.thirdparty_type:
return root.thirdparty_type.id

thirdparty_type_name = graphene.String()
def resolve_thirdparty_type_name(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.thirdparty_type.name
if root.thirdparty_type:
return root.thirdparty_type.name

subject = graphene.JSONString()
def resolve_subject(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
subject_object_dict = root.subject.__dict__
subject_object_dict.pop('_state')
subject_object_dict = {
underscore_to_camel(k): v for k, v in list(subject_object_dict.items())
}
if root.subject_type.name == "batch run":
location = Location.objects.filter(id=subject_object_dict['locationId'], validity_to__isnull=True)
location = location.values('code', 'name')
subject_object_dict['location'] = {
underscore_to_camel(k): v for k, v in location.first().items()
if root.subject:
subject_object_dict = root.subject.__dict__
subject_object_dict.pop('_state')
subject_object_dict = {
underscore_to_camel(k): v for k, v in list(subject_object_dict.items())
}
subject_object_dict = json.dumps(subject_object_dict, cls=DjangoJSONEncoder)
return subject_object_dict
if root.subject_type.name == "batch run":
location = Location.objects.filter(id=subject_object_dict['locationId'], validity_to__isnull=True)
location = location.values('code', 'name')
subject_object_dict['location'] = {
underscore_to_camel(k): v for k, v in location.first().items()
}
subject_object_dict = json.dumps(subject_object_dict, cls=DjangoJSONEncoder)
return subject_object_dict

thirdparty = graphene.JSONString()
def resolve_thirdparty(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
thirdparty_object_dict = root.thirdparty.__dict__
thirdparty_object_dict.pop('_state')
thirdparty_object_dict = {
underscore_to_camel(k): v for k, v in list(thirdparty_object_dict.items())
}
thirdparty_object_dict = json.dumps(thirdparty_object_dict, cls=DjangoJSONEncoder)
return thirdparty_object_dict
if root.thirdparty:
thirdparty_object_dict = root.thirdparty.__dict__
thirdparty_object_dict.pop('_state')
thirdparty_object_dict = {
underscore_to_camel(k): v for k, v in list(thirdparty_object_dict.items())
}
thirdparty_object_dict = json.dumps(thirdparty_object_dict, cls=DjangoJSONEncoder)
return thirdparty_object_dict

class Meta:
model = Bill
Expand All @@ -88,32 +77,29 @@ def get_queryset(cls, queryset, info):


class BillItemGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

line_type = graphene.Int()
line_type_name = graphene.String()
line = graphene.JSONString()

def resolve_line_type(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.line_type.id
if root.line_type:
return root.line_type.id

line_type_name = graphene.String()
def resolve_line_type_name(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.line_type.name
if root.line_type:
return root.line_type.name

line = graphene.JSONString()
def resolve_line(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_bill_search_perms):
raise PermissionDenied(_("unauthorized"))
line_object_dict = root.line.__dict__
line_object_dict.pop('_state')
key_values = list(line_object_dict.items())
line_object_dict.clear()
for k, v in key_values:
new_key = underscore_to_camel(k)
line_object_dict[new_key] = v
line_object_dict = json.dumps(line_object_dict, cls=DjangoJSONEncoder)
return line_object_dict
if root.line:
line_object_dict = root.line.__dict__
line_object_dict.pop('_state')
key_values = list(line_object_dict.items())
line_object_dict.clear()
for k, v in key_values:
new_key = underscore_to_camel(k)
line_object_dict[new_key] = v
line_object_dict = json.dumps(line_object_dict, cls=DjangoJSONEncoder)
return line_object_dict

class Meta:
model = BillItem
Expand All @@ -131,7 +117,6 @@ def get_queryset(cls, queryset, info):


class BillPaymentGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

class Meta:
model = BillPayment
interfaces = (graphene.relay.Node,)
Expand All @@ -148,7 +133,6 @@ def get_queryset(cls, queryset, info):


class BillEventGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

class Meta:
model = BillEvent
interfaces = (graphene.relay.Node,)
Expand Down
119 changes: 53 additions & 66 deletions invoice/gql/gql_types/invoice_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,62 @@

from core import prefix_filterset, ExtendedConnection
from insuree.models import Insuree
from invoice.apps import InvoiceConfig
from invoice.gql.filter_mixin import GenericFilterGQLTypeMixin
from invoice.models import Invoice, InvoiceLineItem, InvoicePayment, InvoiceEvent, InvoiceMutation, \
InvoicePaymentMutation, InvoiceLineItemMutation, InvoiceEventMutation
from invoice.utils import underscore_to_camel
from django.core.exceptions import PermissionDenied
from django.utils.translation import gettext as _


class InvoiceGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

subject = graphene.JSONString()
subject_type = graphene.Int()
subject_type_name = graphene.String()
thirdparty = graphene.JSONString()
thirdparty_type = graphene.Int()
thirdparty_type_name = graphene.String()

def resolve_subject_type(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.subject_type.id
if root.subject_type:
return root.subject_type.id

subject_type_name = graphene.String()
def resolve_subject_type_name(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.subject_type.name
if root.subject_type:
return root.subject_type.name

thirdparty_type = graphene.Int()
def resolve_thirdparty_type(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.thirdparty_type.id
if root.thirdparty_type:
return root.thirdparty_type.id

thirdparty_type_name = graphene.String()
def resolve_thirdparty_type_name(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.thirdparty_type.name
if root.thirdparty_type:
return root.thirdparty_type.name

subject = graphene.JSONString()
def resolve_subject(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
subject_object_dict = root.subject.__dict__
subject_object_dict.pop('_state')
subject_object_dict = {
underscore_to_camel(k): v for k, v in list(subject_object_dict.items())
}
if root.subject_type.name == "family":
insuree = Insuree.objects.filter(id=subject_object_dict['headInsureeId'], validity_to__isnull=True)
insuree = insuree.values('id', 'chf_id', 'uuid', 'last_name', 'other_names')
subject_object_dict['headInsuree'] = {
underscore_to_camel(k): v for k, v in insuree.first().items()
if root.subject:
subject_object_dict = root.subject.__dict__
subject_object_dict.pop('_state')
subject_object_dict = {
underscore_to_camel(k): v for k, v in list(subject_object_dict.items())
}
subject_object_dict = json.dumps(subject_object_dict, cls=DjangoJSONEncoder)
return subject_object_dict
if root.subject_type.name == "family":
insuree = Insuree.objects.filter(id=subject_object_dict['headInsureeId'], validity_to__isnull=True)
insuree = insuree.values('id', 'chf_id', 'uuid', 'last_name', 'other_names')
subject_object_dict['headInsuree'] = {
underscore_to_camel(k): v for k, v in insuree.first().items()
}
subject_object_dict = json.dumps(subject_object_dict, cls=DjangoJSONEncoder)
return subject_object_dict

thirdparty = graphene.JSONString()
def resolve_thirdparty(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
thirdparty_object_dict = root.thirdparty.__dict__
thirdparty_object_dict.pop('_state')
thirdparty_object_dict = {
underscore_to_camel(k): v for k, v in list(thirdparty_object_dict.items())
}
thirdparty_object_dict = json.dumps(thirdparty_object_dict, cls=DjangoJSONEncoder)
return thirdparty_object_dict
if root.thirdparty:
thirdparty_object_dict = root.thirdparty.__dict__

thirdparty_object_dict.pop('_state')
thirdparty_object_dict = {
underscore_to_camel(k): v for k, v in list(thirdparty_object_dict.items())
}
thirdparty_object_dict = json.dumps(thirdparty_object_dict, cls=DjangoJSONEncoder)
return thirdparty_object_dict

class Meta:
model = Invoice
Expand All @@ -86,32 +78,29 @@ def get_queryset(cls, queryset, info):


class InvoiceLineItemGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

line = graphene.JSONString()
line_type = graphene.Int()
line_type_name = graphene.String()

def resolve_line_type(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.line_type.id
if root.line_type:
return root.line_type.id

line_type_name = graphene.String()
def resolve_line_type_name(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
return root.line_type.name
if root.line_type:
return root.line_type.name

line = graphene.JSONString()
def resolve_line(root, info):
if not info.context.user.has_perms(InvoiceConfig.gql_invoice_search_perms):
raise PermissionDenied(_("unauthorized"))
line_object_dict = root.line.__dict__
line_object_dict.pop('_state')
key_values = list(line_object_dict.items())
line_object_dict.clear()
for k, v in key_values:
new_key = underscore_to_camel(k)
line_object_dict[new_key] = v
line_object_dict = json.dumps(line_object_dict, cls=DjangoJSONEncoder)
return line_object_dict
if root.line:
line_object_dict = root.line.__dict__
line_object_dict.pop('_state')
key_values = list(line_object_dict.items())
line_object_dict.clear()
for k, v in key_values:
new_key = underscore_to_camel(k)
line_object_dict[new_key] = v
line_object_dict = json.dumps(line_object_dict, cls=DjangoJSONEncoder)
return line_object_dict

class Meta:
model = InvoiceLineItem
Expand All @@ -129,7 +118,6 @@ def get_queryset(cls, queryset, info):


class InvoicePaymentGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

class Meta:
model = InvoicePayment
interfaces = (graphene.relay.Node,)
Expand All @@ -146,7 +134,6 @@ def get_queryset(cls, queryset, info):


class InvoiceEventGQLType(DjangoObjectType, GenericFilterGQLTypeMixin):

class Meta:
model = InvoiceEvent
interfaces = (graphene.relay.Node,)
Expand Down

0 comments on commit b86d872

Please sign in to comment.