def _default_options(cls): return [ ("provider", "default"), ("model", None), ("stream_name", inflection.underscore(cls.__name__)), ("schema_name", inflection.underscore(cls.__name__)), ]
def construct_and_register_celery_task(self, consumer_cls): """Constructs a Celery-compliant Task class and also registers Task with Celery App Arguments: consumer_cls {BaseSubscriber} -- The Subscriber or Command Handler class to be converted into a Celery Task Returns: ProteanTask -- Decorated and Registered Celery Task class """ attrs = consumer_cls.__dict__ custom_attrs = { "run": attrs["__call__"], # `notify` is the method to run on event "name": underscore(fully_qualified_name( consumer_cls)), # `name` will be the same as the task's queue } attrs = {**attrs, **custom_attrs} # Construct `decorated_cls` dynamically from `ProteanTask`. # `ProteanTask` acts as the base class for all celery tasks. decorated_cls = type(consumer_cls.__name__ + "Task", (ProteanTask, ), {**attrs}) # Register Task class with Celery app decorated_cls_instance = self.celery_app.register_task(decorated_cls()) # Add to Queue so that workers pick it up automatically self.queues.append(Queue(decorated_cls.name)) return decorated_cls_instance
def cache_for(self, view_cls): """Retrieve cache associated with the View""" if self._caches is None: self._initialize() view_provider = view_cls.meta_.provider cache = self.get(view_provider) view_name = underscore(view_cls.__name__) if view_name not in cache._views: cache.register_view(view_cls) return cache
def _process_request(self, usecase_cls, request_object_cls, payload, many=False, no_serialization=False): """ Process the request by running the Protean Tasklet """ # Get the schema class and derive resource name entity_cls = self.get_entity_cls() resource = inflection.underscore(entity_cls.__name__) # Get the serializer for this class serializer = None if not no_serialization: serializer = self.get_serializer(many=many) # Run the use case and return the results response_object = Tasklet.perform(entity_cls, usecase_cls, request_object_cls, payload, raise_error=True) # If no serialization is set just return the response object if no_serialization: return response_object.value # Return empty response for 204s if response_object.code == Status.SUCCESS_WITH_NO_CONTENT: return Response(None, response_object.code.value) # Serialize the results and return the response if many: items = serializer.dump(response_object.value.items) page = int( response_object.value.offset / response_object.value.limit) + 1 result = { INFLECTOR.plural(resource): items.data, 'total': response_object.value.total, 'page': page, } return result, response_object.code.value else: result = serializer.dump(response_object.value) return {resource: result.data}, response_object.code.value
def __init__(self, entity_name, meta): self.abstract = getattr(meta, "abstract", None) or False self.schema_name = getattr(meta, "schema_name", None) or inflection.underscore(entity_name) self.provider = getattr(meta, "provider", None) or "default" self.model = getattr(meta, "model", None) # `order_by` can be provided either as a string or a tuple ordering = getattr(meta, "order_by", ()) if isinstance(ordering, str): self.order_by = (ordering, ) else: self.order_by = tuple(ordering) # Initialize Options self.declared_fields = {} self.value_object_fields = {} self.reference_fields = {} self.id_field = None # Domain Attributes self.aggregate_cls = getattr(meta, "aggregate_cls", None)
def __init__(self, entity_name, meta): self.abstract = getattr(meta, 'abstract', None) or False self.schema_name = (getattr(meta, 'schema_name', None) or inflection.underscore(entity_name)) self.provider = getattr(meta, 'provider', None) or 'default' self.model = getattr(meta, 'model', None) # `order_by` can be provided either as a string or a tuple ordering = getattr(meta, 'order_by', ()) if isinstance(ordering, str): self.order_by = (ordering, ) else: self.order_by = tuple(ordering) # Initialize Options self.declared_fields = {} self.value_object_fields = {} self.reference_fields = {} self.id_field = None # Domain Attributes self.aggregate_cls = getattr(meta, 'aggregate_cls', None) self.bounded_context = getattr(meta, 'bounded_context', None)
def _default_options(cls): return [ ("stream_name", inflection.underscore(cls.__name__)), ]