add configuration
This commit is contained in:
parent
e5989273dc
commit
cb1ff8e410
0
apps/configuration/__init__.py
Normal file
0
apps/configuration/__init__.py
Normal file
8
apps/configuration/admin.py
Normal file
8
apps/configuration/admin.py
Normal 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"""
|
||||||
7
apps/configuration/apps.py
Normal file
7
apps/configuration/apps.py
Normal 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')
|
||||||
24
apps/configuration/migrations/0001_initial.py
Normal file
24
apps/configuration/migrations/0001_initial.py
Normal 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,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
0
apps/configuration/migrations/__init__.py
Normal file
0
apps/configuration/migrations/__init__.py
Normal file
9
apps/configuration/models.py
Normal file
9
apps/configuration/models.py
Normal 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')
|
||||||
3
apps/configuration/tests.py
Normal file
3
apps/configuration/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
3
apps/configuration/views.py
Normal file
3
apps/configuration/views.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
||||||
|
|
@ -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:
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user