Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
converse.js
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
converse.js
Commits
0652bd96
Commit
0652bd96
authored
Feb 19, 2016
by
Michal Čihař
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Be more consistent in method documentation
Signed-off-by:
Michal Čihař
<
michal@cihar.com
>
parent
acd63f2d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
143 additions
and
369 deletions
+143
-369
weblate/trans/models/project.py
weblate/trans/models/project.py
+31
-75
weblate/trans/models/subproject.py
weblate/trans/models/subproject.py
+61
-166
weblate/trans/models/translation.py
weblate/trans/models/translation.py
+51
-128
No files found.
weblate/trans/models/project.py
View file @
0652bd96
...
...
@@ -43,14 +43,11 @@ class ProjectManager(models.Manager):
# pylint: disable=W0232
def
all_acl
(
self
,
user
):
"""
Returns list of projects user is allowed to access.
"""
"""Returns list of projects user is allowed to access."""
return
self
.
get_acl_status
(
user
)[
0
]
def
get_acl_status
(
self
,
user
):
"""
Returns list of projects user is allowed to access
"""Returns list of projects user is allowed to access
and flag whether there is any filtering active.
"""
projects
=
self
.
all
()
...
...
@@ -166,17 +163,12 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
verbose_name_plural
=
ugettext_lazy
(
'Projects'
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
"""
Constructor to initialize some cache properties.
"""
"""Constructor to initialize some cache properties."""
super
(
Project
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
permissions_cache
=
{}
def
has_acl
(
self
,
user
):
"""
Checks whether current user is allowed to access this
project.
"""
"""Checks whether current user is allowed to access this object"""
if
not
self
.
enable_acl
:
return
True
...
...
@@ -189,9 +181,7 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
return
self
.
owners
.
filter
(
id
=
user
.
id
).
exists
()
def
check_acl
(
self
,
request
):
"""
Raises an error if user is not allowed to access this project.
"""
"""Raises an error if user is not allowed to access this project."""
if
not
self
.
has_acl
(
request
.
user
):
messages
.
error
(
request
,
...
...
@@ -200,25 +190,19 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
raise
PermissionDenied
def
all_users
(
self
):
"""
Returns all users having ACL on this project.
"""
"""Returns all users having ACL on this project."""
group
=
Group
.
objects
.
get
(
name
=
self
.
name
)
return
group
.
user_set
.
exclude
(
id__in
=
self
.
owners
.
values_list
(
'id'
,
flat
=
True
)
)
def
add_user
(
self
,
user
):
"""
Adds user based on username of email.
"""
"""Adds user based on username of email."""
group
=
Group
.
objects
.
get
(
name
=
self
.
name
)
user
.
groups
.
add
(
group
)
def
remove_user
(
self
,
user
):
"""
Adds user based on username of email.
"""
"""Adds user based on username of email."""
group
=
Group
.
objects
.
get
(
name
=
self
.
name
)
user
.
groups
.
remove
(
group
)
...
...
@@ -231,31 +215,23 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
)
def
_reverse_url_name
(
self
):
"""
Returns base name for URL reversing.
"""
"""Returns base name for URL reversing."""
return
'project'
def
_reverse_url_kwargs
(
self
):
"""
Returns kwargs for URL reversing.
"""
"""Returns kwargs for URL reversing."""
return
{
'project'
:
self
.
slug
}
def
get_widgets_url
(
self
):
"""
Returns absolute URL for widgets.
"""
"""Returns absolute URL for widgets."""
return
get_site_url
(
reverse
(
'widgets'
,
kwargs
=
{
'project'
:
self
.
slug
})
)
def
get_share_url
(
self
):
"""
Returns absolute URL usable for sharing.
"""
"""Returns absolute URL usable for sharing."""
return
get_site_url
(
reverse
(
'engage'
,
kwargs
=
{
'project'
:
self
.
slug
})
)
...
...
@@ -316,9 +292,7 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
# pylint: disable=W0221
def
_get_percents
(
self
,
lang
=
None
):
"""
Returns percentages of translation status.
"""
"""Returns percentages of translation status."""
# Import translations
from
weblate.trans.models.translation
import
Translation
...
...
@@ -329,17 +303,16 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
# pylint: disable=W0221
def
get_translated_percent
(
self
,
lang
=
None
):
"""
Returns percent of translated strings.
"""
"""Returns percent of translated strings."""
if
lang
is
None
:
return
super
(
Project
,
self
).
get_translated_percent
()
return
self
.
_get_percents
(
lang
)[
0
]
def
get_total
(
self
):
"""
Calculates total number of strings to translate. This is done based on
assumption that all languages have same number of strings.
"""Calculates total number of strings to translate.
This is done based on assumption that all languages have same number
of strings.
"""
totals
=
[]
for
component
in
self
.
subproject_set
.
all
():
...
...
@@ -350,9 +323,10 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
return
sum
(
totals
)
def
get_total_words
(
self
):
"""
Calculates total number of words to translate. This is done based on
assumption that all languages have same number of strings.
"""Calculates total number of words to translate.
This is done based on assumption that all languages have same number
of strings.
"""
totals
=
[]
for
component
in
self
.
subproject_set
.
all
():
...
...
@@ -363,23 +337,17 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
return
sum
(
totals
)
def
get_languages
(
self
):
"""
Returns list of all languages used in project.
"""
"""Returns list of all languages used in project."""
return
Language
.
objects
.
filter
(
translation__subproject__project
=
self
).
distinct
()
def
get_language_count
(
self
):
"""
Returns number of languages used in this project.
"""
"""Returns number of languages used in this project."""
return
self
.
get_languages
().
count
()
def
repo_needs_commit
(
self
):
"""
Checks whether there are some not committed changes.
"""
"""Checks whether there are some not committed changes."""
for
component
in
self
.
subproject_set
.
all
():
if
component
.
repo_needs_commit
():
return
True
...
...
@@ -398,9 +366,7 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
return
False
def
commit_pending
(
self
,
request
,
on_commit
=
True
):
"""
Commits any pending changes.
"""
"""Commits any pending changes."""
ret
=
False
components
=
self
.
all_repo_components
()
...
...
@@ -417,33 +383,25 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
return
ret
def
do_update
(
self
,
request
=
None
,
method
=
None
):
"""
Updates all git repos.
"""
"""Updates all git repos."""
ret
=
True
for
component
in
self
.
all_repo_components
():
ret
&=
component
.
do_update
(
request
,
method
=
method
)
return
ret
def
do_push
(
self
,
request
=
None
):
"""
Pushes all git repos.
"""
"""Pushes all git repos."""
return
self
.
commit_pending
(
request
,
on_commit
=
False
)
def
do_reset
(
self
,
request
=
None
):
"""
Pushes all git repos.
"""
"""Pushes all git repos."""
ret
=
False
for
component
in
self
.
all_repo_components
():
ret
|=
component
.
do_reset
(
request
)
return
ret
def
can_push
(
self
):
"""
Checks whether any suprojects can push.
"""
"""Checks whether any suprojects can push."""
ret
=
False
for
component
in
self
.
subproject_set
.
all
():
ret
|=
component
.
can_push
()
...
...
@@ -451,9 +409,7 @@ class Project(models.Model, PercentMixin, URLMixin, PathMixin):
@
property
def
last_change
(
self
):
"""
Returns date of last change done in Weblate.
"""
"""Returns date of last change done in Weblate."""
components
=
self
.
subproject_set
.
all
()
changes
=
[
component
.
last_change
for
component
in
components
]
changes
=
[
c
for
c
in
changes
if
c
is
not
None
]
...
...
weblate/trans/models/subproject.py
View file @
0652bd96
...
...
@@ -91,9 +91,7 @@ class SubProjectManager(models.Manager):
# pylint: disable=W0232
def
get_linked
(
self
,
val
):
'''
Returns subproject for linked repo.
'''
"""Returns subproject for linked repo."""
if
not
is_repo_link
(
val
):
return
None
project
,
subproject
=
val
[
10
:].
split
(
'/'
,
1
)
...
...
@@ -445,9 +443,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
verbose_name_plural
=
ugettext_lazy
(
'Components'
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
'''
Constructor to initialize some cache properties.
'''
"""Constructor to initialize some cache properties."""
super
(
SubProject
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
_repository_lock
=
None
self
.
_file_format
=
None
...
...
@@ -467,45 +463,32 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
'{0}/{1}: '
.
format
(
self
.
project
.
slug
,
self
.
slug
)
def
has_acl
(
self
,
user
):
'''
Checks whether current user is allowed to access this
subproject.
'''
"""Checks whether current user is allowed to access this object"""
return
self
.
project
.
has_acl
(
user
)
def
check_acl
(
self
,
request
):
'''
Raises an error if user is not allowed to access this project.
'''
"""Raises an error if user is not allowed to access this project."""
self
.
project
.
check_acl
(
request
)
def
_reverse_url_name
(
self
):
'''
Returns base name for URL reversing.
'''
"""Returns base name for URL reversing."""
return
'subproject'
def
_reverse_url_kwargs
(
self
):
'''
Returns kwargs for URL reversing.
'''
"""Returns kwargs for URL reversing."""
return
{
'project'
:
self
.
project
.
slug
,
'subproject'
:
self
.
slug
}
def
get_widgets_url
(
self
):
'''
Returns absolute URL for widgets.
'''
"""Returns absolute URL for widgets."""
return
get_site_url
(
reverse
(
'widgets'
,
kwargs
=
{
'project'
:
self
.
project
.
slug
})
)
def
get_share_url
(
self
):
'''
Returns absolute URL usable for sharing.
'''
"""Returns absolute URL usable for sharing."""
return
get_site_url
(
reverse
(
'engage'
,
kwargs
=
{
'project'
:
self
.
project
.
slug
})
)
...
...
@@ -517,9 +500,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
'%s__%s'
%
(
self
.
project
.
slug
,
self
.
slug
)
def
_get_path
(
self
):
'''
Returns full path to subproject VCS repository.
'''
"""Returns full path to subproject VCS repository."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
get_path
()
else
:
...
...
@@ -527,9 +508,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
@
property
def
repository_lock
(
self
):
'''
Returns lock object for current translation instance.
'''
"""Returns lock object for current translation instance."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
repository_lock
...
...
@@ -545,40 +524,30 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
self
.
_repository_lock
def
can_push
(
self
):
'''
Returns true if push is possible for this subproject.
'''
"""Returns true if push is possible for this subproject."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
can_push
()
return
self
.
push
!=
''
and
self
.
push
is
not
None
@
property
def
is_repo_link
(
self
):
'''
Checks whether repository is just a link for other one.
'''
"""Checks whether repository is just a link for other one."""
return
is_repo_link
(
self
.
repo
)
def
can_add_language
(
self
):
'''
Returns true if new languages can be added.
'''
"""Returns true if new languages can be added."""
return
self
.
new_lang
!=
'none'
@
property
def
linked_subproject
(
self
):
'''
Returns subproject for linked repo.
'''
"""Returns subproject for linked repo."""
if
self
.
_linked_subproject
is
None
:
self
.
_linked_subproject
=
SubProject
.
objects
.
get_linked
(
self
.
repo
)
return
self
.
_linked_subproject
@
property
def
repository
(
self
):
"""
VCS repository object.
"""
"""VCS repository object."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
repository
...
...
@@ -594,9 +563,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
self
.
_repository
def
get_last_remote_commit
(
self
):
'''
Returns latest remote commit we know.
'''
"""Returns latest remote commit we know."""
cache_key
=
'{0}-last-commit'
.
format
(
self
.
get_full_slug
())
result
=
cache
.
get
(
cache_key
)
...
...
@@ -609,9 +576,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
result
def
get_repo_url
(
self
):
'''
Returns link to repository.
'''
"""Returns link to repository."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
get_repo_url
()
if
not
HIDE_REPO_CREDENTIALS
:
...
...
@@ -619,28 +584,23 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
cleanup_repo_url
(
self
.
repo
)
def
get_repo_branch
(
self
):
'''
Returns branch in repository.
'''
"""Returns branch in repository."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
branch
return
self
.
branch
def
get_export_url
(
self
):
'''
Returns URL of exported VCS repository.
'''
"""Returns URL of exported VCS repository."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
git_export
return
self
.
git_export
def
get_repoweb_link
(
self
,
filename
,
line
):
'''
Generates link to source code browser for given file and line.
"""Generates link to source code browser for given file and line
For linked repositories, it is possible to override linked
repository path here.
'''
"""
if
len
(
self
.
repoweb
)
==
0
:
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
get_repoweb_link
(
filename
,
line
)
...
...
@@ -653,9 +613,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
}
def
update_remote_branch
(
self
,
validate
=
False
):
'''
Pulls from remote repository.
'''
"""Pulls from remote repository."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
update_remote_branch
(
validate
)
...
...
@@ -685,10 +643,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
False
def
configure_repo
(
self
,
validate
=
False
):
'''
Ensures repository is correctly configured and points to current
remote.
'''
"""Ensures repository is correctly configured"""
if
self
.
is_repo_link
:
return
...
...
@@ -702,9 +657,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
self
.
update_remote_branch
(
validate
)
def
configure_branch
(
self
):
'''
Ensures local tracking branch exists and is checkouted.
'''
"""Ensures local tracking branch exists and is checkouted."""
if
self
.
is_repo_link
:
return
...
...
@@ -712,9 +665,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
self
.
repository
.
configure_branch
(
self
.
branch
)
def
do_update
(
self
,
request
=
None
,
method
=
None
):
'''
Wrapper for doing repository update and pushing them to translations.
'''
"""Wrapper for doing repository update"""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
do_update
(
request
,
method
=
method
)
...
...
@@ -764,9 +715,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
)
def
do_push
(
self
,
request
,
force_commit
=
True
,
do_update
=
True
):
'''
Wrapper for pushing changes to remote repo.
'''
"""Wrapper for pushing changes to remote repo."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
do_push
(
request
,
force_commit
=
force_commit
,
do_update
=
do_update
...
...
@@ -832,9 +781,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
False
def
do_reset
(
self
,
request
=
None
):
'''
Wrapper for reseting repo to same sources as remote.
'''
"""Wrapper for reseting repo to same sources as remote."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
do_reset
(
request
)
...
...
@@ -876,17 +823,13 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
'weblate://%s/%s'
%
(
self
.
project
.
slug
,
self
.
slug
)
def
get_linked_childs
(
self
):
'''
Returns list of subprojects which link repository to us.
'''
"""Returns list of subprojects which link repository to us."""
return
SubProject
.
objects
.
filter
(
repo
=
self
.
get_repo_link_url
()
)
def
commit_pending
(
self
,
request
,
from_link
=
False
,
skip_push
=
False
):
'''
Checks whether there is any translation which needs commit.
'''
"""Checks whether there is any translation which needs commit."""
if
not
from_link
and
self
.
is_repo_link
:
return
self
.
linked_subproject
.
commit_pending
(
request
,
True
,
skip_push
=
skip_push
...
...
@@ -913,9 +856,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
raise
ParseError
(
str
(
error
))
def
update_branch
(
self
,
request
=
None
,
method
=
None
):
'''
Updates current branch to match remote (if possible).
'''
"""Updates current branch to match remote (if possible)."""
if
self
.
is_repo_link
:
return
self
.
linked_subproject
.
update_branch
(
request
,
method
=
method
)
...
...
@@ -992,9 +933,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
False
def
get_mask_matches
(
self
):
'''
Returns files matching current mask.
'''
"""Returns files matching current mask."""
prefix
=
os
.
path
.
join
(
self
.
get_path
(),
''
)
matches
=
set
([
f
.
replace
(
prefix
,
''
)
...
...
@@ -1015,9 +954,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
def
create_translations
(
self
,
force
=
False
,
langs
=
None
,
request
=
None
,
changed_template
=
False
):
'''
Loads translations from VCS.
'''
"""Loads translations from VCS."""
translations
=
set
()
languages
=
set
()
matches
=
self
.
get_mask_matches
()
...
...
@@ -1074,9 +1011,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
self
.
log_info
(
'updating completed'
)
def
get_lang_code
(
self
,
path
):
'''
Parses language code from path.
'''
"""Parses language code from path."""
# Parse filename
matches
=
self
.
filemask_re
.
match
(
path
)
...
...
@@ -1093,9 +1028,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
code
def
sync_git_repo
(
self
,
validate
=
False
):
'''
Brings VCS repo in sync with current model.
'''
"""Brings VCS repo in sync with current model."""
if
self
.
is_repo_link
:
return
self
.
configure_repo
(
validate
)
...
...
@@ -1104,16 +1037,12 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
self
.
update_branch
()
def
set_default_branch
(
self
):
'''
Set default VCS branch if empty
'''
"""Set default VCS branch if empty"""
if
self
.
branch
==
''
:
self
.
branch
=
VCS_REGISTRY
[
self
.
vcs
].
default_branch
def
clean_repo_link
(
self
):
'''
Validates repository link.
'''
"""Validates repository link."""
try
:
repo
=
SubProject
.
objects
.
get_linked
(
self
.
repo
)
if
repo
is
not
None
and
repo
.
is_repo_link
:
...
...
@@ -1147,9 +1076,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
)
def
clean_lang_codes
(
self
,
matches
):
'''
Validates that there are no double language codes found in the files.
'''
"""Validates that there are no double language codes"""
if
len
(
matches
)
==
0
and
not
self
.
can_add_new_language
():
raise
ValidationError
(
_
(
'The mask did not match any files!'
))
langs
=
set
()
...
...
@@ -1177,9 +1104,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
translated_langs
.
add
(
lang
.
code
)
def
clean_files
(
self
,
matches
):
'''
Validates whether we can parse translation files.
'''
"""Validates whether we can parse translation files."""
notrecognized
=
[]
errors
=
[]
dir_path
=
self
.
get_path
()
...
...
@@ -1212,9 +1137,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
))
def
clean_new_lang
(
self
):
'''
Validates new language choices.
'''
"""Validates new language choices."""
if
self
.
new_lang
==
'add'
:
if
not
self
.
file_format_cls
.
supports_new_language
():
raise
ValidationError
(
_
(
...
...
@@ -1236,9 +1159,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
))
def
clean_template
(
self
):
"""
Validates template value.
"""
"""Validates template value."""
# Test for unexpected template usage
if
self
.
template
!=
''
and
self
.
file_format_cls
.
monolingual
is
False
:
raise
ValidationError
(
...
...
@@ -1270,10 +1191,10 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
)
def
clean
(
self
):
'''
Validator fetches repository and tries to find translation files.
Then
it checks them for validity.
'''
"""Validator fetches repository
It tries to find translation files and
it checks them for validity.
"""
if
self
.
new_lang
==
'url'
and
self
.
project
.
instructions
==
''
:
raise
ValidationError
(
_
(
'Please either fill in instructions URL '
...
...
@@ -1343,24 +1264,20 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
))
def
get_template_filename
(
self
):
'''
Creates absolute filename for template.
'''
"""Creates absolute filename for template."""
return
os
.
path
.
join
(
self
.
get_path
(),
self
.
template
)
def
get_new_base_filename
(
self
):
'''
Creates absolute filename for base file for new translations.
'''
"""Creates absolute filename for base file for new translations."""
if
not
self
.
new_base
:
return
None
return
os
.
path
.
join
(
self
.
get_path
(),
self
.
new_base
)
def
save
(
self
,
*
args
,
**
kwargs
):
'''
Save wrapper which updates backend repository and regenerates
translation data.
'''
"""Save wrapper
It updates backend repository and regenerates
translation data.
"""
self
.
set_default_branch
()
# Detect if VCS config has changed (so that we have to pull the repo)
...
...
@@ -1418,43 +1335,31 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
old
.
project
.
suggestion_set
.
copy
(
self
.
project
)
def
_get_percents
(
self
):
'''
Returns percentages of translation status.
'''
"""Returns percentages of translation status"""
return
self
.
translation_set
.
get_percents
()
def
repo_needs_commit
(
self
):
'''
Checks whether there are some not committed changes.
'''
"""Checks whether there are some not committed changes"""
return
self
.
repository
.
needs_commit
()
def
repo_needs_merge
(
self
):
'''
Checks whether there is something to merge from remote repository.
'''
"""Checks whether there is something to merge from remote repository"""
return
self
.
repository
.
needs_merge
()
def
repo_needs_push
(
self
):
'''
Checks whether there is something to push to remote repository.
'''
"""Checks whether there is something to push to remote repository"""
return
self
.
repository
.
needs_push
()
@
property
def
file_format_cls
(
self
):
'''
Returns file format object.
'''
"""Returns file format object """
if
(
self
.
_file_format
is
None
or
self
.
_file_format
.
name
!=
self
.
file_format
):
self
.
_file_format
=
FILE_FORMATS
[
self
.
file_format
]
return
self
.
_file_format
def
has_template
(
self
):
'''
Returns true if subproject is using template for translation
'''
"""Returns true if subproject is using template for translation"""
monolingual
=
self
.
file_format_cls
.
monolingual
return
(
(
monolingual
or
monolingual
is
None
)
and
...
...
@@ -1463,18 +1368,14 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
)
def
load_template_store
(
self
):
'''
Loads translate-toolkit store for template.
'''
"""Loads translate-toolkit store for template."""
return
self
.
file_format_cls
.
load
(
self
.
get_template_filename
(),
)
@
property
def
template_store
(
self
):
'''
Gets translate-toolkit store for template.
'''
"""Gets translate-toolkit store for template."""
# Do we need template?
if
not
self
.
has_template
():
return
None
...
...
@@ -1489,9 +1390,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
@
property
def
last_change
(
self
):
'''
Returns date of last change done in Weblate.
'''
"""Returns date of last change done in Weblate."""
try
:
change
=
Change
.
objects
.
content
().
filter
(
translation__subproject
=
self
...
...
@@ -1502,9 +1401,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
@
property
def
all_flags
(
self
):
'''
Returns parsed list of flags.
'''
"""Returns parsed list of flags."""
if
self
.
_all_flags
is
None
:
self
.
_all_flags
=
(
self
.
check_flags
.
split
(
','
)
+
...
...
@@ -1527,9 +1424,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return
True
def
add_new_language
(
self
,
language
,
request
):
'''
Creates new language file.
'''
"""Creates new language file."""
if
not
self
.
can_add_new_language
():
raise
ValueError
(
'Not supported operation!'
)
...
...
weblate/trans/models/translation.py
View file @
0652bd96
...
...
@@ -55,9 +55,7 @@ class TranslationManager(models.Manager):
def
check_sync
(
self
,
subproject
,
lang
,
code
,
path
,
force
=
False
,
request
=
None
):
'''
Parses translation meta info and creates/updates translation object.
'''
"""Parses translation meta info and updates translation object"""
translation
,
dummy
=
self
.
get_or_create
(
language
=
lang
,
language_code
=
code
,
...
...
@@ -72,16 +70,14 @@ class TranslationManager(models.Manager):
return
translation
def
enabled
(
self
):
'''
Filters enabled translations.
'''
"""Filters enabled translations."""
return
self
.
filter
(
enabled
=
True
).
select_related
()
def
get_percents
(
self
,
project
=
None
,
subproject
=
None
,
language
=
None
):
'''
Returns tuple consting of status percents -
"""Returns tuple consting of status percents:
(translated, fuzzy, failing checks)
'''
"""
# Filter translations
translations
=
self
if
project
is
not
None
:
...
...
@@ -166,9 +162,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
app_label
=
'trans'
def
__init__
(
self
,
*
args
,
**
kwargs
):
'''
Constructor to initialize some cache properties.
'''
"""Constructor to initialize some cache properties."""
super
(
Translation
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
_store
=
None
self
.
_last_change_obj
=
None
...
...
@@ -184,16 +178,11 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
)
def
has_acl
(
self
,
user
):
'''
Checks whether current user is allowed to access this
subproject.
'''
"""Checks whether current user is allowed to access this object"""
return
self
.
subproject
.
project
.
has_acl
(
user
)
def
check_acl
(
self
,
request
):
'''
Raises an error if user is not allowed to access this project.
'''
"""Raises an error if user is not allowed to access this project."""
self
.
subproject
.
project
.
check_acl
(
request
)
def
is_template
(
self
):
...
...
@@ -204,10 +193,10 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
self
.
filename
==
self
.
subproject
.
template
def
clean
(
self
):
'''
"""
Validates that filename exists and can be opened using
translate-toolkit.
'''
"""
if
not
os
.
path
.
exists
(
self
.
get_filename
()):
raise
ValidationError
(
_
(
...
...
@@ -227,9 +216,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
)
def
_get_percents
(
self
):
'''
Returns percentages of translation status.
'''
"""Returns percentages of translation status."""
# No units?
if
self
.
total
==
0
:
return
(
100
,
0
,
0
)
...
...
@@ -264,9 +251,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
self
.
total
-
self
.
translated
def
get_lock_user_display
(
self
):
'''
Returns formatted lock user.
'''
"""Returns formatted lock user."""
return
get_user_display
(
self
.
lock_user
)
def
get_lock_display
(
self
):
...
...
@@ -277,20 +262,17 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
)
def
is_locked
(
self
,
user
=
None
):
'''
Check whether the translation is locked and
possibly emits messages if request object is
provided.
'''
"""Check whether the translation is locked
Possibly emits messages if request object is provided.
"""
return
(
self
.
is_user_locked
(
user
)
or
self
.
subproject
.
locked
)
def
is_user_locked
(
self
,
user
=
None
):
'''
Checks whether there is valid user lock on this translation.
'''
"""Checks whether there is valid user lock on this translation."""
# Any user?
if
self
.
lock_user
is
None
:
return
False
...
...
@@ -309,9 +291,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
True
def
create_lock
(
self
,
user
,
explicit
=
False
):
'''
Creates lock on translation.
'''
"""Creates lock on translation."""
is_new
=
self
.
lock_user
is
None
self
.
lock_user
=
user
...
...
@@ -324,9 +304,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
self
.
update_lock_time
(
explicit
,
is_new
)
def
update_lock_time
(
self
,
explicit
=
False
,
is_new
=
True
):
'''
Sets lock timestamp.
'''
"""Sets lock timestamp."""
if
explicit
:
seconds
=
appsettings
.
LOCK_TIME
else
:
...
...
@@ -340,9 +318,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
self
.
save
()
def
update_lock
(
self
,
user
,
create
=
True
):
'''
Updates lock timestamp.
'''
"""Updates lock timestamp."""
# Check if we can lock
if
self
.
is_user_locked
(
user
):
return
False
...
...
@@ -360,15 +336,11 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
False
def
_reverse_url_name
(
self
):
'''
Returns base name for URL reversing.
'''
"""Returns base name for URL reversing."""
return
'translation'
def
_reverse_url_kwargs
(
self
):
'''
Returns kwargs for URL reversing.
'''
"""Returns kwargs for URL reversing."""
return
{
'project'
:
self
.
subproject
.
project
.
slug
,
'subproject'
:
self
.
subproject
.
slug
,
...
...
@@ -376,9 +348,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
}
def
get_widgets_url
(
self
):
'''
Returns absolute URL for widgets.
'''
"""Returns absolute URL for widgets."""
return
get_site_url
(
'%s?lang=%s'
%
(
reverse
(
...
...
@@ -391,9 +361,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
)
def
get_share_url
(
self
):
'''
Returns absolute URL usable for sharing.
'''
"""Returns absolute URL usable for sharing."""
return
get_site_url
(
reverse
(
'engage-lang'
,
...
...
@@ -419,15 +387,11 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
)
def
get_filename
(
self
):
'''
Returns absolute filename.
'''
"""Returns absolute filename."""
return
os
.
path
.
join
(
self
.
subproject
.
get_path
(),
self
.
filename
)
def
load_store
(
self
):
'''
Loads translate-toolkit storage from disk.
'''
"""Loads translate-toolkit storage from disk."""
return
self
.
subproject
.
file_format_cls
.
parse
(
self
.
get_filename
(),
self
.
subproject
.
template_store
,
...
...
@@ -435,16 +399,12 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
)
def
supports_language_pack
(
self
):
'''
Checks whether we support language pack download.
'''
"""Checks whether we support language pack download."""
return
self
.
subproject
.
file_format_cls
.
supports_language_pack
()
@
property
def
store
(
self
):
'''
Returns translate-toolkit storage object for a translation.
'''
"""Returns translate-toolkit storage object for a translation."""
if
self
.
_store
is
None
:
try
:
self
.
_store
=
self
.
load_store
()
...
...
@@ -455,9 +415,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
self
.
_store
def
check_sync
(
self
,
force
=
False
,
request
=
None
,
change
=
None
):
'''
Checks whether database is in sync with git and possibly does update.
'''
"""Checks whether database is in sync with git and possibly updates"""
if
change
is
None
:
change
=
Change
.
ACTION_UPDATE
...
...
@@ -579,9 +537,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
self
.
subproject
.
can_push
()
def
get_git_blob_hash
(
self
):
'''
Returns current VCS blob hash for file.
'''
"""Returns current VCS blob hash for file."""
ret
=
self
.
subproject
.
repository
.
get_object_hash
(
self
.
get_filename
())
if
not
self
.
subproject
.
has_template
():
...
...
@@ -595,9 +551,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
])
def
update_stats
(
self
):
'''
Updates translation statistics.
'''
"""Updates translation statistics."""
# Grab stats
stats
=
self
.
unit_set
.
aggregate
(
Sum
(
'num_words'
),
...
...
@@ -661,17 +615,13 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
self
.
store_hash
()
def
store_hash
(
self
):
'''
Stores current hash in database.
'''
"""Stores current hash in database."""
blob_hash
=
self
.
get_git_blob_hash
()
self
.
revision
=
blob_hash
self
.
save
()
def
get_last_author
(
self
,
email
=
False
):
'''
Returns last autor of change done in Weblate.
'''
"""Returns last autor of change done in Weblate."""
if
self
.
last_change_obj
is
None
:
return
None
return
get_author_name
(
...
...
@@ -696,17 +646,13 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
@
property
def
last_change
(
self
):
'''
Returns date of last change done in Weblate.
'''
"""Returns date of last change done in Weblate."""
if
self
.
last_change_obj
is
None
:
return
None
return
self
.
last_change_obj
.
timestamp
def
commit_pending
(
self
,
request
,
author
=
None
,
skip_push
=
False
):
'''
Commits any pending changes.
'''
"""Commits any pending changes."""
# Get author of last changes
last
=
self
.
get_last_author
(
True
)
...
...
@@ -720,9 +666,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
)
def
get_commit_message
(
self
):
'''
Formats commit message based on project configuration.
'''
"""Formats commit message based on project configuration."""
msg
=
self
.
subproject
.
commit_message
%
{
'language'
:
self
.
language_code
,
'language_name'
:
self
.
language
.
name
,
...
...
@@ -744,9 +688,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
msg
def
__git_commit
(
self
,
author
,
timestamp
,
sync
=
False
):
'''
Commits translation to git.
'''
"""Commits translation to git."""
# Format commit message
msg
=
self
.
get_commit_message
()
...
...
@@ -781,9 +723,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
self
.
store_hash
()
def
repo_needs_commit
(
self
):
'''
Checks whether there are some not committed changes.
'''
"""Checks whether there are some not committed changes."""
return
self
.
subproject
.
repository
.
needs_commit
(
self
.
filename
)
def
repo_needs_merge
(
self
):
...
...
@@ -794,14 +734,13 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
def
git_commit
(
self
,
request
,
author
,
timestamp
,
force_commit
=
False
,
sync
=
False
,
skip_push
=
False
,
force_new
=
False
):
'''
Wrapper for commiting translation to git.
"""Wrapper for commiting translation to git.
force_commit forces commit with lazy commits enabled
sync updates git hash stored within the translation (otherwise
translation rescan will be needed)
'''
"""
# Is there something for commit?
if
not
force_new
and
not
self
.
repo_needs_commit
():
return
False
...
...
@@ -837,9 +776,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
True
def
update_unit
(
self
,
unit
,
request
,
user
=
None
):
'''
Updates backend file and unit.
'''
"""Updates backend file and unit."""
if
user
is
None
:
user
=
request
.
user
# Save with lock acquired
...
...
@@ -917,9 +854,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
True
,
pounit
def
get_source_checks
(
self
):
'''
Returns list of failing source checks on current subproject.
'''
"""Returns list of failing source checks on current subproject."""
result
=
TranslationChecklist
()
result
.
add
(
'all'
,
...
...
@@ -959,9 +894,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
result
def
get_translation_checks
(
self
):
'''
Returns list of failing checks on current translation.
'''
"""Returns list of failing checks on current translation."""
result
=
TranslationChecklist
()
# All strings
...
...
@@ -1048,9 +981,9 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
result
def
merge_translations
(
self
,
request
,
store2
,
overwrite
,
add_fuzzy
,
fuzzy
):
"""
Merges translation unit wise, needed for template based translations to
add new strings.
"""
Merges translation unit wise
Needed for template based translations to
add new strings.
"""
ret
=
False
...
...
@@ -1078,9 +1011,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
def
merge_store
(
self
,
request
,
author
,
store2
,
overwrite
,
merge_header
,
add_fuzzy
,
fuzzy
,
merge_comments
):
'''
Merges translate-toolkit store into current translation.
'''
"""Merges translate-toolkit store into current translation."""
# Merge with lock acquired
with
self
.
subproject
.
repository_lock
:
...
...
@@ -1127,9 +1058,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
ret
def
merge_suggestions
(
self
,
request
,
store
,
fuzzy
):
'''
Merges content of translate-toolkit store as a suggestions.
'''
"""Merges content of translate-toolkit store as a suggestions."""
ret
=
False
for
dummy
,
unit
in
store
.
iterate_merge
(
fuzzy
):
# Calculate unit checksum
...
...
@@ -1157,9 +1086,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
def
merge_upload
(
self
,
request
,
fileobj
,
overwrite
,
author
=
None
,
merge_header
=
True
,
method
=
''
,
fuzzy
=
''
,
merge_comments
=
False
):
'''
Top level handler for file uploads.
'''
"""Top level handler for file uploads."""
filecopy
=
fileobj
.
read
()
fileobj
.
close
()
...
...
@@ -1233,9 +1160,7 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
return
ret
,
store
.
count_units
()
def
invalidate_cache
(
self
,
cache_type
=
None
):
'''
Invalidates any cached stats.
'''
"""Invalidates any cached stats."""
# Get parts of key cache
slug
=
self
.
subproject
.
get_full_slug
()
code
=
self
.
language
.
code
...
...
@@ -1259,7 +1184,5 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
}
def
get_export_url
(
self
):
'''
Returns URL of exported git repository.
'''
"""Returns URL of exported git repository."""
return
self
.
subproject
.
get_export_url
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment