pumpwood_communication.microservice_abc.parallel

Module for parallel requests.

 1"""Module for parallel requests."""
 2from .action import ABCParallelActionMicroservice
 3from .delete import ABCParallelDeleteMicroservice
 4from .list import ABCParallelListMicroservice
 5from .retrieve import ABCParallelRetriveMicroservice
 6from .save import ABCParallelSaveMicroservice
 7from .batch import ABCParallelBatchMicroservice
 8
 9
10__docformat__ = "google"
11
12
13__all__ = [
14    ABCParallelActionMicroservice,
15    ABCParallelDeleteMicroservice,
16    ABCParallelListMicroservice,
17    ABCParallelRetriveMicroservice,
18    ABCParallelSaveMicroservice,
19    ABCParallelBatchMicroservice
20]
class ABCParallelActionMicroservice(pumpwood_communication.microservice_abc.parallel.base.ABCParallelBaseMicroservice):
 8class ABCParallelActionMicroservice(ABCParallelBaseMicroservice):
 9    """Abstract class for parallel calls at Pumpwood end-points."""
10
11    def parallel_execute_action(self, model_class: Union[str, list[str]],
12                                list_pk: list[int],
13                                action: str | list[str],
14                                parameters: dict | list[dict] = {},
15                                n_parallel: int = None,
16                                auth_header: dict = None,
17                                base_filter_skip: list[str] | list[list[str]] = None # NOQA
18                                ) -> list[dict]:
19        """Make [n_parallel] parallel execute_action requests.
20
21        Args:
22            model_class:
23                Model Class to perform action over,
24                or a list of model class o make diferent actions.
25            list_pk:
26                A list of the pks to perform action or a
27                single pk to perform action with different paraemters.
28            action:
29                A list of actions to perform or a single
30                action to perform over all pks and parameters.
31            parameters:
32                Parameters used to perform actions
33                or a single dict to be used in all actions.
34            n_parallel:
35                Number of simultaneous requests. If not set, uses
36                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
37                (legacy spelling supported), default 4.
38            auth_header:
39                Auth header to substitute the microservice original
40                at the request (user impersonation).
41            base_filter_skip (list[str] | list[list[str]]):
42                List of base query filter to be skiped, it is necessary to
43                be superuser to skip base query filters.
44
45        Returns:
46            list of the execute_action request data.
47
48        Raises:
49            PumpWoodException:
50                'parallel_length != len([argument])'. Indicates that function
51                arguments does not have all the same length.
52
53        Example:
54            No example yet.
55        """
56        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
57
58        list_model_class = self.convert_to_list(
59            argument=model_class, length=len(list_pk))
60        list_action = self.convert_to_list(
61            argument=action, length=len(list_pk))
62        list_parameters = self.convert_to_list(
63            argument=parameters, length=len(list_pk))
64        list_auth_header = self.convert_to_list(
65            argument=auth_header, length=len(list_pk))
66        list_base_filter_skip = self.convert_to_list(
67            argument=base_filter_skip, length=len(list_pk),
68            force_replicate=True)
69        column_arg = {
70            'model_class': list_model_class,
71            'action': list_action,
72            'parameters': list_parameters,
73            'auth_header': list_auth_header,
74            'base_filter_skip': list_base_filter_skip}
75
76        function_args = self.transpose_args(dict_list=column_arg)
77        return self.parallel_call(
78            function=self.execute_action, function_args=function_args,
79            n_parallel=n_parallel)

Abstract class for parallel calls at Pumpwood end-points.

def parallel_execute_action( self, model_class: Union[str, list[str]], list_pk: list[int], action: str | list[str], parameters: dict | list[dict] = {}, n_parallel: int = None, auth_header: dict = None, base_filter_skip: list[str] | list[list[str]] = None) -> list[dict]:
11    def parallel_execute_action(self, model_class: Union[str, list[str]],
12                                list_pk: list[int],
13                                action: str | list[str],
14                                parameters: dict | list[dict] = {},
15                                n_parallel: int = None,
16                                auth_header: dict = None,
17                                base_filter_skip: list[str] | list[list[str]] = None # NOQA
18                                ) -> list[dict]:
19        """Make [n_parallel] parallel execute_action requests.
20
21        Args:
22            model_class:
23                Model Class to perform action over,
24                or a list of model class o make diferent actions.
25            list_pk:
26                A list of the pks to perform action or a
27                single pk to perform action with different paraemters.
28            action:
29                A list of actions to perform or a single
30                action to perform over all pks and parameters.
31            parameters:
32                Parameters used to perform actions
33                or a single dict to be used in all actions.
34            n_parallel:
35                Number of simultaneous requests. If not set, uses
36                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
37                (legacy spelling supported), default 4.
38            auth_header:
39                Auth header to substitute the microservice original
40                at the request (user impersonation).
41            base_filter_skip (list[str] | list[list[str]]):
42                List of base query filter to be skiped, it is necessary to
43                be superuser to skip base query filters.
44
45        Returns:
46            list of the execute_action request data.
47
48        Raises:
49            PumpWoodException:
50                'parallel_length != len([argument])'. Indicates that function
51                arguments does not have all the same length.
52
53        Example:
54            No example yet.
55        """
56        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
57
58        list_model_class = self.convert_to_list(
59            argument=model_class, length=len(list_pk))
60        list_action = self.convert_to_list(
61            argument=action, length=len(list_pk))
62        list_parameters = self.convert_to_list(
63            argument=parameters, length=len(list_pk))
64        list_auth_header = self.convert_to_list(
65            argument=auth_header, length=len(list_pk))
66        list_base_filter_skip = self.convert_to_list(
67            argument=base_filter_skip, length=len(list_pk),
68            force_replicate=True)
69        column_arg = {
70            'model_class': list_model_class,
71            'action': list_action,
72            'parameters': list_parameters,
73            'auth_header': list_auth_header,
74            'base_filter_skip': list_base_filter_skip}
75
76        function_args = self.transpose_args(dict_list=column_arg)
77        return self.parallel_call(
78            function=self.execute_action, function_args=function_args,
79            n_parallel=n_parallel)

Make [n_parallel] parallel execute_action requests.

Arguments:
  • model_class: Model Class to perform action over, or a list of model class o make diferent actions.
  • list_pk: A list of the pks to perform action or a single pk to perform action with different paraemters.
  • action: A list of actions to perform or a single action to perform over all pks and parameters.
  • parameters: Parameters used to perform actions or a single dict to be used in all actions.
  • n_parallel: Number of simultaneous requests. If not set, uses PUMPWOOD_COMMUNICATION__N_PARALLEL env variable (legacy spelling supported), default 4.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • base_filter_skip (list[str] | list[list[str]]): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
Returns:

list of the execute_action request data.

Raises:
  • PumpWoodException: 'parallel_length != len([argument])'. Indicates that function arguments does not have all the same length.
Example:

No example yet.

class ABCParallelDeleteMicroservice(pumpwood_communication.microservice_abc.parallel.base.ABCParallelBaseMicroservice):
  8class ABCParallelDeleteMicroservice(ABCParallelBaseMicroservice):
  9    """Abstract class for parallel calls at Pumpwood end-points."""
 10
 11    def parallel_delete(self, model_class: Union[str, list[str]],
 12                        list_pk: list[int], n_parallel: int = None,
 13                        auth_header: dict = None,
 14                        base_filter_skip: list[str] | list[list[str]] = None):
 15        """Make many [n_parallel] delete requests.
 16
 17        Args:
 18            model_class:
 19                Model Class to list one.
 20            list_pk:
 21                List of the pks to list one.
 22            n_parallel:
 23                Number of simultaneous requests. If not set, uses
 24                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
 25                (legacy spelling supported), default 4.
 26            auth_header:
 27                Auth header to substitute the microservice original
 28                at the request (user impersonation).
 29            base_filter_skip (list):
 30                List of base query filter to be skiped, it is necessary to
 31                be superuser to skip base query filters.
 32
 33        Returns:
 34            List of the delete request data.
 35
 36        Raises:
 37            PumpWoodException:
 38                'len(model_class)[{}] != len(list_args)[{}]'. Indicates
 39                that length of model_class and list_args arguments are not
 40                equal.
 41        """
 42        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 43
 44        list_model_class = self.convert_to_list(
 45            argument=model_class, length=len(list_pk))
 46        list_auth_header = self.convert_to_list(
 47            argument=auth_header, length=len(list_pk))
 48        list_base_filter_skip = self.convert_to_list(
 49            argument=base_filter_skip, length=len(list_pk),
 50            force_replicate=True)
 51        column_arg = {
 52            'model_class': list_model_class,
 53            'auth_header': list_auth_header,
 54            'base_filter_skip': list_base_filter_skip,
 55            'pk': list_pk}
 56
 57        function_args = self.transpose_args(dict_list=column_arg)
 58        return self.parallel_call(
 59            function=self.delete, function_args=function_args,
 60            n_parallel=n_parallel)
 61
 62    def parallel_delete_many(self, model_class: Union[str, List[str]],
 63                             list_args: List[dict], n_parallel: int = None,
 64                             auth_header: dict = None,
 65                             base_filter_skip: list[str] | list[list[str]] = None # NOQA
 66                             ) -> List[dict]:
 67        """Make [n_parallel] parallel delete_many request.
 68
 69        Args:
 70            model_class (str):
 71                Model Class to delete many.
 72            list_args (list):
 73                A list of list request args (filter_dict, exclude_dict).
 74            n_parallel:
 75                Number of simultaneous requests. If not set, uses
 76                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
 77                (legacy spelling supported), default 4.
 78            auth_header:
 79                Auth header to substitute the microservice original
 80                at the request (user impersonation).
 81            base_filter_skip (list):
 82                List of base query filter to be skiped, it is necessary to
 83                be superuser to skip base query filters.
 84
 85        Returns:
 86            List of the delete many request reponses.
 87
 88        Raises:
 89            PumpWoodException:
 90                'len(model_class)[{}] != len(list_args)[{}]'. Indicates
 91                that length of model_class and list_args arguments
 92                are not equal.
 93
 94        Example:
 95            No example yet.
 96        """
 97        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 98
 99        dict_list = self.expand_list_args(
100            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
101        list_model_class = self.convert_to_list(
102            argument=model_class, length=len(list_args))
103        list_auth_header = self.convert_to_list(
104            argument=auth_header, length=len(list_args))
105        list_base_filter_skip = self.convert_to_list(
106            argument=base_filter_skip, length=len(list_args),
107            force_replicate=True)
108        column_arg = {
109            'model_class': list_model_class,
110            'auth_header': list_auth_header,
111            'base_filter_skip': list_base_filter_skip}
112        column_arg.update(dict_list)
113
114        function_args = self.transpose_args(dict_list=column_arg)
115        return self.parallel_call(
116            function=self.delete_many, function_args=function_args,
117            n_parallel=n_parallel)

Abstract class for parallel calls at Pumpwood end-points.

def parallel_delete( self, model_class: Union[str, list[str]], list_pk: list[int], n_parallel: int = None, auth_header: dict = None, base_filter_skip: list[str] | list[list[str]] = None):
11    def parallel_delete(self, model_class: Union[str, list[str]],
12                        list_pk: list[int], n_parallel: int = None,
13                        auth_header: dict = None,
14                        base_filter_skip: list[str] | list[list[str]] = None):
15        """Make many [n_parallel] delete requests.
16
17        Args:
18            model_class:
19                Model Class to list one.
20            list_pk:
21                List of the pks to list one.
22            n_parallel:
23                Number of simultaneous requests. If not set, uses
24                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
25                (legacy spelling supported), default 4.
26            auth_header:
27                Auth header to substitute the microservice original
28                at the request (user impersonation).
29            base_filter_skip (list):
30                List of base query filter to be skiped, it is necessary to
31                be superuser to skip base query filters.
32
33        Returns:
34            List of the delete request data.
35
36        Raises:
37            PumpWoodException:
38                'len(model_class)[{}] != len(list_args)[{}]'. Indicates
39                that length of model_class and list_args arguments are not
40                equal.
41        """
42        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
43
44        list_model_class = self.convert_to_list(
45            argument=model_class, length=len(list_pk))
46        list_auth_header = self.convert_to_list(
47            argument=auth_header, length=len(list_pk))
48        list_base_filter_skip = self.convert_to_list(
49            argument=base_filter_skip, length=len(list_pk),
50            force_replicate=True)
51        column_arg = {
52            'model_class': list_model_class,
53            'auth_header': list_auth_header,
54            'base_filter_skip': list_base_filter_skip,
55            'pk': list_pk}
56
57        function_args = self.transpose_args(dict_list=column_arg)
58        return self.parallel_call(
59            function=self.delete, function_args=function_args,
60            n_parallel=n_parallel)

Make many [n_parallel] delete requests.

Arguments:
  • model_class: Model Class to list one.
  • list_pk: List of the pks to list one.
  • n_parallel: Number of simultaneous requests. If not set, uses PUMPWOOD_COMMUNICATION__N_PARALLEL env variable (legacy spelling supported), default 4.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
Returns:

List of the delete request data.

Raises:
  • PumpWoodException: 'len(model_class)[{}] != len(list_args)[{}]'. Indicates that length of model_class and list_args arguments are not equal.
def parallel_delete_many( self, model_class: Union[str, List[str]], list_args: List[dict], n_parallel: int = None, auth_header: dict = None, base_filter_skip: list[str] | list[list[str]] = None) -> List[dict]:
 62    def parallel_delete_many(self, model_class: Union[str, List[str]],
 63                             list_args: List[dict], n_parallel: int = None,
 64                             auth_header: dict = None,
 65                             base_filter_skip: list[str] | list[list[str]] = None # NOQA
 66                             ) -> List[dict]:
 67        """Make [n_parallel] parallel delete_many request.
 68
 69        Args:
 70            model_class (str):
 71                Model Class to delete many.
 72            list_args (list):
 73                A list of list request args (filter_dict, exclude_dict).
 74            n_parallel:
 75                Number of simultaneous requests. If not set, uses
 76                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
 77                (legacy spelling supported), default 4.
 78            auth_header:
 79                Auth header to substitute the microservice original
 80                at the request (user impersonation).
 81            base_filter_skip (list):
 82                List of base query filter to be skiped, it is necessary to
 83                be superuser to skip base query filters.
 84
 85        Returns:
 86            List of the delete many request reponses.
 87
 88        Raises:
 89            PumpWoodException:
 90                'len(model_class)[{}] != len(list_args)[{}]'. Indicates
 91                that length of model_class and list_args arguments
 92                are not equal.
 93
 94        Example:
 95            No example yet.
 96        """
 97        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 98
 99        dict_list = self.expand_list_args(
100            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
101        list_model_class = self.convert_to_list(
102            argument=model_class, length=len(list_args))
103        list_auth_header = self.convert_to_list(
104            argument=auth_header, length=len(list_args))
105        list_base_filter_skip = self.convert_to_list(
106            argument=base_filter_skip, length=len(list_args),
107            force_replicate=True)
108        column_arg = {
109            'model_class': list_model_class,
110            'auth_header': list_auth_header,
111            'base_filter_skip': list_base_filter_skip}
112        column_arg.update(dict_list)
113
114        function_args = self.transpose_args(dict_list=column_arg)
115        return self.parallel_call(
116            function=self.delete_many, function_args=function_args,
117            n_parallel=n_parallel)

Make [n_parallel] parallel delete_many request.

Arguments:
  • model_class (str): Model Class to delete many.
  • list_args (list): A list of list request args (filter_dict, exclude_dict).
  • n_parallel: Number of simultaneous requests. If not set, uses PUMPWOOD_COMMUNICATION__N_PARALLEL env variable (legacy spelling supported), default 4.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
Returns:

List of the delete many request reponses.

Raises:
  • PumpWoodException: 'len(model_class)[{}] != len(list_args)[{}]'. Indicates that length of model_class and list_args arguments are not equal.
Example:

No example yet.

class ABCParallelListMicroservice(pumpwood_communication.microservice_abc.parallel.base.ABCParallelBaseMicroservice):
  8class ABCParallelListMicroservice(ABCParallelBaseMicroservice):
  9    """Abstract class for parallel calls at Pumpwood end-points."""
 10
 11    def parallel_list(self, model_class: Union[str, List[str]],
 12                      list_args: List[dict],
 13                      auth_header: dict = None, fields: list = None,
 14                      default_fields: bool = False, limit: int = None,
 15                      foreign_key_fields: bool = False,
 16                      base_filter_skip: list = None,
 17                      n_parallel: int = None,
 18                      flat_results: bool = True,
 19                      as_dataframe: bool = False) -> List[dict]:
 20        """List objects with pagination.
 21
 22        Args:
 23            model_class:
 24                Model class of the end-point
 25            list_args:
 26                filter_dict:
 27                    Filter dict to be used at the query. Filter elements from
 28                    query return that satifies all statements of the dictonary.
 29                exclude_dict:
 30                    Exclude dict to be used at the query. Remove elements from
 31                    query return that satifies all statements of the dictonary.
 32                order_by: Order results acording to list of strings
 33                    correspondent to fields. It is possible to use '-' at the
 34                    begginng of the field name for reverse ordering. Ex.:
 35                    ['description'] for accendent ordering and ['-description']
 36                    for descendent ordering.
 37            auth_header:
 38                Auth header to substitute the microservice original
 39                at the request (user impersonation).
 40            fields (list):
 41                Set the fields to be returned by the list end-point.
 42            default_fields (bool):
 43                Boolean, if true and fields arguments None will return the
 44                default fields set for list by the backend.
 45            limit (int):
 46                Set the limit of elements of the returned query. By default,
 47                backend usually return 50 elements.
 48            foreign_key_fields (bool):
 49                Return forenging key objects. It will return the fk
 50                corresponding object. Ex: `created_by_id` reference to
 51                a user `model_class` the correspondent to User will be
 52                returned at `created_by`.
 53            base_filter_skip (list):
 54                List of base query filter to be skiped, it is necessary to
 55                be superuser to skip base query filters.
 56            n_parallel (int):
 57                Number of parallel requests, if None will use the default
 58                one.
 59            flat_results (bool):
 60                The results will be joined on a single list.
 61            as_dataframe (bool):
 62                If True, returns the results as a pandas DataFrame.
 63
 64        Returns:
 65          Containing objects serialized by list Serializer.
 66
 67        Raises:
 68          No especific raises.
 69        """ # NOQA
 70        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 71
 72        # Convert arguments to list
 73        dict_list = self.expand_list_args(list_args=list_args)
 74        list_model_class = self.convert_to_list(
 75            argument=model_class, length=len(list_args))
 76        list_auth_header = self.convert_to_list(
 77            argument=auth_header, length=len(list_args))
 78        list_fields = self.convert_to_list(
 79            argument=fields, length=len(list_args),
 80            force_replicate=True)
 81        list_default_fields = self.convert_to_list(
 82            argument=default_fields, length=len(list_args))
 83        list_limit = self.convert_to_list(
 84            argument=limit, length=len(list_args))
 85        list_foreign_key_fields = self.convert_to_list(
 86            argument=foreign_key_fields, length=len(list_args))
 87        list_base_filter_skip = self.convert_to_list(
 88            argument=base_filter_skip, length=len(list_args),
 89            force_replicate=True)
 90        list_as_dataframe = self.convert_to_list(
 91            argument=as_dataframe, length=len(list_args),
 92            force_replicate=True)
 93
 94        column_arg = {
 95            'model_class': list_model_class,
 96            'auth_header': list_auth_header,
 97            'fields': list_fields,
 98            'default_fields': list_default_fields,
 99            'limit': list_limit,
100            'foreign_key_fields': list_foreign_key_fields,
101            'base_filter_skip': list_base_filter_skip,
102            "as_dataframe": list_as_dataframe
103        }
104        column_arg.update(dict_list)
105
106        function_args = self.transpose_args(dict_list=column_arg)
107        parallel_results = self.parallel_call(
108            function=self.list, function_args=function_args,
109            n_parallel=n_parallel)
110        if flat_results:
111            return self.flatten_parallel(parallel_results)
112        else:
113            return parallel_results
114
115    def parallel_list_without_pag(self, model_class: Union[str, List[str]],
116                                  list_args: List[dict],
117                                  auth_header: dict = None,
118                                  fields: list = None,
119                                  default_fields: bool = False,
120                                  foreign_key_fields: bool = False,
121                                  base_filter_skip: list = None,
122                                  n_parallel: int = None,
123                                  flat_results: bool = True,
124                                  as_dataframe: bool = False) -> List[dict]:
125        """List objects with pagination.
126
127        Args:
128            model_class:
129                Model class of the end-point
130            list_args:
131                filter_dict:
132                    Filter dict to be used at the query. Filter elements from
133                    query return that satifies all statements of the dictonary.
134                exclude_dict:
135                    Exclude dict to be used at the query. Remove elements from
136                    query return that satifies all statements of the dictonary.
137            auth_header:
138                Auth header to substitute the microservice original
139                at the request (user impersonation).
140            fields (list):
141                Set the fields to be returned by the list end-point.
142            default_fields (bool):
143                Boolean, if true and fields arguments None will return the
144                default fields set for list by the backend.
145            foreign_key_fields (bool):
146                Return forenging key objects. It will return the fk
147                corresponding object. Ex: `created_by_id` reference to
148                a user `model_class` the correspondent to User will be
149                returned at `created_by`.
150            base_filter_skip (list):
151                List of base query filter to be skiped, it is necessary to
152                be superuser to skip base query filters.
153            n_parallel (int):
154                Number of parallel requests, if None will use the default
155                one.
156            flat_results (bool):
157                The results will be joined on a single list.
158            as_dataframe (bool):
159                If True, returns the results as a pandas DataFrame.
160
161        Returns:
162          Containing objects serialized by list Serializer.
163
164        Raises:
165          No especific raises.
166        """ # NOQA
167        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
168
169        # Convert arguments to list
170        dict_list = self.expand_list_args(
171            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
172        list_model_class = self.convert_to_list(
173            argument=model_class, length=len(list_args))
174        list_auth_header = self.convert_to_list(
175            argument=auth_header, length=len(list_args))
176        list_fields = self.convert_to_list(
177            argument=fields, length=len(list_args),
178            force_replicate=True)
179        list_default_fields = self.convert_to_list(
180            argument=default_fields, length=len(list_args))
181        list_foreign_key_fields = self.convert_to_list(
182            argument=foreign_key_fields, length=len(list_args))
183        list_base_filter_skip = self.convert_to_list(
184            argument=base_filter_skip, length=len(list_args),
185            force_replicate=True)
186        list_as_dataframe = self.convert_to_list(
187            argument=as_dataframe, length=len(list_args),
188            force_replicate=True)
189
190        column_arg = {
191            'model_class': list_model_class,
192            'auth_header': list_auth_header,
193            'fields': list_fields,
194            'default_fields': list_default_fields,
195            'foreign_key_fields': list_foreign_key_fields,
196            'base_filter_skip': list_base_filter_skip,
197            "as_dataframe": list_as_dataframe
198        }
199        column_arg.update(dict_list)
200
201        function_args = self.transpose_args(dict_list=column_arg)
202        parallel_results = self.parallel_call(
203            function=self.list_without_pag, function_args=function_args,
204            n_parallel=n_parallel)
205        if flat_results:
206            return self.flatten_parallel(
207                parallel_results, as_dataframe=as_dataframe)
208        else:
209            return parallel_results
210
211    def parallel_list_by_chunks(self, model_class: Union[str, List[str]],
212                                list_args: List[dict],
213                                auth_header: dict = None,
214                                fields: list = None,
215                                default_fields: bool = False,
216                                foreign_key_fields: bool = False,
217                                base_filter_skip: list = None,
218                                n_parallel: int = None,
219                                chunk_size: int = 50000,
220                                flat_results: bool = True,
221                                as_dataframe: bool = False) -> List[dict]:
222        """List objects by chunks.
223
224        Order by arguments on list by chunks will be ignored.
225
226        Args:
227            model_class:
228                Model class of the end-point
229            list_args:
230                filter_dict:
231                    Filter dict to be used at the query. Filter elements from
232                    query return that satifies all statements of the dictonary.
233                exclude_dict:
234                    Exclude dict to be used at the query. Remove elements from
235                    query return that satifies all statements of the dictonary.
236            auth_header:
237                Auth header to substitute the microservice original
238                at the request (user impersonation).
239            fields (list):
240                Set the fields to be returned by the list end-point.
241            default_fields (bool):
242                Boolean, if true and fields arguments None will return the
243                default fields set for list by the backend.
244            foreign_key_fields (bool):
245                Return forenging key objects. It will return the fk
246                corresponding object. Ex: `created_by_id` reference to
247                a user `model_class` the correspondent to User will be
248                returned at `created_by`.
249            base_filter_skip (list):
250                List of base query filter to be skiped, it is necessary to
251                be superuser to skip base query filters.
252            n_parallel (int):
253                Number of parallel requests, if None will use the default
254                one.
255            flat_results (bool):
256                The results will be joined on a single list.
257            as_dataframe (bool):
258                If True, returns the results as a pandas DataFrame.
259            chunk_size (int):
260                Number of objects to fetch per query. Defaults to 50000.
261
262        Returns:
263          Containing objects serialized by list Serializer.
264
265        Raises:
266          No especific raises.
267        """ # NOQA
268        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
269
270        # Convert arguments to list
271        dict_list = self.expand_list_args(
272            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
273        list_model_class = self.convert_to_list(
274            argument=model_class, length=len(list_args))
275        list_auth_header = self.convert_to_list(
276            argument=auth_header, length=len(list_args))
277        list_fields = self.convert_to_list(
278            argument=fields, length=len(list_args),
279            force_replicate=True)
280        list_default_fields = self.convert_to_list(
281            argument=default_fields, length=len(list_args))
282        list_foreign_key_fields = self.convert_to_list(
283            argument=foreign_key_fields, length=len(list_args))
284        list_base_filter_skip = self.convert_to_list(
285            argument=base_filter_skip, length=len(list_args),
286            force_replicate=True)
287        list_chunk_size = self.convert_to_list(
288            argument=chunk_size, length=len(list_args),
289            force_replicate=True)
290        list_as_dataframe = self.convert_to_list(
291            argument=as_dataframe, length=len(list_args),
292            force_replicate=True)
293
294        column_arg = {
295            'model_class': list_model_class,
296            'auth_header': list_auth_header,
297            'fields': list_fields,
298            'default_fields': list_default_fields,
299            'foreign_key_fields': list_foreign_key_fields,
300            'base_filter_skip': list_base_filter_skip,
301            "chunk_size": list_chunk_size,
302            "as_dataframe": list_as_dataframe
303        }
304        column_arg.update(dict_list)
305
306        function_args = self.transpose_args(dict_list=column_arg)
307        parallel_results = self.parallel_call(
308            function=self.list_by_chunks, function_args=function_args,
309            n_parallel=n_parallel)
310        if flat_results:
311            return self.flatten_parallel(
312                parallel_results, as_dataframe=as_dataframe)
313        else:
314            return parallel_results

Abstract class for parallel calls at Pumpwood end-points.

def parallel_list( self, model_class: Union[str, List[str]], list_args: List[dict], auth_header: dict = None, fields: list = None, default_fields: bool = False, limit: int = None, foreign_key_fields: bool = False, base_filter_skip: list = None, n_parallel: int = None, flat_results: bool = True, as_dataframe: bool = False) -> List[dict]:
 11    def parallel_list(self, model_class: Union[str, List[str]],
 12                      list_args: List[dict],
 13                      auth_header: dict = None, fields: list = None,
 14                      default_fields: bool = False, limit: int = None,
 15                      foreign_key_fields: bool = False,
 16                      base_filter_skip: list = None,
 17                      n_parallel: int = None,
 18                      flat_results: bool = True,
 19                      as_dataframe: bool = False) -> List[dict]:
 20        """List objects with pagination.
 21
 22        Args:
 23            model_class:
 24                Model class of the end-point
 25            list_args:
 26                filter_dict:
 27                    Filter dict to be used at the query. Filter elements from
 28                    query return that satifies all statements of the dictonary.
 29                exclude_dict:
 30                    Exclude dict to be used at the query. Remove elements from
 31                    query return that satifies all statements of the dictonary.
 32                order_by: Order results acording to list of strings
 33                    correspondent to fields. It is possible to use '-' at the
 34                    begginng of the field name for reverse ordering. Ex.:
 35                    ['description'] for accendent ordering and ['-description']
 36                    for descendent ordering.
 37            auth_header:
 38                Auth header to substitute the microservice original
 39                at the request (user impersonation).
 40            fields (list):
 41                Set the fields to be returned by the list end-point.
 42            default_fields (bool):
 43                Boolean, if true and fields arguments None will return the
 44                default fields set for list by the backend.
 45            limit (int):
 46                Set the limit of elements of the returned query. By default,
 47                backend usually return 50 elements.
 48            foreign_key_fields (bool):
 49                Return forenging key objects. It will return the fk
 50                corresponding object. Ex: `created_by_id` reference to
 51                a user `model_class` the correspondent to User will be
 52                returned at `created_by`.
 53            base_filter_skip (list):
 54                List of base query filter to be skiped, it is necessary to
 55                be superuser to skip base query filters.
 56            n_parallel (int):
 57                Number of parallel requests, if None will use the default
 58                one.
 59            flat_results (bool):
 60                The results will be joined on a single list.
 61            as_dataframe (bool):
 62                If True, returns the results as a pandas DataFrame.
 63
 64        Returns:
 65          Containing objects serialized by list Serializer.
 66
 67        Raises:
 68          No especific raises.
 69        """ # NOQA
 70        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 71
 72        # Convert arguments to list
 73        dict_list = self.expand_list_args(list_args=list_args)
 74        list_model_class = self.convert_to_list(
 75            argument=model_class, length=len(list_args))
 76        list_auth_header = self.convert_to_list(
 77            argument=auth_header, length=len(list_args))
 78        list_fields = self.convert_to_list(
 79            argument=fields, length=len(list_args),
 80            force_replicate=True)
 81        list_default_fields = self.convert_to_list(
 82            argument=default_fields, length=len(list_args))
 83        list_limit = self.convert_to_list(
 84            argument=limit, length=len(list_args))
 85        list_foreign_key_fields = self.convert_to_list(
 86            argument=foreign_key_fields, length=len(list_args))
 87        list_base_filter_skip = self.convert_to_list(
 88            argument=base_filter_skip, length=len(list_args),
 89            force_replicate=True)
 90        list_as_dataframe = self.convert_to_list(
 91            argument=as_dataframe, length=len(list_args),
 92            force_replicate=True)
 93
 94        column_arg = {
 95            'model_class': list_model_class,
 96            'auth_header': list_auth_header,
 97            'fields': list_fields,
 98            'default_fields': list_default_fields,
 99            'limit': list_limit,
100            'foreign_key_fields': list_foreign_key_fields,
101            'base_filter_skip': list_base_filter_skip,
102            "as_dataframe": list_as_dataframe
103        }
104        column_arg.update(dict_list)
105
106        function_args = self.transpose_args(dict_list=column_arg)
107        parallel_results = self.parallel_call(
108            function=self.list, function_args=function_args,
109            n_parallel=n_parallel)
110        if flat_results:
111            return self.flatten_parallel(parallel_results)
112        else:
113            return parallel_results

List objects with pagination.

Arguments:
  • model_class: Model class of the end-point
  • list_args: filter_dict: Filter dict to be used at the query. Filter elements from query return that satifies all statements of the dictonary. exclude_dict: Exclude dict to be used at the query. Remove elements from query return that satifies all statements of the dictonary. order_by: Order results acording to list of strings correspondent to fields. It is possible to use '-' at the begginng of the field name for reverse ordering. Ex.: ['description'] for accendent ordering and ['-description'] for descendent ordering.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • fields (list): Set the fields to be returned by the list end-point.
  • default_fields (bool): Boolean, if true and fields arguments None will return the default fields set for list by the backend.
  • limit (int): Set the limit of elements of the returned query. By default, backend usually return 50 elements.
  • foreign_key_fields (bool): Return forenging key objects. It will return the fk corresponding object. Ex: created_by_id reference to a user model_class the correspondent to User will be returned at created_by.
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int): Number of parallel requests, if None will use the default one.
  • flat_results (bool): The results will be joined on a single list.
  • as_dataframe (bool): If True, returns the results as a pandas DataFrame.
Returns:

Containing objects serialized by list Serializer.

Raises:
  • No especific raises.
def parallel_list_without_pag( self, model_class: Union[str, List[str]], list_args: List[dict], auth_header: dict = None, fields: list = None, default_fields: bool = False, foreign_key_fields: bool = False, base_filter_skip: list = None, n_parallel: int = None, flat_results: bool = True, as_dataframe: bool = False) -> List[dict]:
115    def parallel_list_without_pag(self, model_class: Union[str, List[str]],
116                                  list_args: List[dict],
117                                  auth_header: dict = None,
118                                  fields: list = None,
119                                  default_fields: bool = False,
120                                  foreign_key_fields: bool = False,
121                                  base_filter_skip: list = None,
122                                  n_parallel: int = None,
123                                  flat_results: bool = True,
124                                  as_dataframe: bool = False) -> List[dict]:
125        """List objects with pagination.
126
127        Args:
128            model_class:
129                Model class of the end-point
130            list_args:
131                filter_dict:
132                    Filter dict to be used at the query. Filter elements from
133                    query return that satifies all statements of the dictonary.
134                exclude_dict:
135                    Exclude dict to be used at the query. Remove elements from
136                    query return that satifies all statements of the dictonary.
137            auth_header:
138                Auth header to substitute the microservice original
139                at the request (user impersonation).
140            fields (list):
141                Set the fields to be returned by the list end-point.
142            default_fields (bool):
143                Boolean, if true and fields arguments None will return the
144                default fields set for list by the backend.
145            foreign_key_fields (bool):
146                Return forenging key objects. It will return the fk
147                corresponding object. Ex: `created_by_id` reference to
148                a user `model_class` the correspondent to User will be
149                returned at `created_by`.
150            base_filter_skip (list):
151                List of base query filter to be skiped, it is necessary to
152                be superuser to skip base query filters.
153            n_parallel (int):
154                Number of parallel requests, if None will use the default
155                one.
156            flat_results (bool):
157                The results will be joined on a single list.
158            as_dataframe (bool):
159                If True, returns the results as a pandas DataFrame.
160
161        Returns:
162          Containing objects serialized by list Serializer.
163
164        Raises:
165          No especific raises.
166        """ # NOQA
167        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
168
169        # Convert arguments to list
170        dict_list = self.expand_list_args(
171            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
172        list_model_class = self.convert_to_list(
173            argument=model_class, length=len(list_args))
174        list_auth_header = self.convert_to_list(
175            argument=auth_header, length=len(list_args))
176        list_fields = self.convert_to_list(
177            argument=fields, length=len(list_args),
178            force_replicate=True)
179        list_default_fields = self.convert_to_list(
180            argument=default_fields, length=len(list_args))
181        list_foreign_key_fields = self.convert_to_list(
182            argument=foreign_key_fields, length=len(list_args))
183        list_base_filter_skip = self.convert_to_list(
184            argument=base_filter_skip, length=len(list_args),
185            force_replicate=True)
186        list_as_dataframe = self.convert_to_list(
187            argument=as_dataframe, length=len(list_args),
188            force_replicate=True)
189
190        column_arg = {
191            'model_class': list_model_class,
192            'auth_header': list_auth_header,
193            'fields': list_fields,
194            'default_fields': list_default_fields,
195            'foreign_key_fields': list_foreign_key_fields,
196            'base_filter_skip': list_base_filter_skip,
197            "as_dataframe": list_as_dataframe
198        }
199        column_arg.update(dict_list)
200
201        function_args = self.transpose_args(dict_list=column_arg)
202        parallel_results = self.parallel_call(
203            function=self.list_without_pag, function_args=function_args,
204            n_parallel=n_parallel)
205        if flat_results:
206            return self.flatten_parallel(
207                parallel_results, as_dataframe=as_dataframe)
208        else:
209            return parallel_results

List objects with pagination.

Arguments:
  • model_class: Model class of the end-point
  • list_args: filter_dict: Filter dict to be used at the query. Filter elements from query return that satifies all statements of the dictonary. exclude_dict: Exclude dict to be used at the query. Remove elements from query return that satifies all statements of the dictonary.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • fields (list): Set the fields to be returned by the list end-point.
  • default_fields (bool): Boolean, if true and fields arguments None will return the default fields set for list by the backend.
  • foreign_key_fields (bool): Return forenging key objects. It will return the fk corresponding object. Ex: created_by_id reference to a user model_class the correspondent to User will be returned at created_by.
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int): Number of parallel requests, if None will use the default one.
  • flat_results (bool): The results will be joined on a single list.
  • as_dataframe (bool): If True, returns the results as a pandas DataFrame.
Returns:

Containing objects serialized by list Serializer.

Raises:
  • No especific raises.
def parallel_list_by_chunks( self, model_class: Union[str, List[str]], list_args: List[dict], auth_header: dict = None, fields: list = None, default_fields: bool = False, foreign_key_fields: bool = False, base_filter_skip: list = None, n_parallel: int = None, chunk_size: int = 50000, flat_results: bool = True, as_dataframe: bool = False) -> List[dict]:
211    def parallel_list_by_chunks(self, model_class: Union[str, List[str]],
212                                list_args: List[dict],
213                                auth_header: dict = None,
214                                fields: list = None,
215                                default_fields: bool = False,
216                                foreign_key_fields: bool = False,
217                                base_filter_skip: list = None,
218                                n_parallel: int = None,
219                                chunk_size: int = 50000,
220                                flat_results: bool = True,
221                                as_dataframe: bool = False) -> List[dict]:
222        """List objects by chunks.
223
224        Order by arguments on list by chunks will be ignored.
225
226        Args:
227            model_class:
228                Model class of the end-point
229            list_args:
230                filter_dict:
231                    Filter dict to be used at the query. Filter elements from
232                    query return that satifies all statements of the dictonary.
233                exclude_dict:
234                    Exclude dict to be used at the query. Remove elements from
235                    query return that satifies all statements of the dictonary.
236            auth_header:
237                Auth header to substitute the microservice original
238                at the request (user impersonation).
239            fields (list):
240                Set the fields to be returned by the list end-point.
241            default_fields (bool):
242                Boolean, if true and fields arguments None will return the
243                default fields set for list by the backend.
244            foreign_key_fields (bool):
245                Return forenging key objects. It will return the fk
246                corresponding object. Ex: `created_by_id` reference to
247                a user `model_class` the correspondent to User will be
248                returned at `created_by`.
249            base_filter_skip (list):
250                List of base query filter to be skiped, it is necessary to
251                be superuser to skip base query filters.
252            n_parallel (int):
253                Number of parallel requests, if None will use the default
254                one.
255            flat_results (bool):
256                The results will be joined on a single list.
257            as_dataframe (bool):
258                If True, returns the results as a pandas DataFrame.
259            chunk_size (int):
260                Number of objects to fetch per query. Defaults to 50000.
261
262        Returns:
263          Containing objects serialized by list Serializer.
264
265        Raises:
266          No especific raises.
267        """ # NOQA
268        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
269
270        # Convert arguments to list
271        dict_list = self.expand_list_args(
272            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
273        list_model_class = self.convert_to_list(
274            argument=model_class, length=len(list_args))
275        list_auth_header = self.convert_to_list(
276            argument=auth_header, length=len(list_args))
277        list_fields = self.convert_to_list(
278            argument=fields, length=len(list_args),
279            force_replicate=True)
280        list_default_fields = self.convert_to_list(
281            argument=default_fields, length=len(list_args))
282        list_foreign_key_fields = self.convert_to_list(
283            argument=foreign_key_fields, length=len(list_args))
284        list_base_filter_skip = self.convert_to_list(
285            argument=base_filter_skip, length=len(list_args),
286            force_replicate=True)
287        list_chunk_size = self.convert_to_list(
288            argument=chunk_size, length=len(list_args),
289            force_replicate=True)
290        list_as_dataframe = self.convert_to_list(
291            argument=as_dataframe, length=len(list_args),
292            force_replicate=True)
293
294        column_arg = {
295            'model_class': list_model_class,
296            'auth_header': list_auth_header,
297            'fields': list_fields,
298            'default_fields': list_default_fields,
299            'foreign_key_fields': list_foreign_key_fields,
300            'base_filter_skip': list_base_filter_skip,
301            "chunk_size": list_chunk_size,
302            "as_dataframe": list_as_dataframe
303        }
304        column_arg.update(dict_list)
305
306        function_args = self.transpose_args(dict_list=column_arg)
307        parallel_results = self.parallel_call(
308            function=self.list_by_chunks, function_args=function_args,
309            n_parallel=n_parallel)
310        if flat_results:
311            return self.flatten_parallel(
312                parallel_results, as_dataframe=as_dataframe)
313        else:
314            return parallel_results

List objects by chunks.

Order by arguments on list by chunks will be ignored.

Arguments:
  • model_class: Model class of the end-point
  • list_args: filter_dict: Filter dict to be used at the query. Filter elements from query return that satifies all statements of the dictonary. exclude_dict: Exclude dict to be used at the query. Remove elements from query return that satifies all statements of the dictonary.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • fields (list): Set the fields to be returned by the list end-point.
  • default_fields (bool): Boolean, if true and fields arguments None will return the default fields set for list by the backend.
  • foreign_key_fields (bool): Return forenging key objects. It will return the fk corresponding object. Ex: created_by_id reference to a user model_class the correspondent to User will be returned at created_by.
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int): Number of parallel requests, if None will use the default one.
  • flat_results (bool): The results will be joined on a single list.
  • as_dataframe (bool): If True, returns the results as a pandas DataFrame.
  • chunk_size (int): Number of objects to fetch per query. Defaults to 50000.
Returns:

Containing objects serialized by list Serializer.

Raises:
  • No especific raises.
class ABCParallelRetriveMicroservice(pumpwood_communication.microservice_abc.parallel.base.ABCParallelBaseMicroservice):
  7class ABCParallelRetriveMicroservice(ABCParallelBaseMicroservice):
  8    """Abstract class for parallel calls at Pumpwood end-points."""
  9
 10    def parallel_retrieve(self, model_class: str | list[str],
 11                          list_pk: list[int],
 12                          default_fields: bool | list[bool] = False,
 13                          foreign_key_fields: bool | list[bool] = False,
 14                          related_fields: bool | list[bool] = False,
 15                          fields: list[str] | list[list[str]] = None,
 16                          auth_header: dict | list[dict] = None,
 17                          use_disk_cache: bool | list[bool] = False,
 18                          use_app_cache: bool | list[bool] = False,
 19                          disk_cache_expire: int | list[int] = None,
 20                          base_filter_skip: list[str] | list[list[str]] = None,
 21                          n_parallel: int | None = None,
 22                          ) -> list[dict]:
 23        """Retrieve an object from PumpWood.
 24
 25        Function to get object serialized by retrieve end-point
 26        (more detailed data).
 27
 28        Args:
 29            model_class:
 30                Model class of the end-point
 31            list_pk:
 32                Object pk
 33            auth_header:
 34                Auth header to substitute the microservice original
 35                at the request (user impersonation).
 36            fields:
 37                Set the fields to be returned by the list end-point.
 38            default_fields:
 39                Boolean, if true and fields arguments None will return the
 40                default fields set for list by the backend.
 41            foreign_key_fields:
 42                Return forenging key objects. It will return the fk
 43                corresponding object. Ex: `created_by_id` reference to
 44                a user `model_class` the correspondent to User will be
 45                returned at `created_by`.
 46            related_fields:
 47                Return related fields objects. Related field objects are
 48                objects that have a forenging key associated with this
 49                model_class, results will be returned as a list of
 50                dictionaries usually in a field with `_set` at end.
 51                Returning related_fields consume backend resorces, use
 52                carefully.
 53            use_disk_cache (bool):
 54                If set true, get request will use local cache to reduce
 55                the requests to the backend.
 56            use_app_cache (bool):
 57                If True, the GET request will use the cache of the application.
 58                Defaults to False.
 59            disk_cache_expire (int):
 60                Time in seconds to expire the cache, it None it will
 61                use de default set be PumpwoodCache.
 62            base_filter_skip (list):
 63                List of base query filter to be skiped, it is necessary to
 64                be superuser to skip base query filters.
 65            n_parallel (int | None):
 66                N parallel request that will be done on backend.
 67
 68        Returns:
 69            Return object with the correspondent pk.
 70
 71        Raises:
 72            PumpWoodObjectDoesNotExist:
 73                If pk not found on database.
 74        """
 75        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 76
 77        list_model_class = self.convert_to_list(
 78            argument=model_class, length=len(list_pk))
 79        list_default_fields = self.convert_to_list(
 80            argument=default_fields, length=len(list_pk))
 81        list_foreign_key_fields = self.convert_to_list(
 82            argument=foreign_key_fields, length=len(list_pk))
 83        list_related_fields = self.convert_to_list(
 84            argument=related_fields, length=len(list_pk))
 85        list_fields = self.convert_to_list(
 86            argument=fields, length=len(list_pk),
 87            force_replicate=True)
 88        list_auth_header = self.convert_to_list(
 89            argument=auth_header, length=len(list_pk))
 90        list_use_disk_cache = self.convert_to_list(
 91            argument=use_disk_cache, length=len(list_pk))
 92        list_use_app_cache = self.convert_to_list(
 93            argument=use_app_cache, length=len(list_pk))
 94        list_disk_cache_expire = self.convert_to_list(
 95            argument=disk_cache_expire, length=len(list_pk))
 96        list_base_filter_skip = self.convert_to_list(
 97            argument=base_filter_skip, length=len(list_pk),
 98            force_replicate=True)
 99
100        column_arg = {
101            'model_class': list_model_class,
102            'pk': list_pk,
103            'default_fields': list_default_fields,
104            'foreign_key_fields': list_foreign_key_fields,
105            'related_fields': list_related_fields,
106            'fields': list_fields,
107            'auth_header': list_auth_header,
108            'use_disk_cache': list_use_disk_cache,
109            'use_app_cache': list_use_app_cache,
110            'disk_cache_expire': list_disk_cache_expire,
111            'base_filter_skip': list_base_filter_skip}
112
113        function_args = self.transpose_args(dict_list=column_arg)
114        return self.parallel_call(
115            function=self.retrieve, function_args=function_args,
116            n_parallel=n_parallel)
117
118    def parallel_list_one(self, model_class: str | list[str],
119                          list_pk: list[int],
120                          default_fields: bool | list[bool] = True,
121                          foreign_key_fields: bool | list[bool] = False,
122                          related_fields: bool | list[bool] = False,
123                          fields: list[str] | list[list[str]] = None,
124                          auth_header: dict | list[dict] = None,
125                          use_disk_cache: bool | list[bool] = False,
126                          use_app_cache: bool | list[bool] = False,
127                          disk_cache_expire: int | list[int] = None,
128                          base_filter_skip: list[str] | list[list[str]] = None,
129                          n_parallel: int | None = None
130                          ) -> list[dict]:
131        """Retrieve an object from PumpWood.
132
133        Function to get object serialized by retrieve end-point
134        (more detailed data).
135
136        Args:
137            model_class:
138                Model class of the end-point
139            list_pk:
140                Object pk
141            auth_header:
142                Auth header to substitute the microservice original
143                at the request (user impersonation).
144            fields:
145                Set the fields to be returned by the list end-point.
146            default_fields:
147                Boolean, if true and fields arguments None will return the
148                default fields set for list by the backend.
149            foreign_key_fields:
150                Return forenging key objects. It will return the fk
151                corresponding object. Ex: `created_by_id` reference to
152                a user `model_class` the correspondent to User will be
153                returned at `created_by`.
154            related_fields:
155                Return related fields objects. Related field objects are
156                objects that have a forenging key associated with this
157                model_class, results will be returned as a list of
158                dictionaries usually in a field with `_set` at end.
159                Returning related_fields consume backend resorces, use
160                carefully.
161            use_disk_cache (bool):
162                If set true, get request will use local cache to reduce
163                the requests to the backend.
164            use_app_cache (bool):
165                If True, the GET request will use the cache of the application.
166                Defaults to False.
167            disk_cache_expire (int):
168                Time in seconds to expire the cache, it None it will
169                use de default set be PumpwoodCache.
170            base_filter_skip (list):
171                List of base query filter to be skiped, it is necessary to
172                be superuser to skip base query filters.
173            n_parallel (int | None):
174                N parallel request that will be done on backend.
175
176        Returns:
177            Return object with the correspondent pk.
178
179        Raises:
180            PumpWoodObjectDoesNotExist:
181                If pk not found on database.
182        """
183        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
184
185        list_model_class = self.convert_to_list(
186            argument=model_class, length=len(list_pk))
187        list_default_fields = self.convert_to_list(
188            argument=default_fields, length=len(list_pk))
189        list_foreign_key_fields = self.convert_to_list(
190            argument=foreign_key_fields, length=len(list_pk))
191        list_related_fields = self.convert_to_list(
192            argument=related_fields, length=len(list_pk))
193        list_fields = self.convert_to_list(
194            argument=fields, length=len(list_pk),
195            force_replicate=True)
196        list_auth_header = self.convert_to_list(
197            argument=auth_header, length=len(list_pk))
198        list_use_disk_cache = self.convert_to_list(
199            argument=use_disk_cache, length=len(list_pk))
200        list_use_app_cache = self.convert_to_list(
201            argument=use_app_cache, length=len(list_pk))
202        list_disk_cache_expire = self.convert_to_list(
203            argument=disk_cache_expire, length=len(list_pk))
204        list_base_filter_skip = self.convert_to_list(
205            argument=base_filter_skip, length=len(list_pk),
206            force_replicate=True)
207
208        column_arg = {
209            'model_class': list_model_class,
210            'pk': list_pk,
211            'default_fields': list_default_fields,
212            'foreign_key_fields': list_foreign_key_fields,
213            'related_fields': list_related_fields,
214            'fields': list_fields,
215            'auth_header': list_auth_header,
216            'use_disk_cache': list_use_disk_cache,
217            'use_app_cache': list_use_app_cache,
218            'disk_cache_expire': list_disk_cache_expire,
219            'base_filter_skip': list_base_filter_skip}
220
221        function_args = self.transpose_args(dict_list=column_arg)
222        return self.parallel_call(
223            function=self.list_one, function_args=function_args,
224            n_parallel=n_parallel)
225
226    def parallel_retrieve_file(self, model_class: str | list[str],
227                               list_pk: list[int],
228                               file_field: str | list[str],
229                               auth_header: dict | list[dict] = None,
230                               save_file: bool | list[bool] = True,
231                               save_path: str | list[str] = "./",
232                               file_name: str | list[str] = None,
233                               if_exists: str | list[str] = "fail",
234                               base_filter_skip: list[str] | list[list[str]] = None,  #NOQA
235                               n_parallel: int | None = None
236                               ) -> list[str] | list[dict]:
237        """Retrieve a file from PumpWood.
238
239        This function will retrieve file as a single request, depending on the
240        size of the files it would be preferred to use streaming end-point.
241
242        Args:
243            model_class:
244                Class of the model to retrieve file.
245            list_pk:
246                Pk of the object associeted file.
247            file_field:
248                Field of the file to be downloaded.
249            auth_header:
250                Dictionary containing the auth header.
251            save_file:
252                If data is to be saved as file or return get
253                response.
254            save_path:
255                Path of the directory to save file.
256            file_name:
257                Name of the file, if None it will have same name as
258                saved in PumpWood.
259            if_exists:
260                Values must be in {'fail', 'change_name', 'overwrite', 'skip'}.
261                Set what to do if there is a file with same name. Skip
262                will not download file if there is already with same
263                os.path.join(save_path, file_name), file_name must be set
264                for skip argument.
265            auth_header:
266                Auth header to substitute the microservice original
267                at the request (user impersonation).
268            base_filter_skip (list):
269                List of base query filter to be skiped, it is necessary to
270                be superuser to skip base query filters.
271            n_parallel (int):
272                Number of parallel requests, if None will use the default
273                one.
274
275        Returns:
276            May return the file name if save_file=True; If false will return
277            a dictonary with keys `filename` with original file name and
278            `content` with binary data of file content.
279
280        Raises:
281            PumpWoodForbidden:
282                'storage_object attribute not set for view, file operations
283                are disable'. This indicates that storage for this backend
284                was not configured, so it is not possible to make storage
285                operations,
286            PumpWoodForbidden:
287                'file_field must be set on self.file_fields dictionary'. This
288                indicates that the `file_field` parameter is not listed as
289                a file field on the backend.
290            PumpWoodObjectDoesNotExist:
291                'field [{}] not found or null at object'. This indicates that
292                the file field requested is not present on object fields.
293            PumpWoodObjectDoesNotExist:
294                'Object not found in storage [{}]'. This indicates that the
295                file associated with file_field is not avaiable at the
296                storage. This should not ocorrur, it might have a manual
297                update at the model_class table or manual removal/rename of
298                files on storage.
299        """
300        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
301
302        # Convert arguments to list
303        list_model_class = self.convert_to_list(
304            argument=model_class, length=len(list_pk))
305        list_file_field = self.convert_to_list(
306            argument=file_field, length=len(list_pk))
307        list_auth_header = self.convert_to_list(
308            argument=auth_header, length=len(list_pk))
309        list_save_file = self.convert_to_list(
310            argument=save_file, length=len(list_pk))
311        list_save_path = self.convert_to_list(
312            argument=save_path, length=len(list_pk))
313        list_file_name = self.convert_to_list(
314            argument=file_name, length=len(list_pk))
315        list_if_exists = self.convert_to_list(
316            argument=if_exists, length=len(list_pk))
317        list_base_filter_skip = self.convert_to_list(
318            argument=base_filter_skip, length=len(list_pk),
319            force_replicate=True)
320
321        column_arg = {
322            'model_class': list_model_class,
323            'pk': list_pk,
324            'file_field': list_file_field,
325            'auth_header': list_auth_header,
326            'save_file': list_save_file,
327            'save_path': list_save_path,
328            'file_name': list_file_name,
329            'if_exists': list_if_exists,
330            'base_filter_skip': list_base_filter_skip}
331        function_args = self.transpose_args(dict_list=column_arg)
332        return self.parallel_call(
333            function=self.retrieve_file, function_args=function_args,
334            n_parallel=n_parallel)
335
336    def parallel_retrieve_streaming_file(self, model_class: str,
337                                         list_pk: list[int],
338                                         file_field: str | list[str],
339                                         file_name: str | list[str],
340                                         auth_header: dict | list[dict] = None,
341                                         save_path: str | list[str] = "./",
342                                         if_exists: str | list[str] = "fail",
343                                         base_filter_skip: list | list[list] = None, # NOQA
344                                         n_parallel: int | None = None
345                                         ) -> str:
346        """Retrieve a file from PumpWood using streaming to retrieve content.
347
348        This funcion uses file streaming to retrieve file content, it should be
349        prefered when dealing with large (bigger than 10Mb) files transfer.
350        Using this end-point the file is not loaded on backend memory content
351        is transfered by chucks that are read at the storage and transfered
352        to user.
353
354        It will necessarily save the content as a file, there is not the
355        possibility of retrieving the content directly from request.
356
357        Args:
358            model_class:
359                Class of the model to retrieve file.
360            list_pk (list):
361                Pk of the object associeted file.
362            file_field:
363                Field of the file to be downloaded.
364            auth_header:
365                Dictionary containing the auth header.
366            save_path:
367                Path of the directory to save file.
368            file_name:
369                Name of the file, if None it will have same name as
370                saved in PumpWood.
371            if_exists:
372                Values must be in {'fail', 'change_name', 'overwrite'}.
373                Set what to do if there is a file with same name.
374            auth_header:
375                Auth header to substitute the microservice original
376                at the request (user impersonation).
377            base_filter_skip (list):
378                List of base query filter to be skiped, it is necessary to
379                be superuser to skip base query filters.
380            n_parallel (int):
381                Number of parallel requests, if None will use the default
382                one.
383
384        Returns:
385            Returns the file path that recived the file content.
386
387        Raises:
388            PumpWoodForbidden:
389                'storage_object attribute not set for view, file operations
390                are disable'. This indicates that storage for this backend
391                was not configured, so it is not possible to make storage
392                operations,
393            PumpWoodForbidden:
394                'file_field must be set on self.file_fields dictionary'. This
395                indicates that the `file_field` parameter is not listed as
396                a file field on the backend.
397            PumpWoodObjectDoesNotExist:
398                'field [{}] not found or null at object'. This indicates that
399                the file field requested is not present on object fields.
400            PumpWoodObjectDoesNotExist:
401                'Object not found in storage [{}]'. This indicates that the
402                file associated with file_field is not avaiable at the
403                storage. This should not ocorrur, it might have a manual
404                update at the model_class table or manual removal/rename of
405                files on storage.
406        """
407        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
408
409        # Convert arguments to list
410        list_model_class = self.convert_to_list(
411            argument=model_class, length=len(list_pk))
412        list_file_field = self.convert_to_list(
413            argument=file_field, length=len(list_pk))
414        list_file_name = self.convert_to_list(
415            argument=file_name, length=len(list_pk))
416        list_auth_header = self.convert_to_list(
417            argument=auth_header, length=len(list_pk))
418        list_save_path = self.convert_to_list(
419            argument=save_path, length=len(list_pk))
420        list_if_exists = self.convert_to_list(
421            argument=if_exists, length=len(list_pk))
422        list_base_filter_skip = self.convert_to_list(
423            argument=base_filter_skip, length=len(list_pk),
424            force_replicate=True)
425
426        column_arg = {
427            'model_class': list_model_class,
428            'pk': list_pk,
429            'file_field': list_file_field,
430            'file_name': list_file_name,
431            'auth_header': list_auth_header,
432            'save_path': list_save_path,
433            'if_exists': list_if_exists,
434            'base_filter_skip': list_base_filter_skip,
435        }
436        function_args = self.transpose_args(dict_list=column_arg)
437        return self.parallel_call(
438            function=self.retrieve_streaming_file, function_args=function_args,
439            n_parallel=n_parallel)

Abstract class for parallel calls at Pumpwood end-points.

def parallel_retrieve( self, model_class: str | list[str], list_pk: list[int], default_fields: bool | list[bool] = False, foreign_key_fields: bool | list[bool] = False, related_fields: bool | list[bool] = False, fields: list[str] | list[list[str]] = None, auth_header: dict | list[dict] = None, use_disk_cache: bool | list[bool] = False, use_app_cache: bool | list[bool] = False, disk_cache_expire: int | list[int] = None, base_filter_skip: list[str] | list[list[str]] = None, n_parallel: int | None = None) -> list[dict]:
 10    def parallel_retrieve(self, model_class: str | list[str],
 11                          list_pk: list[int],
 12                          default_fields: bool | list[bool] = False,
 13                          foreign_key_fields: bool | list[bool] = False,
 14                          related_fields: bool | list[bool] = False,
 15                          fields: list[str] | list[list[str]] = None,
 16                          auth_header: dict | list[dict] = None,
 17                          use_disk_cache: bool | list[bool] = False,
 18                          use_app_cache: bool | list[bool] = False,
 19                          disk_cache_expire: int | list[int] = None,
 20                          base_filter_skip: list[str] | list[list[str]] = None,
 21                          n_parallel: int | None = None,
 22                          ) -> list[dict]:
 23        """Retrieve an object from PumpWood.
 24
 25        Function to get object serialized by retrieve end-point
 26        (more detailed data).
 27
 28        Args:
 29            model_class:
 30                Model class of the end-point
 31            list_pk:
 32                Object pk
 33            auth_header:
 34                Auth header to substitute the microservice original
 35                at the request (user impersonation).
 36            fields:
 37                Set the fields to be returned by the list end-point.
 38            default_fields:
 39                Boolean, if true and fields arguments None will return the
 40                default fields set for list by the backend.
 41            foreign_key_fields:
 42                Return forenging key objects. It will return the fk
 43                corresponding object. Ex: `created_by_id` reference to
 44                a user `model_class` the correspondent to User will be
 45                returned at `created_by`.
 46            related_fields:
 47                Return related fields objects. Related field objects are
 48                objects that have a forenging key associated with this
 49                model_class, results will be returned as a list of
 50                dictionaries usually in a field with `_set` at end.
 51                Returning related_fields consume backend resorces, use
 52                carefully.
 53            use_disk_cache (bool):
 54                If set true, get request will use local cache to reduce
 55                the requests to the backend.
 56            use_app_cache (bool):
 57                If True, the GET request will use the cache of the application.
 58                Defaults to False.
 59            disk_cache_expire (int):
 60                Time in seconds to expire the cache, it None it will
 61                use de default set be PumpwoodCache.
 62            base_filter_skip (list):
 63                List of base query filter to be skiped, it is necessary to
 64                be superuser to skip base query filters.
 65            n_parallel (int | None):
 66                N parallel request that will be done on backend.
 67
 68        Returns:
 69            Return object with the correspondent pk.
 70
 71        Raises:
 72            PumpWoodObjectDoesNotExist:
 73                If pk not found on database.
 74        """
 75        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 76
 77        list_model_class = self.convert_to_list(
 78            argument=model_class, length=len(list_pk))
 79        list_default_fields = self.convert_to_list(
 80            argument=default_fields, length=len(list_pk))
 81        list_foreign_key_fields = self.convert_to_list(
 82            argument=foreign_key_fields, length=len(list_pk))
 83        list_related_fields = self.convert_to_list(
 84            argument=related_fields, length=len(list_pk))
 85        list_fields = self.convert_to_list(
 86            argument=fields, length=len(list_pk),
 87            force_replicate=True)
 88        list_auth_header = self.convert_to_list(
 89            argument=auth_header, length=len(list_pk))
 90        list_use_disk_cache = self.convert_to_list(
 91            argument=use_disk_cache, length=len(list_pk))
 92        list_use_app_cache = self.convert_to_list(
 93            argument=use_app_cache, length=len(list_pk))
 94        list_disk_cache_expire = self.convert_to_list(
 95            argument=disk_cache_expire, length=len(list_pk))
 96        list_base_filter_skip = self.convert_to_list(
 97            argument=base_filter_skip, length=len(list_pk),
 98            force_replicate=True)
 99
100        column_arg = {
101            'model_class': list_model_class,
102            'pk': list_pk,
103            'default_fields': list_default_fields,
104            'foreign_key_fields': list_foreign_key_fields,
105            'related_fields': list_related_fields,
106            'fields': list_fields,
107            'auth_header': list_auth_header,
108            'use_disk_cache': list_use_disk_cache,
109            'use_app_cache': list_use_app_cache,
110            'disk_cache_expire': list_disk_cache_expire,
111            'base_filter_skip': list_base_filter_skip}
112
113        function_args = self.transpose_args(dict_list=column_arg)
114        return self.parallel_call(
115            function=self.retrieve, function_args=function_args,
116            n_parallel=n_parallel)

Retrieve an object from PumpWood.

Function to get object serialized by retrieve end-point (more detailed data).

Arguments:
  • model_class: Model class of the end-point
  • list_pk: Object pk
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • fields: Set the fields to be returned by the list end-point.
  • default_fields: Boolean, if true and fields arguments None will return the default fields set for list by the backend.
  • foreign_key_fields: Return forenging key objects. It will return the fk corresponding object. Ex: created_by_id reference to a user model_class the correspondent to User will be returned at created_by.
  • related_fields: Return related fields objects. Related field objects are objects that have a forenging key associated with this model_class, results will be returned as a list of dictionaries usually in a field with _set at end. Returning related_fields consume backend resorces, use carefully.
  • use_disk_cache (bool): If set true, get request will use local cache to reduce the requests to the backend.
  • use_app_cache (bool): If True, the GET request will use the cache of the application. Defaults to False.
  • disk_cache_expire (int): Time in seconds to expire the cache, it None it will use de default set be PumpwoodCache.
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int | None): N parallel request that will be done on backend.
Returns:

Return object with the correspondent pk.

Raises:
  • PumpWoodObjectDoesNotExist: If pk not found on database.
def parallel_list_one( self, model_class: str | list[str], list_pk: list[int], default_fields: bool | list[bool] = True, foreign_key_fields: bool | list[bool] = False, related_fields: bool | list[bool] = False, fields: list[str] | list[list[str]] = None, auth_header: dict | list[dict] = None, use_disk_cache: bool | list[bool] = False, use_app_cache: bool | list[bool] = False, disk_cache_expire: int | list[int] = None, base_filter_skip: list[str] | list[list[str]] = None, n_parallel: int | None = None) -> list[dict]:
118    def parallel_list_one(self, model_class: str | list[str],
119                          list_pk: list[int],
120                          default_fields: bool | list[bool] = True,
121                          foreign_key_fields: bool | list[bool] = False,
122                          related_fields: bool | list[bool] = False,
123                          fields: list[str] | list[list[str]] = None,
124                          auth_header: dict | list[dict] = None,
125                          use_disk_cache: bool | list[bool] = False,
126                          use_app_cache: bool | list[bool] = False,
127                          disk_cache_expire: int | list[int] = None,
128                          base_filter_skip: list[str] | list[list[str]] = None,
129                          n_parallel: int | None = None
130                          ) -> list[dict]:
131        """Retrieve an object from PumpWood.
132
133        Function to get object serialized by retrieve end-point
134        (more detailed data).
135
136        Args:
137            model_class:
138                Model class of the end-point
139            list_pk:
140                Object pk
141            auth_header:
142                Auth header to substitute the microservice original
143                at the request (user impersonation).
144            fields:
145                Set the fields to be returned by the list end-point.
146            default_fields:
147                Boolean, if true and fields arguments None will return the
148                default fields set for list by the backend.
149            foreign_key_fields:
150                Return forenging key objects. It will return the fk
151                corresponding object. Ex: `created_by_id` reference to
152                a user `model_class` the correspondent to User will be
153                returned at `created_by`.
154            related_fields:
155                Return related fields objects. Related field objects are
156                objects that have a forenging key associated with this
157                model_class, results will be returned as a list of
158                dictionaries usually in a field with `_set` at end.
159                Returning related_fields consume backend resorces, use
160                carefully.
161            use_disk_cache (bool):
162                If set true, get request will use local cache to reduce
163                the requests to the backend.
164            use_app_cache (bool):
165                If True, the GET request will use the cache of the application.
166                Defaults to False.
167            disk_cache_expire (int):
168                Time in seconds to expire the cache, it None it will
169                use de default set be PumpwoodCache.
170            base_filter_skip (list):
171                List of base query filter to be skiped, it is necessary to
172                be superuser to skip base query filters.
173            n_parallel (int | None):
174                N parallel request that will be done on backend.
175
176        Returns:
177            Return object with the correspondent pk.
178
179        Raises:
180            PumpWoodObjectDoesNotExist:
181                If pk not found on database.
182        """
183        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
184
185        list_model_class = self.convert_to_list(
186            argument=model_class, length=len(list_pk))
187        list_default_fields = self.convert_to_list(
188            argument=default_fields, length=len(list_pk))
189        list_foreign_key_fields = self.convert_to_list(
190            argument=foreign_key_fields, length=len(list_pk))
191        list_related_fields = self.convert_to_list(
192            argument=related_fields, length=len(list_pk))
193        list_fields = self.convert_to_list(
194            argument=fields, length=len(list_pk),
195            force_replicate=True)
196        list_auth_header = self.convert_to_list(
197            argument=auth_header, length=len(list_pk))
198        list_use_disk_cache = self.convert_to_list(
199            argument=use_disk_cache, length=len(list_pk))
200        list_use_app_cache = self.convert_to_list(
201            argument=use_app_cache, length=len(list_pk))
202        list_disk_cache_expire = self.convert_to_list(
203            argument=disk_cache_expire, length=len(list_pk))
204        list_base_filter_skip = self.convert_to_list(
205            argument=base_filter_skip, length=len(list_pk),
206            force_replicate=True)
207
208        column_arg = {
209            'model_class': list_model_class,
210            'pk': list_pk,
211            'default_fields': list_default_fields,
212            'foreign_key_fields': list_foreign_key_fields,
213            'related_fields': list_related_fields,
214            'fields': list_fields,
215            'auth_header': list_auth_header,
216            'use_disk_cache': list_use_disk_cache,
217            'use_app_cache': list_use_app_cache,
218            'disk_cache_expire': list_disk_cache_expire,
219            'base_filter_skip': list_base_filter_skip}
220
221        function_args = self.transpose_args(dict_list=column_arg)
222        return self.parallel_call(
223            function=self.list_one, function_args=function_args,
224            n_parallel=n_parallel)

Retrieve an object from PumpWood.

Function to get object serialized by retrieve end-point (more detailed data).

Arguments:
  • model_class: Model class of the end-point
  • list_pk: Object pk
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • fields: Set the fields to be returned by the list end-point.
  • default_fields: Boolean, if true and fields arguments None will return the default fields set for list by the backend.
  • foreign_key_fields: Return forenging key objects. It will return the fk corresponding object. Ex: created_by_id reference to a user model_class the correspondent to User will be returned at created_by.
  • related_fields: Return related fields objects. Related field objects are objects that have a forenging key associated with this model_class, results will be returned as a list of dictionaries usually in a field with _set at end. Returning related_fields consume backend resorces, use carefully.
  • use_disk_cache (bool): If set true, get request will use local cache to reduce the requests to the backend.
  • use_app_cache (bool): If True, the GET request will use the cache of the application. Defaults to False.
  • disk_cache_expire (int): Time in seconds to expire the cache, it None it will use de default set be PumpwoodCache.
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int | None): N parallel request that will be done on backend.
Returns:

Return object with the correspondent pk.

Raises:
  • PumpWoodObjectDoesNotExist: If pk not found on database.
def parallel_retrieve_file( self, model_class: str | list[str], list_pk: list[int], file_field: str | list[str], auth_header: dict | list[dict] = None, save_file: bool | list[bool] = True, save_path: str | list[str] = './', file_name: str | list[str] = None, if_exists: str | list[str] = 'fail', base_filter_skip: list[str] | list[list[str]] = None, n_parallel: int | None = None) -> list[str] | list[dict]:
226    def parallel_retrieve_file(self, model_class: str | list[str],
227                               list_pk: list[int],
228                               file_field: str | list[str],
229                               auth_header: dict | list[dict] = None,
230                               save_file: bool | list[bool] = True,
231                               save_path: str | list[str] = "./",
232                               file_name: str | list[str] = None,
233                               if_exists: str | list[str] = "fail",
234                               base_filter_skip: list[str] | list[list[str]] = None,  #NOQA
235                               n_parallel: int | None = None
236                               ) -> list[str] | list[dict]:
237        """Retrieve a file from PumpWood.
238
239        This function will retrieve file as a single request, depending on the
240        size of the files it would be preferred to use streaming end-point.
241
242        Args:
243            model_class:
244                Class of the model to retrieve file.
245            list_pk:
246                Pk of the object associeted file.
247            file_field:
248                Field of the file to be downloaded.
249            auth_header:
250                Dictionary containing the auth header.
251            save_file:
252                If data is to be saved as file or return get
253                response.
254            save_path:
255                Path of the directory to save file.
256            file_name:
257                Name of the file, if None it will have same name as
258                saved in PumpWood.
259            if_exists:
260                Values must be in {'fail', 'change_name', 'overwrite', 'skip'}.
261                Set what to do if there is a file with same name. Skip
262                will not download file if there is already with same
263                os.path.join(save_path, file_name), file_name must be set
264                for skip argument.
265            auth_header:
266                Auth header to substitute the microservice original
267                at the request (user impersonation).
268            base_filter_skip (list):
269                List of base query filter to be skiped, it is necessary to
270                be superuser to skip base query filters.
271            n_parallel (int):
272                Number of parallel requests, if None will use the default
273                one.
274
275        Returns:
276            May return the file name if save_file=True; If false will return
277            a dictonary with keys `filename` with original file name and
278            `content` with binary data of file content.
279
280        Raises:
281            PumpWoodForbidden:
282                'storage_object attribute not set for view, file operations
283                are disable'. This indicates that storage for this backend
284                was not configured, so it is not possible to make storage
285                operations,
286            PumpWoodForbidden:
287                'file_field must be set on self.file_fields dictionary'. This
288                indicates that the `file_field` parameter is not listed as
289                a file field on the backend.
290            PumpWoodObjectDoesNotExist:
291                'field [{}] not found or null at object'. This indicates that
292                the file field requested is not present on object fields.
293            PumpWoodObjectDoesNotExist:
294                'Object not found in storage [{}]'. This indicates that the
295                file associated with file_field is not avaiable at the
296                storage. This should not ocorrur, it might have a manual
297                update at the model_class table or manual removal/rename of
298                files on storage.
299        """
300        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
301
302        # Convert arguments to list
303        list_model_class = self.convert_to_list(
304            argument=model_class, length=len(list_pk))
305        list_file_field = self.convert_to_list(
306            argument=file_field, length=len(list_pk))
307        list_auth_header = self.convert_to_list(
308            argument=auth_header, length=len(list_pk))
309        list_save_file = self.convert_to_list(
310            argument=save_file, length=len(list_pk))
311        list_save_path = self.convert_to_list(
312            argument=save_path, length=len(list_pk))
313        list_file_name = self.convert_to_list(
314            argument=file_name, length=len(list_pk))
315        list_if_exists = self.convert_to_list(
316            argument=if_exists, length=len(list_pk))
317        list_base_filter_skip = self.convert_to_list(
318            argument=base_filter_skip, length=len(list_pk),
319            force_replicate=True)
320
321        column_arg = {
322            'model_class': list_model_class,
323            'pk': list_pk,
324            'file_field': list_file_field,
325            'auth_header': list_auth_header,
326            'save_file': list_save_file,
327            'save_path': list_save_path,
328            'file_name': list_file_name,
329            'if_exists': list_if_exists,
330            'base_filter_skip': list_base_filter_skip}
331        function_args = self.transpose_args(dict_list=column_arg)
332        return self.parallel_call(
333            function=self.retrieve_file, function_args=function_args,
334            n_parallel=n_parallel)

Retrieve a file from PumpWood.

This function will retrieve file as a single request, depending on the size of the files it would be preferred to use streaming end-point.

Arguments:
  • model_class: Class of the model to retrieve file.
  • list_pk: Pk of the object associeted file.
  • file_field: Field of the file to be downloaded.
  • auth_header: Dictionary containing the auth header.
  • save_file: If data is to be saved as file or return get response.
  • save_path: Path of the directory to save file.
  • file_name: Name of the file, if None it will have same name as saved in PumpWood.
  • if_exists: Values must be in {'fail', 'change_name', 'overwrite', 'skip'}. Set what to do if there is a file with same name. Skip will not download file if there is already with same os.path.join(save_path, file_name), file_name must be set for skip argument.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int): Number of parallel requests, if None will use the default one.
Returns:

May return the file name if save_file=True; If false will return a dictonary with keys filename with original file name and content with binary data of file content.

Raises:
  • PumpWoodForbidden: 'storage_object attribute not set for view, file operations are disable'. This indicates that storage for this backend was not configured, so it is not possible to make storage operations,
  • PumpWoodForbidden: 'file_field must be set on self.file_fields dictionary'. This indicates that the file_field parameter is not listed as a file field on the backend.
  • PumpWoodObjectDoesNotExist: 'field [{}] not found or null at object'. This indicates that the file field requested is not present on object fields.
  • PumpWoodObjectDoesNotExist: 'Object not found in storage [{}]'. This indicates that the file associated with file_field is not avaiable at the storage. This should not ocorrur, it might have a manual update at the model_class table or manual removal/rename of files on storage.
def parallel_retrieve_streaming_file( self, model_class: str, list_pk: list[int], file_field: str | list[str], file_name: str | list[str], auth_header: dict | list[dict] = None, save_path: str | list[str] = './', if_exists: str | list[str] = 'fail', base_filter_skip: list | list[list] = None, n_parallel: int | None = None) -> str:
336    def parallel_retrieve_streaming_file(self, model_class: str,
337                                         list_pk: list[int],
338                                         file_field: str | list[str],
339                                         file_name: str | list[str],
340                                         auth_header: dict | list[dict] = None,
341                                         save_path: str | list[str] = "./",
342                                         if_exists: str | list[str] = "fail",
343                                         base_filter_skip: list | list[list] = None, # NOQA
344                                         n_parallel: int | None = None
345                                         ) -> str:
346        """Retrieve a file from PumpWood using streaming to retrieve content.
347
348        This funcion uses file streaming to retrieve file content, it should be
349        prefered when dealing with large (bigger than 10Mb) files transfer.
350        Using this end-point the file is not loaded on backend memory content
351        is transfered by chucks that are read at the storage and transfered
352        to user.
353
354        It will necessarily save the content as a file, there is not the
355        possibility of retrieving the content directly from request.
356
357        Args:
358            model_class:
359                Class of the model to retrieve file.
360            list_pk (list):
361                Pk of the object associeted file.
362            file_field:
363                Field of the file to be downloaded.
364            auth_header:
365                Dictionary containing the auth header.
366            save_path:
367                Path of the directory to save file.
368            file_name:
369                Name of the file, if None it will have same name as
370                saved in PumpWood.
371            if_exists:
372                Values must be in {'fail', 'change_name', 'overwrite'}.
373                Set what to do if there is a file with same name.
374            auth_header:
375                Auth header to substitute the microservice original
376                at the request (user impersonation).
377            base_filter_skip (list):
378                List of base query filter to be skiped, it is necessary to
379                be superuser to skip base query filters.
380            n_parallel (int):
381                Number of parallel requests, if None will use the default
382                one.
383
384        Returns:
385            Returns the file path that recived the file content.
386
387        Raises:
388            PumpWoodForbidden:
389                'storage_object attribute not set for view, file operations
390                are disable'. This indicates that storage for this backend
391                was not configured, so it is not possible to make storage
392                operations,
393            PumpWoodForbidden:
394                'file_field must be set on self.file_fields dictionary'. This
395                indicates that the `file_field` parameter is not listed as
396                a file field on the backend.
397            PumpWoodObjectDoesNotExist:
398                'field [{}] not found or null at object'. This indicates that
399                the file field requested is not present on object fields.
400            PumpWoodObjectDoesNotExist:
401                'Object not found in storage [{}]'. This indicates that the
402                file associated with file_field is not avaiable at the
403                storage. This should not ocorrur, it might have a manual
404                update at the model_class table or manual removal/rename of
405                files on storage.
406        """
407        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
408
409        # Convert arguments to list
410        list_model_class = self.convert_to_list(
411            argument=model_class, length=len(list_pk))
412        list_file_field = self.convert_to_list(
413            argument=file_field, length=len(list_pk))
414        list_file_name = self.convert_to_list(
415            argument=file_name, length=len(list_pk))
416        list_auth_header = self.convert_to_list(
417            argument=auth_header, length=len(list_pk))
418        list_save_path = self.convert_to_list(
419            argument=save_path, length=len(list_pk))
420        list_if_exists = self.convert_to_list(
421            argument=if_exists, length=len(list_pk))
422        list_base_filter_skip = self.convert_to_list(
423            argument=base_filter_skip, length=len(list_pk),
424            force_replicate=True)
425
426        column_arg = {
427            'model_class': list_model_class,
428            'pk': list_pk,
429            'file_field': list_file_field,
430            'file_name': list_file_name,
431            'auth_header': list_auth_header,
432            'save_path': list_save_path,
433            'if_exists': list_if_exists,
434            'base_filter_skip': list_base_filter_skip,
435        }
436        function_args = self.transpose_args(dict_list=column_arg)
437        return self.parallel_call(
438            function=self.retrieve_streaming_file, function_args=function_args,
439            n_parallel=n_parallel)

Retrieve a file from PumpWood using streaming to retrieve content.

This funcion uses file streaming to retrieve file content, it should be prefered when dealing with large (bigger than 10Mb) files transfer. Using this end-point the file is not loaded on backend memory content is transfered by chucks that are read at the storage and transfered to user.

It will necessarily save the content as a file, there is not the possibility of retrieving the content directly from request.

Arguments:
  • model_class: Class of the model to retrieve file.
  • list_pk (list): Pk of the object associeted file.
  • file_field: Field of the file to be downloaded.
  • auth_header: Dictionary containing the auth header.
  • save_path: Path of the directory to save file.
  • file_name: Name of the file, if None it will have same name as saved in PumpWood.
  • if_exists: Values must be in {'fail', 'change_name', 'overwrite'}. Set what to do if there is a file with same name.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int): Number of parallel requests, if None will use the default one.
Returns:

Returns the file path that recived the file content.

Raises:
  • PumpWoodForbidden: 'storage_object attribute not set for view, file operations are disable'. This indicates that storage for this backend was not configured, so it is not possible to make storage operations,
  • PumpWoodForbidden: 'file_field must be set on self.file_fields dictionary'. This indicates that the file_field parameter is not listed as a file field on the backend.
  • PumpWoodObjectDoesNotExist: 'field [{}] not found or null at object'. This indicates that the file field requested is not present on object fields.
  • PumpWoodObjectDoesNotExist: 'Object not found in storage [{}]'. This indicates that the file associated with file_field is not avaiable at the storage. This should not ocorrur, it might have a manual update at the model_class table or manual removal/rename of files on storage.
class ABCParallelSaveMicroservice(pumpwood_communication.microservice_abc.parallel.base.ABCParallelBaseMicroservice):
 12class ABCParallelSaveMicroservice(ABCParallelBaseMicroservice):
 13    """Abstract class for parallel calls at Pumpwood end-points."""
 14
 15    def parallel_save(self, list_obj_dict: list[dict],
 16                      files: list[dict] = None,
 17                      auth_header: dict = None,
 18                      fields: list[str] | list[list[str]] = None,
 19                      default_fields: bool | list[bool] = False,
 20                      foreign_key_fields: bool | list[bool] = False,
 21                      related_fields: bool | list[bool] = False,
 22                      base_filter_skip: list | list[list[str]] = None,
 23                      n_parallel: int | None = None,
 24                      upsert: bool | list[bool] = False,
 25                      ) -> List[dict]:
 26        """Save or Update a new object.
 27
 28        Function to save or update a new model_class object. If obj_dict['pk']
 29        is None or not defined a new object will be created. The obj
 30        model class is defided at obj_dict['model_class'] and if not defined an
 31        PumpWoodObjectSavingException will be raised.
 32
 33        If files argument is set, request will be transfered using a multipart
 34        request file files mapping file key to file field on backend.
 35
 36        Args:
 37            list_obj_dict:
 38                Model data dictionary. It must have 'model_class'
 39                key and if 'pk' key is not defined a new object will
 40                be created, else object with pk will be updated.
 41            files:
 42                A dictionary of files to be added to as a multi-part
 43                post request. File must be passed as a file object with read
 44                bytes.
 45            auth_header:
 46                Auth header to substitute the microservice original
 47                at the request (user impersonation).
 48            fields:
 49                Set the fields to be returned by the list end-point.
 50            default_fields:
 51                Boolean, if true and fields arguments None will return the
 52                default fields set for list by the backend.
 53            foreign_key_fields:
 54                Return forenging key objects. It will return the fk
 55                corresponding object. Ex: `created_by_id` reference to
 56                a user `model_class` the correspondent to User will be
 57                returned at `created_by`.
 58            related_fields:
 59                Return related fields objects. Related field objects are
 60                objects that have a forenging key associated with this
 61                model_class, results will be returned as a list of
 62                dictionaries usually in a field with `_set` at end.
 63                Returning related_fields consume backend resorces, use
 64                carefully.
 65            base_filter_skip (list):
 66                List of base query filter to be skiped, it is necessary to
 67                be superuser to skip base query filters.
 68            n_parallel (int):
 69                Number of parallel requests, if None will use the default
 70                one.
 71            upsert (bool):
 72                Perform an upsert operation, if the object associated with the
 73                pk is not found, it will be inserted on the database.
 74
 75        Returns:
 76            Return updated/created object data.
 77
 78        Raises:
 79            PumpWoodObjectSavingException:
 80                'To save an object obj_dict must have model_class defined.'
 81                This indicates that the obj_dict must have key `model_class`
 82                indicating model class of the object that will be
 83                updated/created.
 84            PumpWoodObjectDoesNotExist:
 85                'Requested object {model_class}[{pk}] not found.'. This
 86                indicates that the pk passed on obj_dict was not found on
 87                backend database.
 88            PumpWoodIntegrityError:
 89                Error raised when IntegrityError is raised on database. This
 90                might ocorrur when saving objects that does not respect
 91                uniqueness restriction on database or other IntegrityError
 92                like removal of foreign keys with related data.
 93            PumpWoodObjectSavingException:
 94                Return error at object validation on de-serializing the
 95                object or files with unexpected extensions.
 96        """
 97        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 98
 99        # Not coping files because it has pointers to files... don't know
100        # how would that react
101        list_files = None
102        if files is None:
103            list_files = [None] * len(list_obj_dict)
104        else:
105            list_files = files
106
107        if len(list_files) != len(list_obj_dict):
108            msg = (
109                "When parallel save it is necessary that, if provided, "
110                "len(files) == len(list_obj_dict)")
111            raise PumpWoodException(msg)
112
113        # Convert dataframe to list
114        if isinstance(list_obj_dict, pd.DataFrame):
115            list_obj_dict = list_obj_dict.to_dict("records")
116
117        list_auth_header = self.convert_to_list(
118            argument=auth_header, length=len(list_obj_dict))
119        list_fields = self.convert_to_list(
120            argument=fields, length=len(list_obj_dict),
121            force_replicate=True)
122        list_default_fields = self.convert_to_list(
123            argument=default_fields, length=len(list_obj_dict))
124        list_foreign_key_fields = self.convert_to_list(
125            argument=foreign_key_fields, length=len(list_obj_dict))
126        list_related_fields = self.convert_to_list(
127            argument=related_fields, length=len(list_obj_dict))
128        list_base_filter_skip = self.convert_to_list(
129            argument=base_filter_skip, length=len(list_obj_dict),
130            force_replicate=True)
131        list_upsert = self.convert_to_list(
132            argument=upsert, length=len(list_obj_dict))
133
134        column_arg = {
135            'obj_dict': list_obj_dict,
136            'auth_header': list_auth_header,
137            'fields': list_fields,
138            'default_fields': list_default_fields,
139            'foreign_key_fields': list_foreign_key_fields,
140            'related_fields': list_related_fields,
141            'base_filter_skip': list_base_filter_skip,
142            'files': list_files,
143            'upsert': list_upsert
144        }
145        function_args = self.transpose_args(dict_list=column_arg)
146        return self.parallel_call(
147            function=self.save, function_args=function_args,
148            n_parallel=n_parallel)
149
150    def parallel_bulk_save(self, model_class: str,
151                           data_to_save: pd.DataFrame | list[dict],
152                           n_parallel: int = None, chunksize: int = None,
153                           base_filter_skip: list[str] | list[list[str]] = None, # NOQA
154                           auth_header: dict = None):
155        """Break data_to_save in many parallel bulk_save requests.
156
157        Args:
158            model_class:
159                Model class of the data that will be saved.
160            data_to_save:
161                Data that will be saved
162            chunksize:
163                Length of each parallel bulk save chunk. If not set,
164                uses ``PUMPWOOD_COMMUNICATION__PARALLEL_CHUNK_SIZE`` env
165                variable (legacy spelling supported), default 10000.
166            n_parallel:
167                Number of simultaneous requests. If not set, uses
168                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
169                (legacy spelling supported), default 4.
170            auth_header:
171                Auth header to substitute the microservice original
172                at the request (user impersonation).
173            base_filter_skip (list):
174                List of base query filter to be skiped, it is necessary to
175                be superuser to skip base query filters.
176
177        Returns:
178            list of the responses of bulk_save.
179        """
180        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
181        if type(data_to_save) is list:
182            data_to_save = pd.DataFrame(data_to_save)
183
184        chunksize = (
185            PARALLEL_CHUNK_SIZE if chunksize is None else chunksize)
186
187        # Break dataframe in chunks to
188        chunks = break_in_chunks(
189            df_to_break=data_to_save, chunksize=chunksize)
190        list_model_class = self.convert_to_list(
191            argument=model_class, length=len(chunks))
192        list_auth_header = self.convert_to_list(
193            argument=auth_header, length=len(chunks))
194        list_base_filter_skip = self.convert_to_list(
195            argument=base_filter_skip, length=len(chunks),
196            force_replicate=True)
197
198        column_arg = {
199            'model_class': list_model_class,
200            'data_to_save': chunks,
201            'auth_header': list_auth_header,
202            'base_filter_skip': list_base_filter_skip}
203        function_args = self.transpose_args(dict_list=column_arg)
204        return self.parallel_call(
205            function=self.bulk_save, function_args=function_args,
206            n_parallel=n_parallel)

Abstract class for parallel calls at Pumpwood end-points.

def parallel_save( self, list_obj_dict: list[dict], files: list[dict] = None, auth_header: dict = None, fields: list[str] | list[list[str]] = None, default_fields: bool | list[bool] = False, foreign_key_fields: bool | list[bool] = False, related_fields: bool | list[bool] = False, base_filter_skip: list | list[list[str]] = None, n_parallel: int | None = None, upsert: bool | list[bool] = False) -> List[dict]:
 15    def parallel_save(self, list_obj_dict: list[dict],
 16                      files: list[dict] = None,
 17                      auth_header: dict = None,
 18                      fields: list[str] | list[list[str]] = None,
 19                      default_fields: bool | list[bool] = False,
 20                      foreign_key_fields: bool | list[bool] = False,
 21                      related_fields: bool | list[bool] = False,
 22                      base_filter_skip: list | list[list[str]] = None,
 23                      n_parallel: int | None = None,
 24                      upsert: bool | list[bool] = False,
 25                      ) -> List[dict]:
 26        """Save or Update a new object.
 27
 28        Function to save or update a new model_class object. If obj_dict['pk']
 29        is None or not defined a new object will be created. The obj
 30        model class is defided at obj_dict['model_class'] and if not defined an
 31        PumpWoodObjectSavingException will be raised.
 32
 33        If files argument is set, request will be transfered using a multipart
 34        request file files mapping file key to file field on backend.
 35
 36        Args:
 37            list_obj_dict:
 38                Model data dictionary. It must have 'model_class'
 39                key and if 'pk' key is not defined a new object will
 40                be created, else object with pk will be updated.
 41            files:
 42                A dictionary of files to be added to as a multi-part
 43                post request. File must be passed as a file object with read
 44                bytes.
 45            auth_header:
 46                Auth header to substitute the microservice original
 47                at the request (user impersonation).
 48            fields:
 49                Set the fields to be returned by the list end-point.
 50            default_fields:
 51                Boolean, if true and fields arguments None will return the
 52                default fields set for list by the backend.
 53            foreign_key_fields:
 54                Return forenging key objects. It will return the fk
 55                corresponding object. Ex: `created_by_id` reference to
 56                a user `model_class` the correspondent to User will be
 57                returned at `created_by`.
 58            related_fields:
 59                Return related fields objects. Related field objects are
 60                objects that have a forenging key associated with this
 61                model_class, results will be returned as a list of
 62                dictionaries usually in a field with `_set` at end.
 63                Returning related_fields consume backend resorces, use
 64                carefully.
 65            base_filter_skip (list):
 66                List of base query filter to be skiped, it is necessary to
 67                be superuser to skip base query filters.
 68            n_parallel (int):
 69                Number of parallel requests, if None will use the default
 70                one.
 71            upsert (bool):
 72                Perform an upsert operation, if the object associated with the
 73                pk is not found, it will be inserted on the database.
 74
 75        Returns:
 76            Return updated/created object data.
 77
 78        Raises:
 79            PumpWoodObjectSavingException:
 80                'To save an object obj_dict must have model_class defined.'
 81                This indicates that the obj_dict must have key `model_class`
 82                indicating model class of the object that will be
 83                updated/created.
 84            PumpWoodObjectDoesNotExist:
 85                'Requested object {model_class}[{pk}] not found.'. This
 86                indicates that the pk passed on obj_dict was not found on
 87                backend database.
 88            PumpWoodIntegrityError:
 89                Error raised when IntegrityError is raised on database. This
 90                might ocorrur when saving objects that does not respect
 91                uniqueness restriction on database or other IntegrityError
 92                like removal of foreign keys with related data.
 93            PumpWoodObjectSavingException:
 94                Return error at object validation on de-serializing the
 95                object or files with unexpected extensions.
 96        """
 97        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 98
 99        # Not coping files because it has pointers to files... don't know
100        # how would that react
101        list_files = None
102        if files is None:
103            list_files = [None] * len(list_obj_dict)
104        else:
105            list_files = files
106
107        if len(list_files) != len(list_obj_dict):
108            msg = (
109                "When parallel save it is necessary that, if provided, "
110                "len(files) == len(list_obj_dict)")
111            raise PumpWoodException(msg)
112
113        # Convert dataframe to list
114        if isinstance(list_obj_dict, pd.DataFrame):
115            list_obj_dict = list_obj_dict.to_dict("records")
116
117        list_auth_header = self.convert_to_list(
118            argument=auth_header, length=len(list_obj_dict))
119        list_fields = self.convert_to_list(
120            argument=fields, length=len(list_obj_dict),
121            force_replicate=True)
122        list_default_fields = self.convert_to_list(
123            argument=default_fields, length=len(list_obj_dict))
124        list_foreign_key_fields = self.convert_to_list(
125            argument=foreign_key_fields, length=len(list_obj_dict))
126        list_related_fields = self.convert_to_list(
127            argument=related_fields, length=len(list_obj_dict))
128        list_base_filter_skip = self.convert_to_list(
129            argument=base_filter_skip, length=len(list_obj_dict),
130            force_replicate=True)
131        list_upsert = self.convert_to_list(
132            argument=upsert, length=len(list_obj_dict))
133
134        column_arg = {
135            'obj_dict': list_obj_dict,
136            'auth_header': list_auth_header,
137            'fields': list_fields,
138            'default_fields': list_default_fields,
139            'foreign_key_fields': list_foreign_key_fields,
140            'related_fields': list_related_fields,
141            'base_filter_skip': list_base_filter_skip,
142            'files': list_files,
143            'upsert': list_upsert
144        }
145        function_args = self.transpose_args(dict_list=column_arg)
146        return self.parallel_call(
147            function=self.save, function_args=function_args,
148            n_parallel=n_parallel)

Save or Update a new object.

Function to save or update a new model_class object. If obj_dict['pk'] is None or not defined a new object will be created. The obj model class is defided at obj_dict['model_class'] and if not defined an PumpWoodObjectSavingException will be raised.

If files argument is set, request will be transfered using a multipart request file files mapping file key to file field on backend.

Arguments:
  • list_obj_dict: Model data dictionary. It must have 'model_class' key and if 'pk' key is not defined a new object will be created, else object with pk will be updated.
  • files: A dictionary of files to be added to as a multi-part post request. File must be passed as a file object with read bytes.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • fields: Set the fields to be returned by the list end-point.
  • default_fields: Boolean, if true and fields arguments None will return the default fields set for list by the backend.
  • foreign_key_fields: Return forenging key objects. It will return the fk corresponding object. Ex: created_by_id reference to a user model_class the correspondent to User will be returned at created_by.
  • related_fields: Return related fields objects. Related field objects are objects that have a forenging key associated with this model_class, results will be returned as a list of dictionaries usually in a field with _set at end. Returning related_fields consume backend resorces, use carefully.
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
  • n_parallel (int): Number of parallel requests, if None will use the default one.
  • upsert (bool): Perform an upsert operation, if the object associated with the pk is not found, it will be inserted on the database.
Returns:

Return updated/created object data.

