Skip to content

Commit

Permalink
Merge ee40dac into 4535dc7
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Paz committed Jul 12, 2018
2 parents 4535dc7 + ee40dac commit 369bde1
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ install:
env:
global:
- SECRET_KEY=SK
- SUPPORTED_LANGUAGES="en pt"
- SUPPORTED_LANGUAGES="en|pt"
- DOCKER_IMAGE_NAME=ilha/bothub
script:
- flake8
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test:
@make check_environment
@make migrate CHECK_ENVIRONMENT=false
@make collectstatic CHECK_ENVIRONMENT=false
@SUPPORTED_LANGUAGES="en pt" pipenv run python manage.py test && pipenv run coverage report -m
@SUPPORTED_LANGUAGES="en|pt" pipenv run python manage.py test && pipenv run coverage report -m

migrate:
@make check_environment
Expand Down Expand Up @@ -68,7 +68,7 @@ NC = \033[0m
create_environment_vars_file:
@echo "SECRET_KEY=SK" > "${ENVIRONMENT_VARS_FILE}"
@echo "DEBUG=true" >> "${ENVIRONMENT_VARS_FILE}"
@echo "SUPPORTED_LANGUAGES=en de es pt fr it nl" >> "${ENVIRONMENT_VARS_FILE}"
@echo "SUPPORTED_LANGUAGES=en|pt" >> "${ENVIRONMENT_VARS_FILE}"
@echo "${SUCCESS}${NC} Settings file created"

install_development_requirements:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ You can set environment variables in your OS, write on ```.env``` file or pass v
| CSRF_COOKIE_DOMAIN | ```string``` | ```None``` | The domain to be used when setting the CSRF cookie.
| CSRF_COOKIE_SECURE | ```boolean``` | ```False``` | Whether to use a secure cookie for the CSRF cookie.
| BOTHUB_WEBAPP_BASE_URL | ```string``` | ```http://localhost:8080/``` | The bothub-webapp production application URL. Used to refer and redirect user correctly.
| SUPPORTED_LANGUAGES | ```string```| ```en|pt``` | Set supported languages. Separe languages using ```|```. You can set location follow the format: ```[LANGUAGE_CODE]:[LANGUAGE_LOCATION]```.
| BOTHUB_NLP_BASE_URL | ```string``` | ```http://localhost:2657/``` | The bothub-blp production application URL. Used to proxy requests.
| CHECK_ACCESSIBLE_API_URL | ```string``` | ```http://localhost/api/repositories/``` | URL used by ```bothub.health.check.check_accessible_api``` to make a HTTP request. The response status code must be 200.
| SEND_EMAILS | ```boolean``` | ```True``` | Send emails flag.


### Docker Environment Variables

| Variable | Type | Default | Description |
Expand Down
3 changes: 3 additions & 0 deletions bothub/api/serializers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Meta:
read_only=True,
default=serializers.CurrentUserDefault(),
style={'show': False})
language = serializers.ChoiceField(
LANGUAGE_CHOICES,
label=_('Language'))
categories = ModelMultipleChoiceField(
child_relation=serializers.PrimaryKeyRelatedField(
queryset=RepositoryCategory.objects.all()),
Expand Down
4 changes: 4 additions & 0 deletions bothub/api/serializers/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from bothub.common.models import RepositoryTranslatedExampleEntity
from bothub.common.models import RepositoryTranslatedExample
from bothub.common.models import RepositoryExample
from bothub.common.languages import LANGUAGE_CHOICES

from ..validators import CanContributeInRepositoryTranslatedExampleValidator
from ..validators import CanContributeInRepositoryExampleValidator
Expand Down Expand Up @@ -104,6 +105,9 @@ def __init__(self, *args, **kwargs):
CanContributeInRepositoryExampleValidator(),
],
help_text=_('Example\'s ID'))
language = serializers.ChoiceField(
LANGUAGE_CHOICES,
label=_('Language'))
has_valid_entities = serializers.SerializerMethodField()
entities = NewRepositoryTranslatedExampleEntitySeralizer(
many=True,
Expand Down
1 change: 1 addition & 0 deletions bothub/api/tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def test_invalid_language(self):
'slug': 'test',
'language': 'jj',
'categories': [self.category.id],
'description': '',
})
self.assertEqual(
response.status_code,
Expand Down
40 changes: 24 additions & 16 deletions bothub/common/languages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.utils.translation import gettext as _
from django.conf import settings
from django.core.exceptions import ValidationError


LANGUAGE_EN = 'en'
LANGUAGE_DE = 'de'
Expand All @@ -8,22 +11,27 @@
LANGUAGE_IT = 'it'
LANGUAGE_NL = 'nl'

VERBOSE_LANGUAGES = {
LANGUAGE_EN: _('English'),
LANGUAGE_DE: _('German'),
LANGUAGE_ES: _('Spanish'),
LANGUAGE_PT: _('Portuguese'),
LANGUAGE_FR: _('French'),
LANGUAGE_IT: _('Italian'),
LANGUAGE_NL: _('Dutch'),
}

LANGUAGE_CHOICES = [
(LANGUAGE_EN, _('English')),
(LANGUAGE_DE, _('German')),
(LANGUAGE_ES, _('Spanish')),
(LANGUAGE_PT, _('Portuguese')),
(LANGUAGE_FR, _('French')),
(LANGUAGE_IT, _('Italian')),
(LANGUAGE_NL, _('Dutch')),
(l, VERBOSE_LANGUAGES.get(l, l)) for l in
settings.SUPPORTED_LANGUAGES
]

SUPPORTED_LANGUAGES = [
LANGUAGE_EN,
LANGUAGE_DE,
LANGUAGE_ES,
LANGUAGE_PT,
LANGUAGE_FR,
LANGUAGE_IT,
LANGUAGE_NL,
]

def is_valid_language(value):
return value in settings.SUPPORTED_LANGUAGES.keys()


def validate_language(value):
if not is_valid_language(value):
raise ValidationError(_(
'{} is not a supported language.').format(value))
29 changes: 29 additions & 0 deletions bothub/common/migrations/0014_auto_20180709_1909.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 2.0.6 on 2018-07-09 19:09

import bothub.common.languages
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0013_auto_20180706_1212'),
]

operations = [
migrations.AlterField(
model_name='repository',
name='language',
field=models.CharField(help_text="Repository's examples language. The examples can be translated to other languages.", max_length=2, validators=[bothub.common.languages.validate_language], verbose_name='language'),
),
migrations.AlterField(
model_name='repositorytranslatedexample',
name='language',
field=models.CharField(help_text='Translation language', max_length=2, validators=[bothub.common.languages.validate_language], verbose_name='language'),
),
migrations.AlterField(
model_name='repositoryupdate',
name='language',
field=models.CharField(max_length=2, validators=[bothub.common.languages.validate_language], verbose_name='language'),
),
]
20 changes: 13 additions & 7 deletions bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ class Meta:
help_text=_('Easy way to found and share repositories'))
language = models.CharField(
_('language'),
choices=languages.LANGUAGE_CHOICES,
max_length=2,
help_text=_('Repository\'s examples language. The examples can be ' +
'translated to other languages.'))
'translated to other languages.'),
validators=[
languages.validate_language,
])
categories = models.ManyToManyField(
RepositoryCategory,
help_text=CATEGORIES_HELP_TEXT)
Expand Down Expand Up @@ -159,7 +161,7 @@ def languages_status(self):
lambda language: (
language,
self.language_status(language)),
languages.SUPPORTED_LANGUAGES,
settings.SUPPORTED_LANGUAGES.keys(),
))

@property
Expand Down Expand Up @@ -290,8 +292,10 @@ class Meta:
related_name='updates')
language = models.CharField(
_('language'),
choices=languages.LANGUAGE_CHOICES,
max_length=2)
max_length=2,
validators=[
languages.validate_language,
])
created_at = models.DateTimeField(
_('created at'),
auto_now_add=True)
Expand Down Expand Up @@ -484,9 +488,11 @@ class Meta:
help_text=_('Example object'))
language = models.CharField(
_('language'),
choices=languages.LANGUAGE_CHOICES,
max_length=2,
help_text=_('Translation language'))
help_text=_('Translation language'),
validators=[
languages.validate_language,
])
text = models.TextField(
_('text'),
help_text=_('Translation text'))
Expand Down
3 changes: 2 additions & 1 deletion bothub/common/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase
from django.utils import timezone
from django.core.exceptions import ValidationError
from django.conf import settings

from bothub.authentication.models import User

Expand Down Expand Up @@ -267,7 +268,7 @@ def test_languages_status(self):
languages_status = self.repository.languages_status
self.assertListEqual(
list(languages_status.keys()),
languages.SUPPORTED_LANGUAGES)
list(settings.SUPPORTED_LANGUAGES.keys()))
# TODO: Update test_languages_status test
# Create expeted result

Expand Down
10 changes: 10 additions & 0 deletions bothub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from decouple import config
from django.utils.log import DEFAULT_LOGGING

from .utils import cast_supported_languages


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -225,3 +227,11 @@
'handlers': ['bothub.health'],
'level': 'DEBUG',
}


# Supported Languages

SUPPORTED_LANGUAGES = config(
'SUPPORTED_LANGUAGES',
default='en|pt',
cast=cast_supported_languages)
8 changes: 8 additions & 0 deletions bothub/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from collections import OrderedDict


def cast_supported_languages(i):
return OrderedDict([
x.split(':', 1) if ':' in x else (x, x) for x in
i.split('|')
])

0 comments on commit 369bde1

Please sign in to comment.