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

[7008]apps/budgeting/vote-button: only show vote button if has sessio… #4885

Merged
merged 2 commits into from
Feb 3, 2023
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
9 changes: 6 additions & 3 deletions meinberlin/apps/budgeting/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,12 @@ def list(self, request, *args, **kwargs):
permissions["view_vote_count"] = user.has_perm(
"meinberlin_budgeting.view_vote_count", self.module
)
permissions["view_votes_left"] = self._has_valid_token_in_session(
response
) and user.has_perm("meinberlin_budgeting.add_vote", self.module)
permissions[
"has_voting_permission_and_valid_token"
] = self._has_valid_token_in_session(response) and user.has_perm(
"meinberlin_budgeting.add_vote", self.module
)

response.data["permissions"] = permissions
return response

Expand Down
2 changes: 1 addition & 1 deletion meinberlin/apps/budgeting/assets/BudgetingProposalList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const BudgetingProposalList = (props) => {

return (
<>
{(meta?.permissions.view_votes_left && meta?.token_info) &&
{(meta?.permissions.has_voting_permission_and_valid_token) &&
<div className="module-content--light">
<div className="container">
<div className="offset-lg-3 col-lg-6">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const BudgetingProposalListItem = (props) => {
<span className="list-item__author test">{proposal.creator}</span>
{date + ' - ' + proposal.reference_number}
</div>
{proposal.vote_allowed && (
{permissions.has_voting_permission_and_valid_token && proposal.vote_allowed && (
<VoteButton
objectID={proposal.pk}
tokenvoteApiUrl={tokenvoteApiUrl}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const permissions = {
view_support_count: false,
view_rate_count: true,
view_comment_count: true,
view_votes_left: true
has_voting_permission_and_valid_token: false
}

test('Budgeting Proposal List without list item (empty)', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

{% has_perm 'meinberlin_budgeting.view_vote_count' request.user module as may_view_vote_count %}
{% if may_view_vote_count %}
<p class="u-bottom-divider u-align-center">{% blocktranslate with amount=object.vote_count %}This idea received a total of <span class="u-primary">1{{ amount }}</span> votes{% endblocktranslate %}</p>
<p class="u-bottom-divider u-align-center">{% blocktranslate with amount=object.token_vote_count %}This idea received a total of <span class="u-primary">{{ amount }}</span> votes{% endblocktranslate %}</p>
{% endif %}

{% has_perm 'meinberlin_budgeting.vote_proposal' request.user proposal as vote_allowed %}
{% if vote_allowed %}
{% if vote_allowed and has_valid_token_in_session %}
<div class="u-bottom-divider">
{% react_proposals_vote view.module proposal %}
</div>
Expand Down
27 changes: 25 additions & 2 deletions meinberlin/apps/budgeting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def has_valid_token_in_session(self, request):
if module_key in request.session["voting_tokens"]:
return (
VotingToken.get_voting_token_by_hash(
token_hash=request.session["voting_tokens"][module_key], module=self.module
token_hash=request.session["voting_tokens"][module_key],
module=self.module,
)
is not None
)
Expand Down Expand Up @@ -125,7 +126,9 @@ def post(self, request, *args, **kwargs):
class ProposalDetailView(idea_views.AbstractIdeaDetailView):
model = models.Proposal
queryset = (
models.Proposal.objects.annotate_positive_rating_count().annotate_negative_rating_count()
models.Proposal.objects.annotate_positive_rating_count()
.annotate_negative_rating_count()
.annotate_token_vote_count()
)
permission_required = "meinberlin_budgeting.view_proposal"

Expand Down Expand Up @@ -153,11 +156,31 @@ def get_back(self):
return None, None
return None, None

def has_valid_token_in_session(self, request):
"""Return whether a valid token is stored in the session.

The token is valid if it is valid for the respective module.
"""
if "voting_tokens" in request.session:
module_key = str(self.module.id)
if module_key in request.session["voting_tokens"]:
return (
VotingToken.get_voting_token_by_hash(
token_hash=request.session["voting_tokens"][module_key],
module=self.module,
)
is not None
)
return False

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
back_link, back_string = self.get_back()
context["back"] = back_link
context["back_string"] = back_string
context["has_valid_token_in_session"] = self.has_valid_token_in_session(
self.request
)
return context


Expand Down