Skip to content

Commit

Permalink
feat: Update fetch_content method to handle optional community_ids an…
Browse files Browse the repository at this point in the history
…d improve admin access logic
  • Loading branch information
abdullai-t committed Nov 24, 2024
1 parent 4db63ea commit 0605bd5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/api/handlers/media_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ def fetch_content(self, request):
"""Fetches image content related communities that admins can browse through"""
context: Context = request.context
args: dict = context.args
self.validator.expect("community_ids", "str_list", is_required=True)
self.validator.expect("community_ids", "str_list", is_required=False)

Check warning on line 108 in src/api/handlers/media_library.py

View check run for this annotation

Codecov / codecov/patch

src/api/handlers/media_library.py#L108

Added line #L108 was not covered by tests
self.validator.expect("lower_limit", int, is_required=False)
self.validator.expect("upper_limit", int, is_required=False)
args, err = self.validator.verify(args, strict=True)
if err:
return err
images, error = self.service.fetch_content(args)
images, error = self.service.fetch_content(context, args)

Check warning on line 114 in src/api/handlers/media_library.py

View check run for this annotation

Codecov / codecov/patch

src/api/handlers/media_library.py#L114

Added line #L114 was not covered by tests
if error:
return error
return MassenergizeResponse(data=images)
Expand Down
4 changes: 2 additions & 2 deletions src/api/services/media_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def generate_hashes(self, args, context):
return None, error
return response, None

def fetch_content(self, args):
images, error = self.store.fetch_content(args)
def fetch_content(self, context, args):
images, error = self.store.fetch_content(context, args)

Check warning on line 44 in src/api/services/media_library.py

View check run for this annotation

Codecov / codecov/patch

src/api/services/media_library.py#L44

Added line #L44 was not covered by tests
if error:
return None, error
return self.organiseData(data=serialize_all(images, True), args=args), None
Expand Down
12 changes: 11 additions & 1 deletion src/api/store/media_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,21 @@ def find_images(self, args, _):
images = Media.objects.filter(pk__in=ids)
return images, None

def fetch_content(self, args):
def fetch_content(self, context: Context, args):
com_ids = args.get("community_ids") or []
upper_limit = args.get("upper_limit")
lower_limit = args.get("lower_limit")
images = None

if not com_ids:
if context.user_is_community_admin:
communities, _ = get_admin_communities(context)
com_ids = [c.id for c in communities]
elif context.user_is_super_admin:
com_ids = [c.id for c in Community.objects.all()]

Check warning on line 196 in src/api/store/media_library.py

View check run for this annotation

Codecov / codecov/patch

src/api/store/media_library.py#L191-L196

Added lines #L191 - L196 were not covered by tests
else:
com_ids = []

Check warning on line 198 in src/api/store/media_library.py

View check run for this annotation

Codecov / codecov/patch

src/api/store/media_library.py#L198

Added line #L198 was not covered by tests

if upper_limit and lower_limit:
images = (
Media.objects.filter(
Expand Down

0 comments on commit 0605bd5

Please sign in to comment.