Skip to content

Commit

Permalink
Suggestions: Avoid passing no longer needed translation object
Browse files Browse the repository at this point in the history
It was neded when foreign key was not used to link suggestions to units,
but it is no longer true for several releases.

Issue #49
  • Loading branch information
nijel committed Jul 29, 2020
1 parent 47e4053 commit b0f65a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions weblate/trans/models/suggestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __str__(self):
)

@transaction.atomic
def accept(self, translation, request, permission="suggestion.accept"):
def accept(self, request, permission="suggestion.accept"):
if not request.user.has_perm(permission, self.unit):
messages.error(request, _("Failed to accept suggestion!"))
return
Expand Down Expand Up @@ -184,7 +184,7 @@ def add_vote(self, request, value):
# Automatic accepting
required_votes = self.unit.translation.component.suggestion_autoaccept
if required_votes and self.get_num_votes() >= required_votes:
self.accept(self.unit.translation, request, "suggestion.vote")
self.accept(request, "suggestion.vote")

def get_checks(self):
# Build fake unit to run checks
Expand Down
20 changes: 8 additions & 12 deletions weblate/trans/views/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ def handle_revert(unit, request, next_unit_url):
return HttpResponseRedirect(next_unit_url)


def check_suggest_permissions(request, mode, translation, suggestion):
def check_suggest_permissions(request, mode, unit, suggestion):
"""Check permission for suggestion handling."""
user = request.user
if mode in ("accept", "accept_edit"):
if not user.has_perm("suggestion.accept", translation):
if not user.has_perm("suggestion.accept", unit):
messages.error(
request, _("You do not have privilege to accept suggestions!")
)
Expand All @@ -346,15 +346,15 @@ def check_suggest_permissions(request, mode, translation, suggestion):
)
return False
elif mode in ("upvote", "downvote"):
if not user.has_perm("suggestion.vote", translation):
if not user.has_perm("suggestion.vote", unit):
messages.error(
request, _("You do not have privilege to vote for suggestions!")
)
return False
return True


def handle_suggestions(translation, request, this_unit_url, next_unit_url):
def handle_suggestions(request, unit, this_unit_url, next_unit_url):
"""Handle suggestion deleting/accepting."""
sugid = ""
params = ("accept", "accept_edit", "delete", "spam", "upvote", "downvote")
Expand All @@ -370,20 +370,18 @@ def handle_suggestions(translation, request, this_unit_url, next_unit_url):

# Fetch suggestion
try:
suggestion = Suggestion.objects.get(
pk=int(sugid), unit__translation=translation
)
suggestion = Suggestion.objects.get(pk=int(sugid), unit=unit)
except (Suggestion.DoesNotExist, ValueError):
messages.error(request, _("Invalid suggestion!"))
return HttpResponseRedirect(this_unit_url)

# Permissions check
if not check_suggest_permissions(request, mode, translation, suggestion):
if not check_suggest_permissions(request, mode, unit, suggestion):
return HttpResponseRedirect(this_unit_url)

# Perform operation
if "accept" in request.POST or "accept_edit" in request.POST:
suggestion.accept(translation, request)
suggestion.accept(request)
if "accept" in request.POST:
redirect_url = next_unit_url
elif "delete" in request.POST or "spam" in request.POST:
Expand Down Expand Up @@ -470,9 +468,7 @@ def translate(request, project, component, lang):
response = handle_translate(request, unit, this_unit_url, next_unit_url)
elif not locked or "delete" in request.POST or "spam" in request.POST:
# Handle accepting/deleting suggestions
response = handle_suggestions(
translation, request, this_unit_url, next_unit_url
)
response = handle_suggestions(request, unit, this_unit_url, next_unit_url)

# Handle translation merging
elif "merge" in request.GET and not locked:
Expand Down

0 comments on commit b0f65a0

Please sign in to comment.