From 206203f053e7400cf27662c88784399c1a210716 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Thu, 11 Apr 2024 22:21:52 +0530 Subject: [PATCH 1/2] fix(poll): Always retain sequence of poll options Keep the sequence of options in poll results consistent with the sequence of options in the poll. This will reduce the cognitive load on users when they are trying to interpret the results of a poll. --- raven/api/raven_poll.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/raven/api/raven_poll.py b/raven/api/raven_poll.py index 7863b7890..625178d36 100644 --- a/raven/api/raven_poll.py +++ b/raven/api/raven_poll.py @@ -136,10 +136,9 @@ def get_all_votes(poll_id): if not frappe.has_permission(doctype="Raven Poll", doc=poll_id, ptype="read"): frappe.throw(_("You do not have permission to access this poll"), frappe.PermissionError) - # Check if the poll is anonymous - is_anonymous = frappe.get_cached_value("Raven Poll", poll_id, "is_anonymous") + poll_doc = frappe.get_cached_doc("Raven Poll", poll_id) - if is_anonymous: + if poll_doc.is_anonymous: frappe.throw(_("This poll is anonymous. You do not have permission to access the votes."), frappe.PermissionError) else: # Get all votes for this poll @@ -150,19 +149,12 @@ def get_all_votes(poll_id): ) # Initialize results dictionary - results = {} + results = {option.name: { 'users': [], 'count': option.votes } for option in poll_doc.options} # Process votes for vote in votes: option = vote['option'] - if option not in results: - results[option] = { - 'users': [vote['user_id']], - 'count': 1 - } - else: - results[option]['users'].append(vote['user_id']) - results[option]['count'] += 1 + results[option]['users'].append(vote['user_id']) # Calculate total votes total_votes = sum(result['count'] for result in results.values()) From 2a1fbd466731200b403f5ea3f1a5efdd5300c352 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Thu, 11 Apr 2024 22:41:02 +0530 Subject: [PATCH 2/2] fix: Only show voted options in the poll results --- raven/api/raven_poll.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raven/api/raven_poll.py b/raven/api/raven_poll.py index 625178d36..7367f52cd 100644 --- a/raven/api/raven_poll.py +++ b/raven/api/raven_poll.py @@ -149,7 +149,7 @@ def get_all_votes(poll_id): ) # Initialize results dictionary - results = {option.name: { 'users': [], 'count': option.votes } for option in poll_doc.options} + results = {option.name: { 'users': [], 'count': option.votes } for option in poll_doc.options if option.votes} # Process votes for vote in votes: