Commit 32d10838 authored by Michal Čihař's avatar Michal Čihař

Merge remote-tracking branch 'origin/master'

parents 6f7abfc9 2382f037
from django.core.management.base import BaseCommand
from optparse import make_option
from django.contrib.auth.models import Group, Permission, User
from accounts.models import create_groups
class Command(BaseCommand):
help = 'setups default groups'
......@@ -10,40 +10,12 @@ class Command(BaseCommand):
dest='move',
default=False,
help='Move all users to Users group'),
make_option('--no-update',
action='store_false',
dest='update',
default=True,
help='Prevents updates to existing group definitions'),
)
def handle(self, *args, **options):
group, created = Group.objects.get_or_create(name = 'Users')
group.permissions.add(
Permission.objects.get(codename = 'upload_translation'),
Permission.objects.get(codename = 'overwrite_translation'),
Permission.objects.get(codename = 'save_translation'),
Permission.objects.get(codename = 'accept_suggestion'),
Permission.objects.get(codename = 'delete_suggestion'),
Permission.objects.get(codename = 'ignore_check'),
Permission.objects.get(codename = 'upload_dictionary'),
Permission.objects.get(codename = 'add_dictionary'),
Permission.objects.get(codename = 'change_dictionary'),
Permission.objects.get(codename = 'delete_dictionary'),
)
group, created = Group.objects.get_or_create(name = 'Managers')
group.permissions.add(
Permission.objects.get(codename = 'author_translation'),
Permission.objects.get(codename = 'upload_translation'),
Permission.objects.get(codename = 'overwrite_translation'),
Permission.objects.get(codename = 'commit_translation'),
Permission.objects.get(codename = 'update_translation'),
Permission.objects.get(codename = 'push_translation'),
Permission.objects.get(codename = 'automatic_translation'),
Permission.objects.get(codename = 'save_translation'),
Permission.objects.get(codename = 'accept_suggestion'),
Permission.objects.get(codename = 'delete_suggestion'),
Permission.objects.get(codename = 'ignore_check'),
Permission.objects.get(codename = 'upload_dictionary'),
Permission.objects.get(codename = 'add_dictionary'),
Permission.objects.get(codename = 'change_dictionary'),
Permission.objects.get(codename = 'delete_dictionary'),
)
if options['move']:
for u in User.objects.all():
u.groups.add(group)
create_groups(options['update'], options['move'])
......@@ -6,7 +6,8 @@ from django.contrib.auth.signals import user_logged_in
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _, gettext
from django.contrib import messages
from django.contrib.auth.models import Group
from django.contrib.auth.models import Group, Permission, User
from django.db.models.signals import post_syncdb
from lang.models import Language
......@@ -67,3 +68,48 @@ def create_profile_callback(sender, **kwargs):
pass
post_save.connect(create_profile_callback, sender = User)
def create_groups(update, move):
group, created = Group.objects.get_or_create(name = 'Users')
if created or update:
group.permissions.add(
Permission.objects.get(codename = 'upload_translation'),
Permission.objects.get(codename = 'overwrite_translation'),
Permission.objects.get(codename = 'save_translation'),
Permission.objects.get(codename = 'accept_suggestion'),
Permission.objects.get(codename = 'delete_suggestion'),
Permission.objects.get(codename = 'ignore_check'),
Permission.objects.get(codename = 'upload_dictionary'),
Permission.objects.get(codename = 'add_dictionary'),
Permission.objects.get(codename = 'change_dictionary'),
Permission.objects.get(codename = 'delete_dictionary'),
)
group, created = Group.objects.get_or_create(name = 'Managers')
if created or update:
group.permissions.add(
Permission.objects.get(codename = 'author_translation'),
Permission.objects.get(codename = 'upload_translation'),
Permission.objects.get(codename = 'overwrite_translation'),
Permission.objects.get(codename = 'commit_translation'),
Permission.objects.get(codename = 'update_translation'),
Permission.objects.get(codename = 'push_translation'),
Permission.objects.get(codename = 'automatic_translation'),
Permission.objects.get(codename = 'save_translation'),
Permission.objects.get(codename = 'accept_suggestion'),
Permission.objects.get(codename = 'delete_suggestion'),
Permission.objects.get(codename = 'ignore_check'),
Permission.objects.get(codename = 'upload_dictionary'),
Permission.objects.get(codename = 'add_dictionary'),
Permission.objects.get(codename = 'change_dictionary'),
Permission.objects.get(codename = 'delete_dictionary'),
)
if move:
for u in User.objects.all():
u.groups.add(group)
def sync_create_groups(sender, **kwargs):
if sender.__name__ == 'accounts.models':
create_groups(False, False)
post_syncdb.connect(sync_create_groups)
......@@ -24,9 +24,7 @@ Installation
Install all required components (see above), adjust :file:`settings.py` and
then run :program:`./manage.py syncdb` to create database structure. Now you
should be able to create translation projects using admin interface. You
probably also want to run :program:`./manage.py setuplang` to get default list
of languages and :program:`./manage.py setupgroups` to initialize default groups.
should be able to create translation projects using admin interface.
You should also login to admin interface (on ``/admin/`` URL) and adjust
default site name to match your domain.
......
......@@ -40,6 +40,9 @@ The ./manage.py is extended with following commands:
Configures default groups and (if called with ``--move``) assigns all users
to default group.
The option ``--no-update`` disables update of existing groups (only adds
new ones).
.. seealso:: :ref:`privileges`
.. option:: setuplang
......@@ -47,6 +50,9 @@ The ./manage.py is extended with following commands:
Setups list of languages (it has own list and all defined in
translate-toolkit).
The option ``--no-update`` disables update of existing languages (only add
new ones).
.. option:: updatechecks
Updates all check for all units. This could be useful only on upgrades
......
......@@ -23,8 +23,6 @@ Installing from sources
.. code-block:: sh
./manage.py syncdb
./manage.py setuplang
./manage.py setupgroups
./manage.py compilemessages # If you are using Git checkout
#. Configure webserver to serve Weblate, see :ref:`server`.
......
# -*- coding: UTF-8 -*-
from django.core.management.base import BaseCommand
from optparse import make_option
from lang.models import Language
from translate.lang import data
EXTRALANGS = [
('ur', 'Urdu', 2, '(n != 1)'),
('uz@latin', 'Uzbek (latin)', 1, '0'),
('uz', 'Uzbek', 1, '0'),
('sr@latin', 'Serbian (latin)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('sr_RS@latin', 'Serbian (latin)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('sr@cyrillic', 'Serbian (cyrillic)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('sr_RS@cyrillic', 'Serbian (cyrillic)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('be@latin', 'Belarusian (latin)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('en_US', 'English (United States)', 2, '(n != 1)'),
('nb_NO', 'Norwegian Bokmål', 2, '(n != 1)'),
('pt_PT', 'Portuguese (Portugal)', 2, '(n > 1)'),
]
class Command(BaseCommand):
help = 'Populates language definitions'
def handle(self, *args, **options):
for code, props in data.languages.items():
lang, created = Language.objects.get_or_create(
code = code)
lang.name = props[0].split(';')[0]
# Use shorter name
if code == 'ia':
lang.name = 'Interlingua'
# Shorten name
elif code == 'el':
lang.name = 'Greek'
elif code == 'st':
lang.name = 'Sotho'
elif code == 'oc':
lang.name = 'Occitan'
elif code == 'nb':
lang.name = 'Norwegian Bokmål'
if code == 'gd' and props[2] == 'nplurals=4; plural=(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3':
# Workaround bug in data
lang.nplurals = 4
lang.pluralequation = '(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3'
elif code == 'kk':
# Kazakh should have plurals, ttkit says it does not have
lang.nplurals = 2
lang.pluralequation = '(n != 1)'
else:
lang.nplurals = props[1]
lang.pluralequation = props[2]
lang.save()
for props in EXTRALANGS:
lang, created = Language.objects.get_or_create(
code = props[0])
lang.name = props[1]
lang.nplurals = props[2]
lang.pluralequation = props[3]
lang.save()
option_list = BaseCommand.option_list + (
make_option('--no-update',
action='store_false',
dest='update',
default=True,
help='Prevents updates to existing language definitions'),
)
def handle(self, *args, **options):
Language.objects.setup(options['update'])
# -*- coding: UTF-8 -*-
from django.db import models
from django.utils.translation import ugettext as _
from django.db.models import Sum
from translate.lang import data
from django.db.models.signals import post_syncdb
EXTRALANGS = [
('ur', 'Urdu', 2, '(n != 1)'),
('uz@latin', 'Uzbek (latin)', 1, '0'),
('uz', 'Uzbek', 1, '0'),
('sr@latin', 'Serbian (latin)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('sr_RS@latin', 'Serbian (latin)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('sr@cyrillic', 'Serbian (cyrillic)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('sr_RS@cyrillic', 'Serbian (cyrillic)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('be@latin', 'Belarusian (latin)', 3, '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'),
('en_US', 'English (United States)', 2, '(n != 1)'),
('nb_NO', 'Norwegian Bokmål', 2, '(n != 1)'),
('pt_PT', 'Portuguese (Portugal)', 2, '(n > 1)'),
]
class LanguageManager(models.Manager):
def auto_create(self, code):
'''
Autmatically creates new language based on code and best guess
of parameters.
'''
# Create standard language
lang = Language.objects.create(
code = code,
name = '%s (generated)' % code,
nplurals = 2,
pluralequation = '(n != 1)',
)
# In case this is just a different variant of known language, get params from that
if '_' in code:
try:
baselang = Language.objects.get(code = code.split('_')[0])
lang.name = '%s (generated - %s)' % (
baselang.name,
code,
)
lang.nplurals = baselang.nplurals
lang.pluralequation = baselang.pluralequation
lang.save()
except Language.DoesNotExist:
pass
return lang
def setup(self, update):
'''
Creates basic set of languages based on languages defined in ttkit
and on our list of extra languages.
'''
# Languages from ttkit
for code, props in data.languages.items():
lang, created = Language.objects.get_or_create(
code = code)
# Should we update existing?
if not update and not created:
continue
# Fixups (mostly shortening) of langauge names
if code == 'ia':
lang.name = 'Interlingua'
elif code == 'el':
lang.name = 'Greek'
elif code == 'st':
lang.name = 'Sotho'
elif code == 'oc':
lang.name = 'Occitan'
elif code == 'nb':
lang.name = 'Norwegian Bokmål'
else:
# Standard ttkit language name
lang.name = props[0].split(';')[0]
# Fixes for broken plurals
if code == 'gd' and props[2] == 'nplurals=4; plural=(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3':
# Workaround bug in data
lang.nplurals = 4
lang.pluralequation = '(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3'
elif code == 'kk':
# Kazakh should have plurals, ttkit says it does not have
lang.nplurals = 2
lang.pluralequation = '(n != 1)'
else:
# Standard plurals
lang.nplurals = props[1]
lang.pluralequation = props[2]
# Save language
lang.save()
# Create Weblate extra languages
for props in EXTRALANGS:
lang, created = Language.objects.get_or_create(
code = props[0])
# Should we update existing?
if not update and not created:
continue
lang.name = props[1]
lang.nplurals = props[2]
lang.pluralequation = props[3]
lang.save()
def setup_lang(sender=None, **kwargs):
'''
Hook for creating basic set of languages on syncdb.
'''
if sender.__name__ == 'lang.models':
Language.objects.setup(False)
post_syncdb.connect(setup_lang)
class Language(models.Model):
code = models.SlugField(unique = True)
......@@ -8,6 +124,8 @@ class Language(models.Model):
nplurals = models.SmallIntegerField(default = 0)
pluralequation = models.CharField(max_length = 255, blank = True)
objects = LanguageManager()
class Meta:
ordering = ['name']
......
......@@ -76,25 +76,7 @@ class TranslationManager(models.Manager):
try:
lang = Language.objects.get(code = code)
except Language.DoesNotExist:
lang = Language.objects.create(
code = code,
name = '%s (generated)' % code,
nplurals = 2,
pluralequation = '(n != 1)',
)
# In case this is just a different variant of known language, get params from that
if '_' in code:
try:
baselang = Language.objects.get(code = code.split('_')[0])
lang.name = '%s (generated - %s)' % (
baselang.name,
code,
)
lang.nplurals = baselang.nplurals
lang.pluralequation = baselang.pluralequation
lang.save()
except Language.DoesNotExist:
pass
lang = Language.objects.auto_create(code)
translation, created = self.get_or_create(
language = lang,
subproject = subproject,
......
......@@ -344,6 +344,12 @@ class SubProject(models.Model):
del gitrepo
return ret
def get_mask_matches(self):
'''
Returns files matching current mask.
'''
return glob(os.path.join(self.get_path(), self.filemask))
def get_translation_blobs(self):
'''
Iterator over translations in filesystem.
......@@ -353,7 +359,7 @@ class SubProject(models.Model):
# Glob files
prefix = os.path.join(self.get_path(), '')
for f in glob(os.path.join(self.get_path(), self.filemask)):
for f in self.get_mask_matches():
filename = f.replace(prefix, '')
yield (
self.get_lang_code(filename),
......@@ -380,12 +386,23 @@ class SubProject(models.Model):
# Remove possible encoding part
return code.split('.')[0]
def save(self, *args, **kwargs):
def sync_git_repo(self):
'''
Brings git repo in sync with current model.
'''
self.configure_repo()
self.configure_branch()
self.commit_pending()
self.update_branch()
def clean(self):
self.sync_git_repo()
if len(self.get_mask_matches()) == 0:
raise ValidationError(_('The mask did not match any files!'))
def save(self, *args, **kwargs):
self.sync_git_repo()
super(SubProject, self).save(*args, **kwargs)
self.create_translations()
......
......@@ -59,6 +59,7 @@ sed -i 's@weblate-path@%{WLDIR}@g' examples/django.wsgi
%install
install -d %{buildroot}/%{WLDIR}
install -d %{buildroot}/%{WLETCDIR}
ln -s ../ %{buildroot}/%{WLDIR}
# Copy all files
cp -a . %{buildroot}/%{WLDIR}
......
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