add configuration

This commit is contained in:
Dmitriy Kuzmenko 2019-08-28 17:53:52 +03:00
parent e5989273dc
commit cb1ff8e410
12 changed files with 82 additions and 8 deletions

View File

View File

@ -0,0 +1,8 @@
from django.contrib import admin
from solo.admin import SingletonModelAdmin
from configuration.models import TranslationSettings
@admin.register(TranslationSettings)
class TranslationAdmin(SingletonModelAdmin):
"""Translation admin"""

View File

@ -0,0 +1,7 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _
class ConfigurationConfig(AppConfig):
name = 'configuration'
verbose_name = _('configuration')

View File

@ -0,0 +1,24 @@
# Generated by Django 2.2.4 on 2019-08-28 13:33
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='TranslationSettings',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('default_language', models.CharField(default='en-GB', max_length=10, verbose_name='default language')),
],
options={
'abstract': False,
},
),
]

View File

@ -0,0 +1,9 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from solo.models import SingletonModel
class TranslationSettings(SingletonModel):
"""Translation settings solo model."""
default_language = models.CharField(
_('default language'), max_length=10, default='en-GB')

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@ -49,9 +49,9 @@ class EstablishmentSubType(ProjectBaseMixin, TraslatedFieldsMixin):
verbose_name = _('Establishment subtype') verbose_name = _('Establishment subtype')
verbose_name_plural = _('Establishment subtypes') verbose_name_plural = _('Establishment subtypes')
def __str__(self): # def __str__(self):
"""__str__ method.""" # """__str__ method."""
return self.name # return self.name
def clean_fields(self, exclude=None): def clean_fields(self, exclude=None):
if not self.establishment_type.use_subtypes: if not self.establishment_type.use_subtypes:

View File

@ -1,6 +1,6 @@
"""Custom middleware.""" """Custom middleware."""
from django.utils import translation from django.utils import translation
from configuration.models import TranslationSettings
def get_locale(cookie_dict): def get_locale(cookie_dict):
return cookie_dict.get('locale') return cookie_dict.get('locale')
@ -16,9 +16,8 @@ def parse_cookies(get_response):
cookie_dict = request.COOKIES cookie_dict = request.COOKIES
# processing locale cookie # processing locale cookie
locale = get_locale(cookie_dict) locale = get_locale(cookie_dict) or TranslationSettings.get_solo().default_language
if locale: translation.activate(locale)
translation.activate(locale)
request.locale = locale request.locale = locale
# processing country country cookie # processing country country cookie

View File

@ -30,11 +30,27 @@ class TJSONField(JSONField):
"""Overrided JsonField.""" """Overrided JsonField."""
def to_locale(language):
"""Turn a language name (en-us) into a locale name (en_US)."""
language, _, country = language.lower().partition('-')
if not country:
return language
# A language with > 2 characters after the dash only has its first
# character after the dash capitalized; e.g. sr-latn becomes sr_Latn.
# A language with 2 characters after the dash has both characters
# capitalized; e.g. en-us becomes en_US.
country, _, tail = country.partition('-')
country = country.title() if len(country) > 2 else country.upper()
if tail:
country += '-' + tail
return language + '-' + country
def translate_field(self, field_name): def translate_field(self, field_name):
def translate(self): def translate(self):
field = getattr(self, field_name) field = getattr(self, field_name)
if isinstance(field, dict): if isinstance(field, dict):
return field.get(get_language()) return field.get(to_locale(get_language()))
return None return None
return translate return translate

View File

@ -63,6 +63,7 @@ PROJECT_APPS = [
'news.apps.NewsConfig', 'news.apps.NewsConfig',
'partner.apps.PartnerConfig', 'partner.apps.PartnerConfig',
'translation.apps.TranslationConfig', 'translation.apps.TranslationConfig',
'configuration.apps.ConfigurationConfig',
] ]
EXTERNAL_APPS = [ EXTERNAL_APPS = [
@ -78,6 +79,7 @@ EXTERNAL_APPS = [
'rest_framework_social_oauth2', 'rest_framework_social_oauth2',
'django_extensions', 'django_extensions',
'rest_framework_simplejwt.token_blacklist', 'rest_framework_simplejwt.token_blacklist',
'solo',
] ]
@ -381,3 +383,6 @@ DATA_UPLOAD_MAX_MEMORY_SIZE = 5242880 # 5MB
# Maximum uploaded file size # Maximum uploaded file size
FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880 # 5MB FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880 # 5MB
FILE_UPLOAD_PERMISSIONS = 0o644 FILE_UPLOAD_PERMISSIONS = 0o644
SOLO_CACHE_TIMEOUT = 300