{"action":"opened","issue":{"url":"https://api.github.com/repos/torchbox/wagtailsurveys/issues/2","repository_url":"https://api.github.com/repos/torchbox/wagtailsurveys","labels_url":"https://api.github.com/repos/torchbox/wagtailsurveys/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/torchbox/wagtailsurveys/issues/2/comments","events_url":"https://api.github.com/repos/torchbox/wagtailsurveys/issues/2/events","html_url":"https://github.com/torchbox/wagtailsurveys/issues/2","id":158097801,"number":2,"title":"Slight issue with the 'Show results' sample code","user":{"login":"maltem-za","id":1404980,"avatar_url":"https://avatars.githubusercontent.com/u/1404980?v=3","gravatar_id":"","url":"https://api.github.com/users/maltem-za","html_url":"https://github.com/maltem-za","followers_url":"https://api.github.com/users/maltem-za/followers","following_url":"https://api.github.com/users/maltem-za/following{/other_user}","gists_url":"https://api.github.com/users/maltem-za/gists{/gist_id}","starred_url":"https://api.github.com/users/maltem-za/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maltem-za/subscriptions","organizations_url":"https://api.github.com/users/maltem-za/orgs","repos_url":"https://api.github.com/users/maltem-za/repos","events_url":"https://api.github.com/users/maltem-za/events{/privacy}","received_events_url":"https://api.github.com/users/maltem-za/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-06-02T09:34:21Z","updated_at":"2016-06-02T09:34:21Z","closed_at":null,"body":"The 'Show results' sample code does not take into account that the answer is a list if the 'Checkboxes' field type is used.\r\n\r\n`question_stats[answer] = question_stats.get(answer, 0) + 1` fails with:\r\nTypeError: unhashable type: 'list'\r\nunder these circumstances.\r\n\r\nModifying the code as follows solves this problem:\r\n```python\r\n# snip\r\n# Get all submissions for current page\r\nsubmissions = self.get_submission_class().objects.filter(page=self)\r\nfor submission in submissions:\r\n\tdata = submission.get_data()\r\n\r\n\t# Count results for each question\r\n\tfor name, label in data_fields:\r\n\t\tanswer = data.get(name)\r\n\t\tif answer is None:\r\n\t\t\t# Something wrong with data.\r\n\t\t\t# Probably you have changed questions\r\n\t\t\t# and now we are receiving answers for old questions.\r\n\t\t\t# Just skip them.\r\n\t\t\tcontinue\r\n\r\n\t\tif type(answer) is list:\r\n\t\t\t# answer is a list if the field type is 'Checkboxes'\r\n\t\t\tanswer = u', '.join(answer)\r\n\r\n\t\tquestion_stats = results.get(label, {})\r\n\t\tquestion_stats[answer] = question_stats.get(answer, 0) + 1\r\n\t\tresults[label] = question_stats\r\n# /snip\r\n```"}}
{"actor":{"display_login":"maltem-za"}}