From 227868f9b03e1c24a3ca46dbd4ac6c45161c4cf5 Mon Sep 17 00:00:00 2001 From: Travis Cashion <58757351+TCashion@users.noreply.github.com> Date: Fri, 15 Sep 2023 15:28:11 -0600 Subject: [PATCH 1/3] tc/INT-7084/change-org-fetch-assets-attachments --- brandfolder/organization.py | 9 ++-- brandfolder/resource_container.py | 73 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/brandfolder/organization.py b/brandfolder/organization.py index b717610..c1baa8c 100644 --- a/brandfolder/organization.py +++ b/brandfolder/organization.py @@ -1,8 +1,8 @@ from brandfolder.resource import Resource -from brandfolder.resource_container import ResourceContainer -from brandfolder.brandfolder import Brandfolder +from brandfolder.resource_container import ResourceContainer, ModifiedResourceContainer from brandfolder.asset import Asset from brandfolder.attachment import Attachment +from brandfolder.brandfolder import Brandfolder class Organization(Resource): @@ -11,9 +11,10 @@ class Organization(Resource): def __init__(self, client, **kwargs): super().__init__(client, **kwargs) + self.brandfolders = ResourceContainer(client, Brandfolder, parent=self) + self.assets = ModifiedResourceContainer(client, Asset, parent=self) + self.attachments = ModifiedResourceContainer(client, Attachment, parent=self) - self.assets = ResourceContainer(client, Asset, parent=self) - self.attachments = ResourceContainer(client, Attachment, parent=self) def __repr__(self): return f'<{self.resource_name} {self.attributes["slug"]}: {self.id}>' diff --git a/brandfolder/resource_container.py b/brandfolder/resource_container.py index ab2b784..a1367c9 100644 --- a/brandfolder/resource_container.py +++ b/brandfolder/resource_container.py @@ -29,6 +29,21 @@ def fetch(self, params=None, per=None, page=None, **kwargs): return [self.resource_class(self.client, data=data, included=included) for data in res['data']] + def paginated_fetch(self, params=None, per=None, page=None, **kwargs): + if params is None: + params = {} + + if per: + params['per'] = per + if page: + params['page'] = page + + res = self.client.get(endpoint=self.endpoint, params=params, **kwargs) + included = res.get('included', []) + + return [self.resource_class(self.client, data=data, included=included) + for data in res['data']], res['meta'] + def first(self, params=None, **kwargs): if params is None: params = {} @@ -45,3 +60,61 @@ def fetch_by_id(self, id, **kwargs): def search(self, query_params, **kwargs): params = {'search': query_params, **kwargs} return self.fetch(params=params) + + +class ModifiedResourceContainer(ResourceContainer): + """ + ModifiedResourceContainer is a modified resource container for organizations to fetch assets and attachments + without using the deprecated endpoints /organizations/{id}/assets and /organizations/{id}/attachments + """ + restricted = ['assets', 'attachments'] + + def fetch(self, params=None, per=100, page=1, **kwargs): + total_resources_to_fetch = per * page + + if self.parent \ + and self.parent.resource_type == 'organizations' \ + and self.resource_type in self.restricted: + + resources = [] + stop_op = False + + brandfolders = self.parent.brandfolders.fetch() + + while len(resources) < total_resources_to_fetch and not stop_op: + for i, brandfolder in enumerate(brandfolders): + method = getattr(brandfolder, 'assets') \ + if self.resource_type == 'assets' \ + else getattr(brandfolder, 'attachments') + + stop_page_for_this_bf = False + p = 1 + while stop_page_for_this_bf is False: + fetched_resources, meta = method.paginated_fetch(per=100, page=p, **kwargs) + resources.extend(fetched_resources) + if len(resources) >= total_resources_to_fetch \ + or len(fetched_resources) == 0 \ + or meta['total_pages'] == p: + stop_page_for_this_bf = True + if i == len(brandfolders) - 1: + stop_op = True + break + else: + p += 1 + + if len(resources) >= total_resources_to_fetch: + resources = resources[:total_resources_to_fetch] + break + elif len(resources) == 0: + stop_op = True + break + + if resources: + resources = resources[-per:] + return resources + + res = self.client.get(endpoint=self.endpoint, params=params, **kwargs) + included = res.get('included', []) + + return [self.resource_class(self.client, data=data, included=included) + for data in res['data']] From 529e7b90b8410068238bae8fc7fc4f9feb1c1501 Mon Sep 17 00:00:00 2001 From: Travis Cashion <58757351+TCashion@users.noreply.github.com> Date: Fri, 15 Sep 2023 15:39:07 -0600 Subject: [PATCH 2/3] tc/INT-7084/change-org-fetch-assets-attachments --- brandfolder/resource_container.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/brandfolder/resource_container.py b/brandfolder/resource_container.py index a1367c9..10a3dd4 100644 --- a/brandfolder/resource_container.py +++ b/brandfolder/resource_container.py @@ -83,35 +83,29 @@ def fetch(self, params=None, per=100, page=1, **kwargs): while len(resources) < total_resources_to_fetch and not stop_op: for i, brandfolder in enumerate(brandfolders): - method = getattr(brandfolder, 'assets') \ - if self.resource_type == 'assets' \ - else getattr(brandfolder, 'attachments') + method = getattr(brandfolder, 'assets') if self.resource_type == 'assets' else getattr(brandfolder, + 'attachments') stop_page_for_this_bf = False p = 1 while stop_page_for_this_bf is False: fetched_resources, meta = method.paginated_fetch(per=100, page=p, **kwargs) resources.extend(fetched_resources) - if len(resources) >= total_resources_to_fetch \ - or len(fetched_resources) == 0 \ - or meta['total_pages'] == p: + if len(resources) >= total_resources_to_fetch or len(fetched_resources) == 0 or meta[ + 'total_pages'] == p: stop_page_for_this_bf = True - if i == len(brandfolders) - 1: - stop_op = True - break else: p += 1 + if i == len(brandfolders) - 1: + stop_op = True + break + if len(resources) >= total_resources_to_fetch: resources = resources[:total_resources_to_fetch] break - elif len(resources) == 0: - stop_op = True - break - if resources: - resources = resources[-per:] - return resources + return resources[-per:] res = self.client.get(endpoint=self.endpoint, params=params, **kwargs) included = res.get('included', []) From 47af55578641ec2da796127337c9b595a791110b Mon Sep 17 00:00:00 2001 From: Travis Cashion <58757351+TCashion@users.noreply.github.com> Date: Fri, 15 Sep 2023 15:43:57 -0600 Subject: [PATCH 3/3] tc/INT-7084/change-org-fetch-assets-attachments --- brandfolder/resource_container.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/brandfolder/resource_container.py b/brandfolder/resource_container.py index 10a3dd4..f0f6ca9 100644 --- a/brandfolder/resource_container.py +++ b/brandfolder/resource_container.py @@ -106,9 +106,6 @@ def fetch(self, params=None, per=100, page=1, **kwargs): break return resources[-per:] - - res = self.client.get(endpoint=self.endpoint, params=params, **kwargs) - included = res.get('included', []) - - return [self.resource_class(self.client, data=data, included=included) - for data in res['data']] + else: + r = ResourceContainer(self.client, self.resource_class, self.parent) + return r.fetch(params=params, per=per, page=page, **kwargs) \ No newline at end of file