Skip to content

Commit

Permalink
Wrap suggestion voting permission check in a function
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Čihař <michal@cihar.com>
  • Loading branch information
nijel committed Apr 16, 2015
1 parent c898d1c commit a7cff89
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
5 changes: 3 additions & 2 deletions weblate/html/translate.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{% can_suggest user unit.translation as user_can_suggest %}
{% can_accept_suggestion user unit.translation as user_can_accept_suggestion %}
{% can_delete_suggestion user unit.translation as user_can_delete_suggestion %}
{% can_vote_suggestion user unit.translation as user_can_vote_suggestion %}


{% block extra_meta %}
Expand Down Expand Up @@ -180,13 +181,13 @@ <h5><strong>
{% endif %}
</strong></h5>
{% format_translation suggestion.target unit.translation.language unit.target %}
{% if unit.can_vote_suggestions %}
{% if user_can_vote_suggestions %}
<p class="help-block">
{% blocktrans count count=suggestion.get_num_votes %}{{ count }} vote{% plural %}{{ count }} votes{% endblocktrans %}
</p>
{% endif %}
<div class="pull-right flip">
{% if unit.can_vote_suggestions and perms.trans.vote_suggestion %}
{% if user_can_vote_suggestions %}
<button type="submit" class="btn btn-success btn-xs" name="upvote" value="{{ suggestion.id }}"><i class="fa fa-thumbs-up"></i> {% trans "Vote for" %}</button>
<button type="submit" class="btn btn-danger btn-xs" name="downvote" value="{{ suggestion.id }}"><i class="fa fa-thumbs-down"></i> {% trans "Vote against" %}</button>
{% endif %}
Expand Down
6 changes: 0 additions & 6 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,6 @@ def nearby(self):
position__lte=self.position + appsettings.NEARBY_MESSAGES,
).select_related()

def can_vote_suggestions(self):
"""
Whether we can vote for suggestions.
"""
return self.translation.subproject.suggestion_voting

def translate(self, request, new_target, new_fuzzy):
"""
Stores new translation of a unit.
Expand Down
3 changes: 2 additions & 1 deletion weblate/trans/models/unitdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from weblate.lang.models import Language
from weblate.trans.checks import CHECKS
from weblate.trans.models.changes import Change
from weblate.trans.permissions import can_vote_suggestion
from weblate.accounts.avatar import get_user_display
from weblate.accounts.models import notify_new_suggestion, notify_new_comment

Expand Down Expand Up @@ -60,7 +61,7 @@ def add(self, unit, target, request):
)

# Add unit vote
if user is not None and unit.can_vote_suggestions():
if can_vote_suggestion(user, unit.translation):
suggestion.add_vote(
unit.translation,
request,
Expand Down
17 changes: 17 additions & 0 deletions weblate/trans/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,20 @@ def can_delete_suggestion(user, translation):
Checks whether user can delete suggestions to given translation.
"""
return can_edit(user, translation, 'trans.delete_suggestion')


def can_vote_suggestions(user, translation):
"""
Checks whether user can vote suggestions on given translation.
"""
if translation is None or user is None:
return False
if not translation.subproject.suggestion_voting:
return False
if translation.subproject.locked:
return False
if not user.has_perm('trans.vote_suggestion'):
return False
if translation.is_template() and not user.has_perm('trans.save_template'):
return False
return True
7 changes: 7 additions & 0 deletions weblate/trans/templatetags/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ def can_delete_suggestion(user, translation):
return weblate.trans.permissions.can_delete_suggestion(
user, translation
)


@register.assignment_tag
def can_vote_suggestion(user, translation):
return weblate.trans.permissions.can_vote_suggestion(
user, translation
)

0 comments on commit a7cff89

Please sign in to comment.