commit
ee48b1d2ad
22 changed files with 629 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||||
|
# SOME DESCRIPTIVE TITLE. |
||||||
|
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER |
||||||
|
# This file is distributed under the same license as the PACKAGE package. |
||||||
|
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
||||||
|
# |
||||||
|
#, fuzzy |
||||||
|
msgid "" |
||||||
|
msgstr "" |
||||||
|
"Project-Id-Version: PACKAGE VERSION\n" |
||||||
|
"Report-Msgid-Bugs-To: \n" |
||||||
|
"POT-Creation-Date: 2017-02-20 10:07+0000\n" |
||||||
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" |
||||||
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
||||||
|
"Language-Team: LANGUAGE <LL@li.org>\n" |
||||||
|
"Language: \n" |
||||||
|
"MIME-Version: 1.0\n" |
||||||
|
"Content-Type: text/plain; charset=UTF-8\n" |
||||||
|
"Content-Transfer-Encoding: 8bit\n" |
||||||
|
"Plural-Forms: nplurals=2; plural=(n != 1);\n" |
||||||
|
|
||||||
|
#: test_app/models.py:15 test_app/models.py:28 |
||||||
|
msgid "Title" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:18 |
||||||
|
msgid "Entry Category" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:19 |
||||||
|
msgid "Entry Categories" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:32 |
||||||
|
msgid "Description" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:36 |
||||||
|
msgid "Categories" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:40 |
||||||
|
msgid "Entry" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:41 |
||||||
|
msgid "Entries" |
||||||
|
msgstr "" |
@ -0,0 +1,46 @@ |
|||||||
|
# SOME DESCRIPTIVE TITLE. |
||||||
|
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER |
||||||
|
# This file is distributed under the same license as the PACKAGE package. |
||||||
|
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
||||||
|
# |
||||||
|
#, fuzzy |
||||||
|
msgid "" |
||||||
|
msgstr "" |
||||||
|
"Project-Id-Version: PACKAGE VERSION\n" |
||||||
|
"Report-Msgid-Bugs-To: \n" |
||||||
|
"POT-Creation-Date: 2017-02-20 10:07+0000\n" |
||||||
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" |
||||||
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
||||||
|
"Language-Team: LANGUAGE <LL@li.org>\n" |
||||||
|
"Language: \n" |
||||||
|
"MIME-Version: 1.0\n" |
||||||
|
"Content-Type: text/plain; charset=UTF-8\n" |
||||||
|
"Content-Transfer-Encoding: 8bit\n" |
||||||
|
|
||||||
|
#: test_app/models.py:15 test_app/models.py:28 |
||||||
|
msgid "Title" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:18 |
||||||
|
msgid "Entry Category" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:19 |
||||||
|
msgid "Entry Categories" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:32 |
||||||
|
msgid "Description" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:36 |
||||||
|
msgid "Categories" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:40 |
||||||
|
msgid "Entry" |
||||||
|
msgstr "" |
||||||
|
|
||||||
|
#: test_app/models.py:41 |
||||||
|
msgid "Entries" |
||||||
|
msgstr "" |
@ -0,0 +1,157 @@ |
|||||||
|
""" |
||||||
|
Django settings for main project. |
||||||
|
|
||||||
|
Generated by 'django-admin startproject' using Django 1.10.5. |
||||||
|
|
||||||
|
For more information on this file, see |
||||||
|
https://docs.djangoproject.com/en/1.10/topics/settings/ |
||||||
|
|
||||||
|
For the full list of settings and their values, see |
||||||
|
https://docs.djangoproject.com/en/1.10/ref/settings/ |
||||||
|
""" |
||||||
|
|
||||||
|
import os |
||||||
|
|
||||||
|
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) |
||||||
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
||||||
|
|
||||||
|
|
||||||
|
# Quick-start development settings - unsuitable for production |
||||||
|
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ |
||||||
|
|
||||||
|
# SECURITY WARNING: keep the secret key used in production secret! |
||||||
|
SECRET_KEY = 'u0epoh#uh98^p&d%^tg918&&ilkmet9d%o#vz0*^be@2c@ammn' |
||||||
|
|
||||||
|
# SECURITY WARNING: don't run with debug turned on in production! |
||||||
|
DEBUG = True |
||||||
|
|
||||||
|
ALLOWED_HOSTS = [] |
||||||
|
|
||||||
|
|
||||||
|
# Application definition |
||||||
|
|
||||||
|
INSTALLED_APPS = [ |
||||||
|
'django.contrib.admin', |
||||||
|
'django.contrib.auth', |
||||||
|
'django.contrib.contenttypes', |
||||||
|
'django.contrib.sessions', |
||||||
|
'django.contrib.messages', |
||||||
|
'django.contrib.staticfiles', |
||||||
|
|
||||||
|
'haystack', |
||||||
|
'search', |
||||||
|
'utils', |
||||||
|
|
||||||
|
'main.test_app', |
||||||
|
] |
||||||
|
|
||||||
|
|
||||||
|
MIDDLEWARE = [ |
||||||
|
'django.middleware.security.SecurityMiddleware', |
||||||
|
'django.contrib.sessions.middleware.SessionMiddleware', |
||||||
|
'django.middleware.common.CommonMiddleware', |
||||||
|
'django.middleware.csrf.CsrfViewMiddleware', |
||||||
|
'django.contrib.auth.middleware.AuthenticationMiddleware', |
||||||
|
'django.contrib.messages.middleware.MessageMiddleware', |
||||||
|
'django.middleware.clickjacking.XFrameOptionsMiddleware', |
||||||
|
] |
||||||
|
|
||||||
|
ROOT_URLCONF = 'main.urls' |
||||||
|
|
||||||
|
TEMPLATES = [ |
||||||
|
{ |
||||||
|
'BACKEND': 'django.template.backends.django.DjangoTemplates', |
||||||
|
'DIRS': [], |
||||||
|
'APP_DIRS': True, |
||||||
|
'OPTIONS': { |
||||||
|
'context_processors': [ |
||||||
|
'django.template.context_processors.debug', |
||||||
|
'django.template.context_processors.request', |
||||||
|
'django.contrib.auth.context_processors.auth', |
||||||
|
'django.contrib.messages.context_processors.messages', |
||||||
|
], |
||||||
|
}, |
||||||
|
}, |
||||||
|
] |
||||||
|
|
||||||
|
WSGI_APPLICATION = 'main.wsgi.application' |
||||||
|
|
||||||
|
|
||||||
|
# Database |
||||||
|
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases |
||||||
|
|
||||||
|
DATABASES = { |
||||||
|
'default': { |
||||||
|
'ENGINE': 'django.db.backends.sqlite3', |
||||||
|
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
# Password validation |
||||||
|
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators |
||||||
|
|
||||||
|
AUTH_PASSWORD_VALIDATORS = [ |
||||||
|
{ |
||||||
|
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', |
||||||
|
}, |
||||||
|
{ |
||||||
|
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', |
||||||
|
}, |
||||||
|
{ |
||||||
|
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', |
||||||
|
}, |
||||||
|
{ |
||||||
|
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', |
||||||
|
}, |
||||||
|
] |
||||||
|
|
||||||
|
|
||||||
|
# Internationalization |
||||||
|
# https://docs.djangoproject.com/en/1.10/topics/i18n/ |
||||||
|
|
||||||
|
LANGUAGE_CODE = 'de' |
||||||
|
|
||||||
|
LANGUAGES = ( |
||||||
|
("de", "Deutsch"), |
||||||
|
("en", "English"), |
||||||
|
) |
||||||
|
|
||||||
|
|
||||||
|
TIME_ZONE = 'UTC' |
||||||
|
|
||||||
|
USE_I18N = True |
||||||
|
|
||||||
|
USE_L10N = True |
||||||
|
|
||||||
|
USE_TZ = True |
||||||
|
|
||||||
|
|
||||||
|
# Static files (CSS, JavaScript, Images) |
||||||
|
# https://docs.djangoproject.com/en/1.10/howto/static-files/ |
||||||
|
|
||||||
|
STATIC_URL = '/static/' |
||||||
|
|
||||||
|
|
||||||
|
HAYSTACK_CONNECTIONS = { |
||||||
|
'default': { |
||||||
|
'ENGINE': 'search.multilingual_xapian_backend.MultilingualXapianEngine', |
||||||
|
'PATH': os.path.join(BASE_DIR, 'var', 'searchindex', 'de.xapian_index'), |
||||||
|
'LANGUAGE': 'german2', # Normalises umlauts and ß |
||||||
|
}, |
||||||
|
'default_de': { |
||||||
|
'ENGINE': 'search.multilingual_xapian_backend.MultilingualXapianEngine', |
||||||
|
'PATH': os.path.join(BASE_DIR, 'var', 'searchindex', 'de.xapian_index'), |
||||||
|
'LANGUAGE': 'german2', # Normalises umlauts and ß |
||||||
|
}, |
||||||
|
'default_en': { |
||||||
|
'ENGINE': 'search.multilingual_xapian_backend.MultilingualXapianEngine', |
||||||
|
'PATH': os.path.join(BASE_DIR, 'var', 'searchindex', 'en.xapian_index'), |
||||||
|
'LANGUAGE': 'english', |
||||||
|
# 'FLAGS': 'english', |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
# : the options used to parse AutoQueries; the default is FLAG_PHRASE | FLAG_BOOLEAN | FLAG_LOVEHATE | FLAG_WILDCARD | FLAG_PURE_NOT See here for more information on what they mean. |
||||||
|
# HAYSTACK_XAPIAN_STEMMING_STRATEGY: This option lets you chose the stemming strategy used by Xapian. Possible values are STEM_NONE, STEM_SOME, STEM_ALL, STEM_ALL_Z, where STEM_SOME is the default. See here for more information about the different strategies. |
@ -0,0 +1,3 @@ |
|||||||
|
from django.contrib import admin |
||||||
|
|
||||||
|
# Register your models here. |
@ -0,0 +1,5 @@ |
|||||||
|
from django.apps import AppConfig |
||||||
|
|
||||||
|
|
||||||
|
class TestAppConfig(AppConfig): |
||||||
|
name = 'test_app' |
@ -0,0 +1,30 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
# Generated by Django 1.10.5 on 2017-02-19 18:55 |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
initial = True |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.CreateModel( |
||||||
|
name='Entry', |
||||||
|
fields=[ |
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||||
|
('title_de', models.CharField(max_length=200, verbose_name='Title (de)')), |
||||||
|
('title_en', models.CharField(blank=True, max_length=200, verbose_name='Title (en)')), |
||||||
|
('description_de', models.TextField(blank=True, verbose_name='Description (de)')), |
||||||
|
('description_en', models.TextField(blank=True, verbose_name='Description (en)')), |
||||||
|
], |
||||||
|
options={ |
||||||
|
'verbose_name': 'Entry', |
||||||
|
'verbose_name_plural': 'Entries', |
||||||
|
}, |
||||||
|
), |
||||||
|
] |
@ -0,0 +1,32 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
# Generated by Django 1.10.5 on 2017-02-20 09:28 |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('test_app', '0001_initial'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.CreateModel( |
||||||
|
name='Category', |
||||||
|
fields=[ |
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||||
|
('title_de', models.CharField(max_length=200, verbose_name='Title (de)')), |
||||||
|
('title_en', models.CharField(blank=True, max_length=200, verbose_name='Title (en)')), |
||||||
|
], |
||||||
|
options={ |
||||||
|
'verbose_name': 'Entry Category', |
||||||
|
'verbose_name_plural': 'Entry Categories', |
||||||
|
}, |
||||||
|
), |
||||||
|
migrations.AddField( |
||||||
|
model_name='entry', |
||||||
|
name='categories', |
||||||
|
field=models.ManyToManyField(blank=True, related_name='ideas', to='test_app.Category', verbose_name='Categories'), |
||||||
|
), |
||||||
|
] |
@ -0,0 +1,50 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from django.core.urlresolvers import reverse |
||||||
|
from django.core.urlresolvers import NoReverseMatch |
||||||
|
from django.db import models |
||||||
|
from django.utils.translation import ugettext_lazy as _ |
||||||
|
from django.utils.encoding import python_2_unicode_compatible |
||||||
|
|
||||||
|
from utils.fields import TranslatableCharField, TranslatableTextField |
||||||
|
|
||||||
|
|
||||||
|
@python_2_unicode_compatible |
||||||
|
class Category(models.Model): |
||||||
|
title = TranslatableCharField(_("Title"), max_length=200) |
||||||
|
|
||||||
|
class Meta: |
||||||
|
verbose_name = _("Entry Category") |
||||||
|
verbose_name_plural = _("Entry Categories") |
||||||
|
|
||||||
|
def __str__(self): |
||||||
|
return self.title |
||||||
|
|
||||||
|
|
||||||
|
@python_2_unicode_compatible |
||||||
|
class Entry(models.Model): |
||||||
|
title = TranslatableCharField( |
||||||
|
_("Title"), |
||||||
|
max_length=200, |
||||||
|
) |
||||||
|
description = TranslatableTextField( |
||||||
|
_("Description"), |
||||||
|
blank=True, |
||||||
|
) |
||||||
|
categories = models.ManyToManyField(Category, |
||||||
|
verbose_name=_("Categories"), blank=True, |
||||||
|
related_name="ideas") |
||||||
|
|
||||||
|
class Meta: |
||||||
|
verbose_name = _("Entry") |
||||||
|
verbose_name_plural = _("Entries") |
||||||
|
|
||||||
|
def __str__(self): |
||||||
|
return self.title |
||||||
|
|
||||||
|
def get_url_path(self): |
||||||
|
try: |
||||||
|
return reverse("idea_detail", kwargs={"id": self.pk}) |
||||||
|
except NoReverseMatch: |
||||||
|
return "" |
@ -0,0 +1,33 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from haystack import indexes |
||||||
|
from .models import Entry |
||||||
|
|
||||||
|
|
||||||
|
class EntryIndex(indexes.SearchIndex, indexes.Indexable): |
||||||
|
text = indexes.CharField(document=True) |
||||||
|
|
||||||
|
def get_model(self): |
||||||
|
return Entry |
||||||
|
|
||||||
|
def index_queryset(self, using=None): |
||||||
|
"""Used when the entire index for model is updated.""" |
||||||
|
return self.get_model().objects.all() |
||||||
|
|
||||||
|
# def index_queryset(self, using=None): |
||||||
|
# """Used when the entire index for model is updated.""" |
||||||
|
# if using and using != "default": |
||||||
|
# lang_code = using.replace("default_", "") |
||||||
|
# else: |
||||||
|
# lang_code = settings.LANGUAGE_CODE[:2] |
||||||
|
# return self.get_model().objects.filter(language=lang_code) |
||||||
|
|
||||||
|
def prepare_text(self, obj): |
||||||
|
# this will be called for each language / backend |
||||||
|
return "\n".join(( |
||||||
|
obj.title, |
||||||
|
obj.description, |
||||||
|
"\n".join([cat.title |
||||||
|
for cat in obj.categories.all()]), |
||||||
|
)) |
@ -0,0 +1,18 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
|
||||||
|
from django.test import TestCase |
||||||
|
from django.utils import translation |
||||||
|
|
||||||
|
from main.test_app.models import Entry |
||||||
|
|
||||||
|
|
||||||
|
translation.activate('de') |
||||||
|
|
||||||
|
Entry.objects.create(title_de="Deutscher Titel", title_en="English Title", description_de="Ein kleiner kurzer Text.", description_en="A short text.") |
||||||
|
|
||||||
|
from django.core.management import call_command |
||||||
|
|
||||||
|
call_command('rebuild_index') |
||||||
|
|
@ -0,0 +1,3 @@ |
|||||||
|
from django.shortcuts import render |
||||||
|
|
||||||
|
# Create your views here. |
@ -0,0 +1,21 @@ |
|||||||
|
"""main URL Configuration |
||||||
|
|
||||||
|
The `urlpatterns` list routes URLs to views. For more information please see: |
||||||
|
https://docs.djangoproject.com/en/1.10/topics/http/urls/ |
||||||
|
Examples: |
||||||
|
Function views |
||||||
|
1. Add an import: from my_app import views |
||||||
|
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') |
||||||
|
Class-based views |
||||||
|
1. Add an import: from other_app.views import Home |
||||||
|
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') |
||||||
|
Including another URLconf |
||||||
|
1. Import the include() function: from django.conf.urls import url, include |
||||||
|
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) |
||||||
|
""" |
||||||
|
from django.conf.urls import url |
||||||
|
from django.contrib import admin |
||||||
|
|
||||||
|
urlpatterns = [ |
||||||
|
url(r'^admin/', admin.site.urls), |
||||||
|
] |
@ -0,0 +1,16 @@ |
|||||||
|
""" |
||||||
|
WSGI config for main project. |
||||||
|
|
||||||
|
It exposes the WSGI callable as a module-level variable named ``application``. |
||||||
|
|
||||||
|
For more information on this file, see |
||||||
|
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ |
||||||
|
""" |
||||||
|
|
||||||
|
import os |
||||||
|
|
||||||
|
from django.core.wsgi import get_wsgi_application |
||||||
|
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings") |
||||||
|
|
||||||
|
application = get_wsgi_application() |
@ -0,0 +1,22 @@ |
|||||||
|
#!/usr/bin/env python |
||||||
|
import os |
||||||
|
import sys |
||||||
|
|
||||||
|
if __name__ == "__main__": |
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings") |
||||||
|
try: |
||||||
|
from django.core.management import execute_from_command_line |
||||||
|
except ImportError: |
||||||
|
# The above import may fail for some other reason. Ensure that the |
||||||
|
# issue is really that Django is missing to avoid masking other |
||||||
|
# exceptions on Python 2. |
||||||
|
try: |
||||||
|
import django |
||||||
|
except ImportError: |
||||||
|
raise ImportError( |
||||||
|
"Couldn't import Django. Are you sure it's installed and " |
||||||
|
"available on your PYTHONPATH environment variable? Did you " |
||||||
|
"forget to activate a virtual environment?" |
||||||
|
) |
||||||
|
raise |
||||||
|
execute_from_command_line(sys.argv) |
@ -0,0 +1,38 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from django.conf import settings |
||||||
|
from django.utils import translation |
||||||
|
|
||||||
|
from haystack import connections |
||||||
|
from haystack.constants import DEFAULT_ALIAS |
||||||
|
from xapian_backend import \ |
||||||
|
XapianSearchBackend, XapianSearchQuery, XapianEngine |
||||||
|
|
||||||
|
|
||||||
|
class MultilingualXapianSearchBackend(XapianSearchBackend): |
||||||
|
def update(self, index, iterable, commit=True, language_specific=False): |
||||||
|
if not language_specific and self.connection_alias == "default": |
||||||
|
current_language = (translation.get_language() or settings.LANGUAGE_CODE)[:2] |
||||||
|
for lang_code, lang_name in settings.LANGUAGES: |
||||||
|
using = "default_%s" % lang_code |
||||||
|
translation.activate(lang_code) |
||||||
|
backend = connections[using].get_backend() |
||||||
|
backend.update(index, iterable, commit, |
||||||
|
language_specific=True) |
||||||
|
translation.activate(current_language) |
||||||
|
elif language_specific: |
||||||
|
super(MultilingualXapianSearchBackend, self).\ |
||||||
|
update(index, iterable, commit) |
||||||
|
|
||||||
|
|
||||||
|
class MultilingualXapianSearchQuery(XapianSearchQuery): |
||||||
|
def __init__(self, using=DEFAULT_ALIAS): |
||||||
|
lang_code = translation.get_language()[:2] |
||||||
|
using = "default_%s" % lang_code |
||||||
|
super(MultilingualXapianSearchQuery, self).__init__(using) |
||||||
|
|
||||||
|
|
||||||
|
class MultilingualXapianEngine(XapianEngine): |
||||||
|
backend = MultilingualXapianSearchBackend |
||||||
|
query = MultilingualXapianSearchQuery |
@ -0,0 +1,107 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from django.conf import settings |
||||||
|
from django.db import models |
||||||
|
from django.utils.translation import get_language |
||||||
|
from django.utils.translation import string_concat |
||||||
|
|
||||||
|
|
||||||
|
class TranslatableCharField(models.CharField): |
||||||
|
|
||||||
|
def __init__(self, verbose_name=None, **kwargs): |
||||||
|
self._blank = kwargs.get("blank", False) |
||||||
|
self._editable = kwargs.get("editable", True) |
||||||
|
|
||||||
|
super(TranslatableCharField, self).__init__(verbose_name, **kwargs) |
||||||
|
|
||||||
|
def contribute_to_class(self, cls, name, virtual_only=False): |
||||||
|
if not cls._meta.abstract: |
||||||
|
for lang_code, lang_name in settings.LANGUAGES: |
||||||
|
if lang_code == settings.LANGUAGE_CODE: |
||||||
|
_blank = self._blank |
||||||
|
else: |
||||||
|
_blank = True |
||||||
|
|
||||||
|
localized_field = models.CharField( |
||||||
|
string_concat(self.verbose_name, " (%s)" % lang_code), |
||||||
|
name=self.name, |
||||||
|
primary_key=self.primary_key, |
||||||
|
max_length=self.max_length, |
||||||
|
unique=self.unique, |
||||||
|
blank=_blank, |
||||||
|
null=False, # intentionally ignored |
||||||
|
db_index=self.db_index, |
||||||
|
rel=self.rel, |
||||||
|
default=self.default or "", |
||||||
|
editable=self._editable, |
||||||
|
serialize=self.serialize, |
||||||
|
choices=self.choices, |
||||||
|
help_text=self.help_text, |
||||||
|
db_column=None, |
||||||
|
db_tablespace=self.db_tablespace |
||||||
|
) |
||||||
|
|
||||||
|
localized_field.contribute_to_class( |
||||||
|
cls, |
||||||
|
"%s_%s" % (name, lang_code), |
||||||
|
) |
||||||
|
|
||||||
|
def translated_value(self): |
||||||
|
# For empty / non-existing translation fall back to main field |
||||||
|
language = get_language() |
||||||
|
val = self.__dict__["%s_%s" % (name, language)] |
||||||
|
if not val: |
||||||
|
val = self.__dict__["%s_%s" % (name, settings.LANGUAGE_CODE)] |
||||||
|
return val |
||||||
|
|
||||||
|
setattr(cls, name, property(translated_value)) |
||||||
|
|
||||||
|
|
||||||
|
class TranslatableTextField(models.TextField): |
||||||
|
|
||||||
|
def __init__(self, verbose_name=None, **kwargs): |
||||||
|
self._blank = kwargs.get("blank", False) |
||||||
|
self._editable = kwargs.get("editable", True) |
||||||
|
|
||||||
|
super(TranslatableTextField, self).__init__(verbose_name, **kwargs) |
||||||
|
|
||||||
|
def contribute_to_class(self, cls, name, virtual_only=False): |
||||||
|
if not cls._meta.abstract: |
||||||
|
for lang_code, lang_name in settings.LANGUAGES: |
||||||
|
if lang_code == settings.LANGUAGE_CODE: |
||||||
|
_blank = self._blank |
||||||
|
else: |
||||||
|
_blank = True |
||||||
|
|
||||||
|
localized_field = models.TextField( |
||||||
|
string_concat(self.verbose_name, " (%s)" % lang_code), |
||||||
|
name=self.name, |
||||||
|
primary_key=self.primary_key, |
||||||
|
max_length=self.max_length, |
||||||
|
unique=self.unique, |
||||||
|
blank=_blank, |
||||||
|
null=False, # intentionally ignored |
||||||
|
db_index=self.db_index, |
||||||
|
rel=self.rel, |
||||||
|
default=self.default or "", |
||||||
|
editable=self._editable, |
||||||
|
serialize=self.serialize, |
||||||
|
choices=self.choices, |
||||||
|
help_text=self.help_text, |
||||||
|
db_column=None, |
||||||
|
db_tablespace=self.db_tablespace |
||||||
|
) |
||||||
|
|
||||||
|
localized_field.contribute_to_class( |
||||||
|
cls, "%s_%s" % (name, lang_code), |
||||||
|
) |
||||||
|
|
||||||
|
def translated_value(self): |
||||||
|
language = get_language() |
||||||
|
val = self.__dict__["%s_%s" % (name, language)] |
||||||
|
if not val: |
||||||
|
val = self.__dict__["%s_%s" % (name, settings.LANGUAGE_CODE)] |
||||||
|
return val |
||||||
|
|
||||||
|
setattr(cls, name, property(translated_value)) |
Loading…
Reference in new issue