Skip to content

Commit

Permalink
has_unread search filter
Browse files Browse the repository at this point in the history
  • Loading branch information
folix-01 committed Jul 31, 2024
1 parent 2435bf9 commit a09c265
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/collective/feedback/restapi/services/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,23 @@ def get_data(self):
if data["last_vote"] < date:
data["last_vote"] = date

# avg calculation
pages_to_remove = []

for uid, feedback in feedbacks.items():
# avg calculation
feedback["vote"] = feedback.pop("vote_sum") / feedback.pop("vote_num")

has_undread = query.get("has_unread", None)

# Use has_unread filter
if has_undread is not None:
has_undread = not (has_undread == "false") and has_undread == "true"
if feedback["has_unread"] != has_undread:
pages_to_remove.append(uid)

for uid in pages_to_remove:
del feedbacks[uid]

result = list(feedbacks.values())

# sort
Expand Down
64 changes: 63 additions & 1 deletion src/collective/feedback/tests/test_feedbacks_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
class TestGet(unittest.TestCase):
layer = RESTAPI_TESTING

def add_record(self, date=None, vote="", uid="", comment="", title=""):
def add_record(self, date=None, vote="", uid="", comment="", title="", read=False):
if not date:
date = datetime.now()

soup = get_soup("feedback_soup", self.portal)

transaction.commit()

record = Record()
record.attrs["vote"] = vote
record.attrs["date"] = date
Expand All @@ -37,6 +40,9 @@ def add_record(self, date=None, vote="", uid="", comment="", title=""):
record.attrs["uid"] = uid
if title:
record.attrs["title"] = title
if read:
record.attrs["read"] = read

soup.add(record)
transaction.commit()

Expand Down Expand Up @@ -233,3 +239,59 @@ def test_users_without_permission_dont_have_can_delete_feedbacks_action(self):

self.assertIn("can_delete_feedbacks", res["actions"])
self.assertFalse(res["actions"]["can_delete_feedbacks"])

def test_has_unread_filter(self):
response = self.api_session.get(self.url)
res = response.json()

self.assertEqual(res["items_total"], 0)

now = datetime.now()

self.add_record(vote=1, comment="is ok", date=now, read=True)

response = self.api_session.get(self.url)

res = response.json()

self.assertEqual(res["items_total"], 1)

self.assertEqual(
res["items"],
[
{
"comments": 1,
"last_vote": json_compatible(now),
"title": "",
"uid": "",
"vote": 1.0,
"has_unread": False,
}
],
)

self.add_record(vote=1, comment="is ok", date=now, read=False)

# has_unread=true case
response = self.api_session.get(self.url + "?has_unread=true")

self.assertEqual(
response.json()["items"],
[
{
"comments": 2,
"last_vote": json_compatible(now),
"title": "",
"uid": "",
"vote": 1.0,
"has_unread": True,
}
],
)

self.add_record(vote=1, comment="is ok", date=now)

# has_unread=false case
response = self.api_session.get(self.url + "?has_unread=false")

self.assertEqual(response.json()["items"], [])

0 comments on commit a09c265

Please sign in to comment.