Raises:
  • PumpWoodObjectSavingException: 'To save an object obj_dict must have model_class defined.' This indicates that the obj_dict must have key model_class indicating model class of the object that will be updated/created.
  • PumpWoodObjectDoesNotExist: 'Requested object {model_class}[{pk}] not found.'. This indicates that the pk passed on obj_dict was not found on backend database.
  • PumpWoodIntegrityError: Error raised when IntegrityError is raised on database. This might ocorrur when saving objects that does not respect uniqueness restriction on database or other IntegrityError like removal of foreign keys with related data.
  • PumpWoodObjectSavingException: Return error at object validation on de-serializing the object or files with unexpected extensions.
def parallel_bulk_save( self, model_class: str, data_to_save: pandas.DataFrame | list[dict], n_parallel: int = None, chunksize: int = None, base_filter_skip: list[str] | list[list[str]] = None, auth_header: dict = None):
150    def parallel_bulk_save(self, model_class: str,
151                           data_to_save: pd.DataFrame | list[dict],
152                           n_parallel: int = None, chunksize: int = None,
153                           base_filter_skip: list[str] | list[list[str]] = None, # NOQA
154                           auth_header: dict = None):
155        """Break data_to_save in many parallel bulk_save requests.
156
157        Args:
158            model_class:
159                Model class of the data that will be saved.
160            data_to_save:
161                Data that will be saved
162            chunksize:
163                Length of each parallel bulk save chunk. If not set,
164                uses ``PUMPWOOD_COMMUNICATION__PARALLEL_CHUNK_SIZE`` env
165                variable (legacy spelling supported), default 10000.
166            n_parallel:
167                Number of simultaneous requests. If not set, uses
168                ``PUMPWOOD_COMMUNICATION__N_PARALLEL`` env variable
169                (legacy spelling supported), default 4.
170            auth_header:
171                Auth header to substitute the microservice original
172                at the request (user impersonation).
173            base_filter_skip (list):
174                List of base query filter to be skiped, it is necessary to
175                be superuser to skip base query filters.
176
177        Returns:
178            list of the responses of bulk_save.
179        """
180        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
181        if type(data_to_save) is list:
182            data_to_save = pd.DataFrame(data_to_save)
183
184        chunksize = (
185            PARALLEL_CHUNK_SIZE if chunksize is None else chunksize)
186
187        # Break dataframe in chunks to
188        chunks = break_in_chunks(
189            df_to_break=data_to_save, chunksize=chunksize)
190        list_model_class = self.convert_to_list(
191            argument=model_class, length=len(chunks))
192        list_auth_header = self.convert_to_list(
193            argument=auth_header, length=len(chunks))
194        list_base_filter_skip = self.convert_to_list(
195            argument=base_filter_skip, length=len(chunks),
196            force_replicate=True)
197
198        column_arg = {
199            'model_class': list_model_class,
200            'data_to_save': chunks,
201            'auth_header': list_auth_header,
202            'base_filter_skip': list_base_filter_skip}
203        function_args = self.transpose_args(dict_list=column_arg)
204        return self.parallel_call(
205            function=self.bulk_save, function_args=function_args,
206            n_parallel=n_parallel)

Break data_to_save in many parallel bulk_save requests.

Arguments:
  • model_class: Model class of the data that will be saved.
  • data_to_save: Data that will be saved
  • chunksize: Length of each parallel bulk save chunk. If not set, uses PUMPWOOD_COMMUNICATION__PARALLEL_CHUNK_SIZE env variable (legacy spelling supported), default 10000.
  • n_parallel: Number of simultaneous requests. If not set, uses PUMPWOOD_COMMUNICATION__N_PARALLEL env variable (legacy spelling supported), default 4.
  • auth_header: Auth header to substitute the microservice original at the request (user impersonation).
  • base_filter_skip (list): List of base query filter to be skiped, it is necessary to be superuser to skip base query filters.
Returns:

list of the responses of bulk_save.

class ABCParallelBatchMicroservice(pumpwood_communication.microservice_abc.parallel.base.ABCParallelBaseMicroservice):
  9class ABCParallelBatchMicroservice(ABCParallelBaseMicroservice):
 10    """Abstract class for parallel calls at Pumpwood end-points."""
 11
 12    def parallel_pivot(self, model_class: str,
 13                       list_args: list[dict[str, Any]],
 14                       columns: None | List[str] = None,
 15                       format: str = 'list',
 16                       variables: List[str] = None,
 17                       fields: List[str] = None,
 18                       show_deleted: bool = False,
 19                       add_pk_column: bool = False,
 20                       auth_header: dict = None,
 21                       as_dataframe: bool = False,
 22                       n_parallel: int = None,
 23                       flat_results: bool = True) -> Union[List[dict], pd.DataFrame]: # NOQA
 24        """Execute parallel calls for pivot end-points.
 25
 26        This method performs multiple calls to a pivot end-point in
 27        parallel, allowing to request aggregations with different filters
 28        or exclusions simultaneously.
 29
 30        Args:
 31            model_class (str):
 32                Model class of the end-point.
 33            list_args (list[dict[str, Any]]):
 34                A list of dictionaries containing `filter_dict` and
 35                `exclude_dict` to be used in the parallel queries.
 36            columns (list):
 37                List of columns to group by on the pivot operation.
 38                Defaults to None.
 39            format (str):
 40                Format of the returned data. Defaults to 'list'.
 41            variables (list):
 42                Variables to be used in the pivot values. Defaults to None.
 43            fields (list):
 44                Fields to be returned by the end-point. Defaults to None.
 45            show_deleted (bool):
 46                If deleted objects should be included. Defaults to False.
 47            add_pk_column (bool):
 48                If the primary key column should be added to the result.
 49                Defaults to False.
 50            auth_header (dict):
 51                Authorization header to impersonate the request.
 52                Defaults to None.
 53            as_dataframe (bool):
 54                If True, returns the results as a pandas DataFrame.
 55                Defaults to False.
 56            n_parallel (int):
 57                Number of parallel requests. If None, uses default.
 58                Defaults to None.
 59            flat_results (bool):
 60                If True, results will be joined into a single list or
 61                DataFrame. Defaults to True.
 62
 63        Returns:
 64            Union[List[dict], pd.DataFrame]:
 65                Containing objects resulting from the pivot operation,
 66                either as a list of dictionaries or a pandas DataFrame.
 67
 68        Raises:
 69            No specific raises mapped for this execution.
 70        """
 71        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 72
 73        # Convert arguments to list
 74        dict_list = self.expand_list_args(
 75            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
 76        list_model_class = self.convert_to_list(
 77            argument=model_class, length=len(list_args))
 78        list_auth_header = self.convert_to_list(
 79            argument=auth_header, length=len(list_args))
 80        list_fields = self.convert_to_list(
 81            argument=fields, length=len(list_args),
 82            force_replicate=True)
 83        list_format = self.convert_to_list(
 84            argument=format, length=len(list_args))
 85        list_columns = self.convert_to_list(
 86            argument=columns, length=len(list_args),
 87            force_replicate=True)
 88        list_variables = self.convert_to_list(
 89            argument=variables, length=len(list_args),
 90            force_replicate=True)
 91        list_show_deleted = self.convert_to_list(
 92            argument=show_deleted, length=len(list_args),
 93            force_replicate=True)
 94        list_add_pk_column = self.convert_to_list(
 95            argument=add_pk_column, length=len(list_args),
 96            force_replicate=True)
 97        list_as_dataframe = self.convert_to_list(
 98            argument=as_dataframe, length=len(list_args),
 99            force_replicate=True)
100
101        column_arg = {
102            'model_class': list_model_class,
103            'auth_header': list_auth_header,
104            'fields': list_fields,
105            'columns': list_columns,
106            'format': list_format,
107            'variables': list_variables,
108            'show_deleted': list_show_deleted,
109            'add_pk_column': list_add_pk_column,
110            "as_dataframe": list_as_dataframe}
111        column_arg.update(dict_list)
112
113        function_args = self.transpose_args(dict_list=column_arg)
114        parallel_results = self.parallel_call(
115            function=self.pivot, function_args=function_args,
116            n_parallel=n_parallel)
117        if flat_results:
118            return self.flatten_parallel(
119                parallel_results, as_dataframe=as_dataframe)
120        else:
121            return parallel_results

Abstract class for parallel calls at Pumpwood end-points.

def parallel_pivot( self, model_class: str, list_args: list[dict[str, typing.Any]], columns: Optional[List[str]] = None, format: str = 'list', variables: List[str] = None, fields: List[str] = None, show_deleted: bool = False, add_pk_column: bool = False, auth_header: dict = None, as_dataframe: bool = False, n_parallel: int = None, flat_results: bool = True) -> Union[List[dict], pandas.DataFrame]:
 12    def parallel_pivot(self, model_class: str,
 13                       list_args: list[dict[str, Any]],
 14                       columns: None | List[str] = None,
 15                       format: str = 'list',
 16                       variables: List[str] = None,
 17                       fields: List[str] = None,
 18                       show_deleted: bool = False,
 19                       add_pk_column: bool = False,
 20                       auth_header: dict = None,
 21                       as_dataframe: bool = False,
 22                       n_parallel: int = None,
 23                       flat_results: bool = True) -> Union[List[dict], pd.DataFrame]: # NOQA
 24        """Execute parallel calls for pivot end-points.
 25
 26        This method performs multiple calls to a pivot end-point in
 27        parallel, allowing to request aggregations with different filters
 28        or exclusions simultaneously.
 29
 30        Args:
 31            model_class (str):
 32                Model class of the end-point.
 33            list_args (list[dict[str, Any]]):
 34                A list of dictionaries containing `filter_dict` and
 35                `exclude_dict` to be used in the parallel queries.
 36            columns (list):
 37                List of columns to group by on the pivot operation.
 38                Defaults to None.
 39            format (str):
 40                Format of the returned data. Defaults to 'list'.
 41            variables (list):
 42                Variables to be used in the pivot values. Defaults to None.
 43            fields (list):
 44                Fields to be returned by the end-point. Defaults to None.
 45            show_deleted (bool):
 46                If deleted objects should be included. Defaults to False.
 47            add_pk_column (bool):
 48                If the primary key column should be added to the result.
 49                Defaults to False.
 50            auth_header (dict):
 51                Authorization header to impersonate the request.
 52                Defaults to None.
 53            as_dataframe (bool):
 54                If True, returns the results as a pandas DataFrame.
 55                Defaults to False.
 56            n_parallel (int):
 57                Number of parallel requests. If None, uses default.
 58                Defaults to None.
 59            flat_results (bool):
 60                If True, results will be joined into a single list or
 61                DataFrame. Defaults to True.
 62
 63        Returns:
 64            Union[List[dict], pd.DataFrame]:
 65                Containing objects resulting from the pivot operation,
 66                either as a list of dictionaries or a pandas DataFrame.
 67
 68        Raises:
 69            No specific raises mapped for this execution.
 70        """
 71        n_parallel = self.get_n_parallel(n_parallel=n_parallel)
 72
 73        # Convert arguments to list
 74        dict_list = self.expand_list_args(
 75            list_args=list_args, keys=['filter_dict', 'exclude_dict'])
 76        list_model_class = self.convert_to_list(
 77            argument=model_class, length=len(list_args))
 78        list_auth_header = self.convert_to_list(
 79            argument=auth_header, length=len(list_args))
 80        list_fields = self.convert_to_list(
 81            argument=fields, length=len(list_args),
 82            force_replicate=True)
 83        list_format = self.convert_to_list(
 84            argument=format, length=len(list_args))
 85        list_columns = self.convert_to_list(
 86            argument=columns, length=len(list_args),
 87            force_replicate=True)
 88        list_variables = self.convert_to_list(
 89            argument=variables, length=len(list_args),
 90            force_replicate=True)
 91        list_show_deleted = self.convert_to_list(
 92            argument=show_deleted, length=len(list_args),
 93            force_replicate=True)
 94        list_add_pk_column = self.convert_to_list(
 95            argument=add_pk_column, length=len(list_args),
 96            force_replicate=True)
 97        list_as_dataframe = self.convert_to_list(
 98            argument=as_dataframe, length=len(list_args),
 99            force_replicate=True)
100
101        column_arg = {
102            'model_class': list_model_class,
103            'auth_header': list_auth_header,
104            'fields': list_fields,
105            'columns': list_columns,
106            'format': list_format,
107            'variables': list_variables,
108            'show_deleted': list_show_deleted,
109            'add_pk_column': list_add_pk_column,
110            "as_dataframe": list_as_dataframe}
111        column_arg.update(dict_list)
112
113        function_args = self.transpose_args(dict_list=column_arg)
114        parallel_results = self.parallel_call(
115            function=self.pivot, function_args=function_args,
116            n_parallel=n_parallel)
117        if flat_results:
118            return self.flatten_parallel(
119                parallel_results, as_dataframe=as_dataframe)
120        else:
121            return parallel_results

Execute parallel calls for pivot end-points.

This method performs multiple calls to a pivot end-point in parallel, allowing to request aggregations with different filters or exclusions simultaneously.

Arguments:
  • model_class (str): Model class of the end-point.
  • list_args (list[dict[str, Any]]): A list of dictionaries containing filter_dict and exclude_dict to be used in the parallel queries.
  • columns (list): List of columns to group by on the pivot operation. Defaults to None.
  • format (str): Format of the returned data. Defaults to 'list'.
  • variables (list): Variables to be used in the pivot values. Defaults to None.
  • fields (list): Fields to be returned by the end-point. Defaults to None.
  • show_deleted (bool): If deleted objects should be included. Defaults to False.
  • add_pk_column (bool): If the primary key column should be added to the result. Defaults to False.
  • auth_header (dict): Authorization header to impersonate the request. Defaults to None.
  • as_dataframe (bool): If True, returns the results as a pandas DataFrame. Defaults to False.
  • n_parallel (int): Number of parallel requests. If None, uses default. Defaults to None.
  • flat_results (bool): If True, results will be joined into a single list or DataFrame. Defaults to True.
Returns:

Union[List[dict], pd.DataFrame]: Containing objects resulting from the pivot operation, either as a list of dictionaries or a pandas DataFrame.

Raises:
  • No specific raises mapped for this execution.