Skip to content

Commit

Permalink
polls: add new django setting to enable or disable the unregistered poll
Browse files Browse the repository at this point in the history
voting

- add missing changelog
  • Loading branch information
goapunk authored and m4ra committed Nov 13, 2024
1 parent cb21e2e commit d00d656
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
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} />
</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

0 comments on commit d00d656

Please sign in to comment.