Commit e4988cc5 authored by Michal Čihař's avatar Michal Čihař

Implement checks for translation locking (issue #60)

parent 7e10b166
......@@ -897,6 +897,51 @@ class Translation(models.Model):
return 0
return round(self.translated * 100.0 / self.total, 1)
def is_locked(self, request = None):
'''
Check whether the translation is locked and
possibly emmits messages if request object is
provided.
'''
# Check for project lock
if self.subproject.locked:
if request is not None:
messages.error(request, _('This translation is currently locked for updates!'))
return True
# Check for translation lock
if self.is_user_locked():
if request is not None:
messages.error(
request,
_('This translation is locked by %s for translation till %s!') % (
self.lock_user.get_full_name(),
self.lock_timestamp,
)
)
return True
return False
def is_user_locked(self):
'''
Checks whether there is valid user lock on this translation.
'''
# Any user?
if self.lock_user is None:
return False
# Is lock still valid?
if self.lock_timestamp < datetime.now():
# Clear the lock
self.lock_user = None
self.save()
return False
return True
def get_non_translated(self):
return self.total - self.translated
......
......@@ -343,8 +343,8 @@ def show_translation(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project, enabled = True)
last_changes = Change.objects.filter(unit__translation = obj).order_by('-timestamp')[:10]
if obj.subproject.locked:
messages.error(request, _('This translation is currently locked for updates!'))
# Check locks
obj.is_locked(request)
# How much is user allowed to configure upload?
if request.user.has_perm('trans.author_translation'):
......@@ -655,8 +655,8 @@ def get_filter_name(rqtype, search_query):
def translate(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project, enabled = True)
if obj.subproject.locked:
messages.error(request, _('This translation is currently locked for updates!'))
# Check locks
locked = obj.is_locked(request)
if request.user.is_authenticated():
profile = request.user.get_profile()
......@@ -686,7 +686,7 @@ def translate(request, project, subproject, lang):
))
form = TranslationForm(request.POST)
if form.is_valid() and not obj.subproject.locked:
if form.is_valid() and not locked:
# Check whether translation is not outdated
obj.check_sync()
try:
......@@ -807,7 +807,7 @@ def translate(request, project, subproject, lang):
messages.error(request, _('Message you wanted to translate is no longer available!'))
# Handle accepting/deleting suggestions
if not obj.subproject.locked and ('accept' in request.GET or 'delete' in request.GET):
if not locked and ('accept' in request.GET or 'delete' in request.GET):
# Check for authenticated users
if not request.user.is_authenticated():
messages.error(request, _('You need to log in to be able to manage suggestions!'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment