Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import json
import graphene
from graphql_relay.node.node import from_global_id
from companies import models
from companies.schema import CompanyNode, BrandNode
from franchise.utils import get_instance
class CompanyInput(graphene.InputObjectType):
name = graphene.String(description='Name of your company')
email = graphene.String(description='Email of your company')
phone_number = graphene.String(description='Phone number of your company')
director = graphene.String(description='Director of your company')
franchise_head = graphene.String(description='Franchise Head of your company')
class BrandInput(graphene.InputObjectType):
company = graphene.List(CompanyInput)
name = graphene.String()
website = graphene.String()
description = graphene.String()
country = graphene.String()
city = graphene.String()
address = graphene.String()
pin_code = graphene.String()
operation_date = graphene.String()
franchising_date = graphene.String()
number_of_franchises = graphene.String()
number_of_outlets = graphene.String()
class CreateCompany(graphene.Mutation):
class Arguments:
input = CompanyInput(description="These fields are required", required=True)
class Meta:
description = "Creates a new company"
# model = models.Company
errors = graphene.String()
company = graphene.Field(CompanyNode)
@staticmethod
def mutate(root, info, input=None):
if not info.context.user.is_authenticated:
return CreateCompany(errors=json.dumps('Please Login to list your company'))
company = models.Company.objects.create(owner=info.context.user,
name=input.name,
email=input.email,
phone_number=input.phone_number,
director=input.director,
franchise_head=input.franchise_head)
return CreateCompany(company=company, errors=None)
class UpdateCompany(graphene.Mutation):
class Arguments:
input = CompanyInput(description="These fields are required", required=True)
id = graphene.String(required=True)
class Meta:
description = "Update an existing company"
# model = models.Company
errors = graphene.String()
company = graphene.Field(CompanyNode)
@staticmethod
def mutate(root, info, id=None, input=None):
if not info.context.user.is_authenticated:
return UpdateCompany(errors=json.dumps('Please Login to continue'))
try:
company_instance = get_instance(models.Company, id)
if company_instance:
company_instance.owner=info.context.user
company_instance.name=input.get('name'),
company_instance.email=input.email,
company_instance.phone_number=input.phone_number,
company_instance.director=input.director,
company_instance.franchise_head=input.franchise_head
company_instance.save()
print('company instance after saving', company_instance)
return UpdateCompany(company=company_instance)
except models.Company.DoesNotExist:
return UpdateCompany(errors=json.dumps('No Company Exist to Update'))
class CreateBrand(graphene.Mutation):
class Arguments:
input = BrandInput(description="These fields are required", required=True)
slug = graphene.String(description="Slug of company the brand belongs to", required=True)
class Meta:
description = "Create Brand Mutation"
error = graphene.String()
brand = graphene.Node(BrandNode)
@staticmethod
def mutate(root, info, **args):
print('args', **args, args)
if not info.context.user.is_authenticated:
return CreateBrand(errors=json.dumps('Please Login to list your brand'))
try:
company = models.Company.objects.get(slug=args.get('slug'))
if company:
brand = models.Brand.objects.create(
company=company,
name=args.get('company')['name'],
slug = args.get('company')['slug'],
website = args.get('company')['website'],
description = args.get('company')['description'],
country = args.get('company')['country'],
city = args.get('company')['city'],
address = args.get('company')['address'],
pin_code = args.get('company')['pin_code'],
operation_date = args.get('company')['operation_date'],
franchising_date = args.get('company')['franchising_date'],
number_of_franchises = args.get('company')['number_of_franchises'],
number_of_outlets = args.get('company')['number_of_outlets']
)
return CreateBrand(brand=brand, errors=null)
except models.Company.DoesNotExist:
return CreateBrand(errors=json.dumps('Company should be required'))
class Mutation(graphene.ObjectType):
create_company = CreateCompany.Field()
update_company = UpdateCompany.Field()
create_brand = CreateBrand.Field()
import graphene
# from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType
from companies.models import Industry, Segment, Company, Brand, BusinessModel
class IndustryNode(DjangoObjectType):
class Meta:
model = Industry
interfaces = (graphene.Node, )
filter_fields = ['name', ]
class SegmentNode(DjangoObjectType):
class Meta:
model = Segment
interfaces = (graphene.Node, )
filter_fields = ['name', ]
class CompanyNode(DjangoObjectType):
class Meta:
model = Company
interfaces = (graphene.Node, )
filter_fields = {
'name': ['exact', 'icontains', 'istartswith'],
}
class BrandNode(DjangoObjectType):
class Meta:
model = Brand
interfaces = (graphene.Node, )
class Query(object):
industry = graphene.Field(IndustryNode)
segment = graphene.Field(SegmentNode)
company = graphene.Field(CompanyNode, slug=graphene.String())
brand = graphene.Field(BrandNode, slug=graphene.String())
all_industries = graphene.List(IndustryNode)
all_segments = graphene.List(SegmentNode)
all_companies = graphene.List(CompanyNode)
all_brands = graphene.List(BrandNode)
@staticmethod
def resolve_all_industries(self, info, **kwargs):
return Industry.objects.all()
@staticmethod
def resolve_all_segments(self, info, **kwargs):
return Segment.objects.all()
@staticmethod
def resolve_all_companies(self, info, **kwargs):
return Company.objects.all()
@staticmethod
def resolve_all_brands(self, info, **kwargs):
return Brand.objects.all()
@staticmethod
def resolve_company(self, info, **kwargs):
print('info', info)
print('###########')
print('kwargs', kwargs, **kwargs)
slug = kwargs.get('slug')
if slug is not None:
return Company.objects.get(slug=slug)
return None
@staticmethod
def resolve_brand(self, info, **kwargs):
slug = kwargs.get('slug')
if slug is not None:
return Brand.objects.get(slug=slug)
return None
@staticmethod
def resolve_business_model(self, info, **kwargs):
slug = kwargs.get('slug')
if slug is not None:
return BusinessModel.objects.get(slug=slug)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.