Skip to content

Commit

Permalink
Add sort and filter options
Browse files Browse the repository at this point in the history
  • Loading branch information
folix-01 committed Oct 30, 2024
1 parent c34ae36 commit 63a3562
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/collective/feedback/restapi/services/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from collective.feedback.interfaces import ICollectiveFeedbackStore

DEFAULT_SORT_KEY = "date"


@implementer(IPublishTraverse)
class FeedbackGet(Service):
Expand All @@ -35,6 +37,9 @@ def reply(self):
results = self.get_single_object_feedbacks(self.params[0])
else:
results = self.get_data()

results = self.filter_unread(self.sort_results(results))

batch = HypermediaBatch(self.request, results)
data = {
"@id": batch.canonical_url,
Expand All @@ -48,6 +53,24 @@ def reply(self):
data["actions"] = {"can_delete_feedbacks": self.can_delete_feedbacks()}
return data

def sort_results(self, results):
sort_on = self.request.get("sort_on")
sort_order = self.request.get("sort_order", "")

return sorted(
results,
key=lambda item: item.get(sort_on, DEFAULT_SORT_KEY),
reverse=sort_order == "descending",
)

def filter_unread(self, results):
unread = self.request.get("unread")

if unread:
return list(filter(lambda item: not item.get("read"), results))

return results

def can_delete_feedbacks(self):
return api.user.has_permission("collective.feedback: Delete Feedbacks")

Expand Down

0 comments on commit 63a3562

Please sign in to comment.