create a ProcessorFactory

This commit is contained in:
Alejandro Nunez Capote 2019-06-10 12:15:14 -04:00
parent fa0de7b334
commit 3778806bae
2 changed files with 31 additions and 26 deletions

View File

@ -1,6 +1,6 @@
"""Filters to ElasticSearch"""
from graphene import String, Boolean, Int
from graphene_django.elasticsearch.filter.processors import PROCESSORS
from graphene_django.elasticsearch.filter.processors import ProcessorFactory
class FilterES(object):
@ -31,26 +31,14 @@ class FilterES(object):
self.processor = None
if self.lookup_expressions:
for variant in self.lookup_expressions:
if variant in PROCESSORS:
self.processor = self.build_processor(variant)
else:
raise ValueError('We do not have processor: %s.' % variant)
self.processor = ProcessorFactory.make_processor(variant, self, self.processor)
else:
self.processor = self.build_processor(self.default_processor)
self.processor = ProcessorFactory.make_processor(self.default_processor, self, self.processor)
self.argument = argument or self.default_argument
self.fields = self.processor.generate_field()
def build_processor(self, variant):
"""
Create a new processor based on the name
:param variant: Processor name
:return: Returns a Processor instance
"""
processor_class = PROCESSORS[variant]
return processor_class(self, self.processor)
def attach_processor(self, observer):
"""
Generating a query based on the arguments passed to graphene field

View File

@ -234,7 +234,8 @@ class GteProcessor(Processor):
return Q("range", **{name: {'gte': value}})
PROCESSORS = {
class ProcessorFactory(object):
processors = {
"contains": ContainsProcessor,
"term": TermProcessor,
"regex": RegexProcessor,
@ -244,4 +245,20 @@ PROCESSORS = {
"exits": ExitsProcessor,
"lte": LteProcessor,
"gte": GteProcessor,
}
}
@classmethod
def make_processor(cls, variant, filter_es, parent_processor):
"""
Create a new processor based on the name
:param variant: Processor name
:param filter_es: Target filter
:param parent_processor: Parent in the chain
:return: Returns a Processor instance
"""
if variant in cls.processors:
processor_class = cls.processors[variant]
return processor_class(filter_es, parent_processor)
else:
raise ValueError('We do not have processor: %s.' % variant)