Merge branch 'develop' of ssh://gl.id-east.ru:222/gm/gm-backend into develop

This commit is contained in:
evgeniy-st 2019-08-28 18:03:09 +03:00
commit d9793b2056
14 changed files with 97 additions and 9 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_plural = _('Establishment subtypes')
def __str__(self):
"""__str__ method."""
return self.name
# def __str__(self):
# """__str__ method."""
# return self.name
def clean_fields(self, exclude=None):
if not self.establishment_type.use_subtypes:

View File

@ -5,11 +5,23 @@ from . import models
class ImageSerializer(serializers.ModelSerializer):
"""Serializer for model Image."""
# REQUEST
file = serializers.ImageField(source='image',
write_only=True)
# RESPONSE
url = serializers.SerializerMethodField()
class Meta:
"""Meta class"""
model = models.Image
fields = (
'id',
'image',
'file',
'url'
)
def get_url(self, obj):
"""Get absolute URL path"""
return obj.get_full_image_url(request=self.context.get('request'))

View File

@ -1,10 +1,12 @@
from rest_framework import generics
from utils.permissions import IsAuthenticatedAndTokenIsValid
from . import models, serializers
class ImageUploadView(generics.CreateAPIView):
"""Upload image to gallery"""
permission_classes = (IsAuthenticatedAndTokenIsValid, )
model = models.Image
queryset = models.Image.objects.all()
serializer_class = serializers.ImageSerializer

View File

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

View File

@ -30,11 +30,27 @@ class TJSONField(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(self):
field = getattr(self, field_name)
if isinstance(field, dict):
return field.get(get_language())
return field.get(to_locale(get_language()))
return None
return translate

View File

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