Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polls: add new django setting to enable or disable the unregistered poll #1692

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion adhocracy4/polls/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from rest_framework import serializers

from adhocracy4.dashboard import components
Expand Down Expand Up @@ -153,7 +154,12 @@ def get_has_user_vote(self, poll):
return False

def update(self, instance, data):
instance.allow_unregistered_users = data.get("allow_unregistered_users", False)
if getattr(settings, "A4_POLL_ENABLE_UNREGISTERED_USERS", False):
instance.allow_unregistered_users = data.get(
"allow_unregistered_users", False
)
else:
instance.allow_unregistered_users = False
instance.save()
# Delete removed questions from the database
instance.questions.exclude(
Expand Down
9 changes: 5 additions & 4 deletions adhocracy4/polls/static/PollDashboard/EditPollManagement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,11 @@ export const EditPollManagement = (props) => {
onSubmit={(e) => handleSubmit(e)} onChange={() => removeAlert()}
className="editpoll__questions"
>
<div>
<label htmlFor="allowUnregisteredUsersCheckbox">Allow unregistered user to vote</label>
<input type="checkbox" id="allowUnregisteredUsersCheckbox" onChange={() => setAllowUnregisteredUsers((state) => !state)} checked={allowUnregisteredUsers} />
</div>
{props.enableUnregisteredUsers &&
<div>
<label htmlFor="allowUnregisteredUsersCheckbox">Allow unregistered user to vote</label>
<input type="checkbox" id="allowUnregisteredUsersCheckbox" onChange={() => setAllowUnregisteredUsers((state) => !state)} checked={allowUnregisteredUsers} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use a couple more attributes:
aria-checked={allowUnregisteredUsers} // Adds an ARIA attribute for screen readers
aria-label="Toggle allowing unregistered users to vote"

Copy link
Contributor Author

@goapunk goapunk Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hom3mad3 are you ok with us collecting these things for the ui refactor story for now instead of fixing them as part of this issue ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, because now with the new backend changes we keep making the code worse. could you guys maybe make a list of stuff you encounter?

</div>}
<FlipMove easing="cubic-bezier(0.25, 0.5, 0.75, 1)">
{
questions.map((question, index, arr) => {
Expand Down
6 changes: 2 additions & 4 deletions adhocracy4/polls/static/react_poll_management.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import { EditPollManagement } from './PollDashboard/EditPollManagement'
function init () {
ReactWidgetInit('a4', 'poll-management',
function (el) {
const pollId = el.dataset.pollId
const props = JSON.parse(el.dataset.attributes)
const root = createRoot(el)

const reloadOnSuccess = JSON.parse(el.getAttribute('data-reloadOnSuccess'))

root.render(
<EditPollManagement pollId={pollId} reloadOnSuccess={reloadOnSuccess} />
<EditPollManagement {...props} />
)
}
)
Expand Down
16 changes: 9 additions & 7 deletions adhocracy4/polls/templatetags/react_polls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ def react_polls(poll: Poll):

@register.simple_tag
def react_poll_form(poll, reload_on_success=False):
attributes = {
"pollId": poll.pk,
"reloadOnSuccess": reload_on_success,
"enableUnregisteredUsers": getattr(
settings, "A4_POLL_ENABLE_UNREGISTERED_USERS", False
),
}
reload_on_success = json.dumps(reload_on_success)

return format_html(
(
'<div data-a4-widget="poll-management" data-poll-id="{pollId}" '
' data-reloadOnSuccess="{reload_on_success}">'
"</div>"
),
pollId=poll.pk,
reload_on_success=reload_on_success,
'<div data-a4-widget="poll-management" data-attributes="{attributes}"></div>',
attributes=json.dumps(attributes),
)
6 changes: 6 additions & 0 deletions changelog/8381.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Added

- add option to allow unregistered users to vote in a poll:
- the feature is controlled via a new django setting `A4_POLL_ENABLE_UNREGISTERED_USERS` to enable or disable it
- add a new captcha react component to integrate the captcha in the poll