Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from RouteAtlas/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
angvp committed Dec 8, 2014
2 parents 295a779 + 5ab7640 commit c9a0b8a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 26 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
language: python
python:
- "2.7"
- "3.4"
env:
- DJANGO=1.5
- DJANGO=1.6.4
- DJANGO=1.7.1
install:
- pip install -q Django==$DJANGO --use-mirrors
- pip install -q nose django-nose --use-mirrors
- pip install -q coverage coveralls --use-mirrors
- pip install -e . --use-mirrors
- pip install -q Django==$DJANGO
- pip install -q nose django-nose
- pip install -q coverage coveralls
- pip install -e .
script:
- coverage run --source klingon setup.py test
after_success:
Expand Down
56 changes: 42 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
klingon
========================


.. image:: https://coveralls.io/repos/RouteAtlas/django-klingon/badge.png?branch=master
:target: https://coveralls.io/r/RouteAtlas/django-klingon?branch=master

.. image:: https://travis-ci.org/RouteAtlas/django-klingon.svg?branch=master
:target: http://travis-ci.org/RouteAtlas/django-klingon

Welcome to the documentation for django-klingon!

django-klingon is an attempt to make django model translations suck
Expand All @@ -10,40 +17,36 @@ Setup & Integration
------------------------------------

In your settings files:
add django-klingon to INSTALLED_APPS:
add django-klingon to INSTALLED_APPS::

```
INSTALLED_APPS = (
...
'klingon',
...
)
```

specify a default language if you want to use your fields to store the
default language:
default language::

KLINGON_DEFAULT_LANGUAGE = 'en'

Extend you models to add API support:
first add Translatable to your model Class definition. This will add the
API functions
API functions::

```
from klingon.models import Translatable
...
class Book(models.Model, Translatable):
...
```

in the same model add an attribute to indicate which fields will be
translatables:
translatables::

...
translatable_fields = ('title', 'description')
...

your model should look like this:
your model should look like this::

class Book(models.Model, Translatable):
title = models.CharField(max_length=100)
Expand All @@ -54,9 +57,11 @@ your model should look like this:


Add admin capabilities:
______________________

you can include an inline to your model admin and a custom action
to create the translations. To do this in your ModelAdmin class do
this:
this::

from klingon.admin import TranslationInline, create_translations
...
Expand All @@ -68,32 +73,55 @@ this:
* see full example in example_project folder of source code of klingon


Using Specific Widgets in the TranslationInline form of the admin:
________________________________________________

You can specify the widget to be use on an inline form by passing a dictionary to TranslationInlineForm.
So, you might want to extend the TranslationInline with a new form that will a "widgets" dictionary,
where you can specify the widget that each filds has to use, for example::

class RichTranslationInlineForm(TranslationInlineForm):
widgets = {
'CharField': forms.TextInput(attrs={'class': 'klingon-char-field'}),
'TextField': forms.Textarea(attrs={'class': 'klingon-text-field'}),
}

class RichTranslationInline(TranslationInline):
form = RichTranslationInlineForm

and then you just simply use the RichTranslationInline class on your AdminModels, for example::

class BookAdmin(admin.ModelAdmin):
inlines = [RichTranslationInline]

* see full example in example_project folder of source code of klingon

Using the API
------------------------------------

To create the translation you can do the follwing:

Suppose that you have and object called book:
Suppose that you have and object called book::

> book = Book.objects.create(
title="The Raven",
description="The Raven is a narrative poem",
publication_date=datetime.date(1845, 1, 1)
)

you can create translation for that instances like this
you can create translation for that instances like this::

> book.set_translation('es', 'title', 'El Cuervo')
> book.set_translation('es', 'description', 'El Cuervo es un poema narrativo')

a translation could be access individually:
a translation could be access individually::

> self.book.get_translation('es', 'title')
'El Cuervo'
> book.get_translation('es', 'description')
'El Cuervo es un poema narrativo'

or you can get all translations together:
or you can get all translations together::

> self.book.translations('es')
{
Expand Down
15 changes: 13 additions & 2 deletions example_project/testapp/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
from django import forms
from django.contrib import admin
from klingon.admin import TranslationInline, create_translations
from klingon.admin import TranslationInline, TranslationInlineForm, create_translations

from models import Book, Library

class RichTranslationInlineForm(TranslationInlineForm):
widgets = {
'CharField': forms.TextInput(attrs={'class': 'klingon-char-field'}),
'TextField': forms.Textarea(attrs={'class': 'klingon-text-field'}),
}

class RichTranslationInline(TranslationInline):
form = RichTranslationInlineForm


class BookAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'description', 'translations_link')
search_fields = ['title', 'description']
list_filter = ['publication_date',]
inlines = [TranslationInline]
inlines = [RichTranslationInline]
actions = [create_translations]

admin.site.register(Book, BookAdmin)
Expand Down
2 changes: 1 addition & 1 deletion klingon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
no integrations pain in your app!
"""

__version__ = '0.0.2'
__version__ = '0.0.3'
21 changes: 20 additions & 1 deletion klingon/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.forms.models import ModelForm
from django.utils.translation import ugettext as _

from models import Translation
from .models import Translation


class TranslationAdmin(admin.ModelAdmin):
Expand All @@ -22,9 +22,28 @@ def create_translations(modeladmin, request, queryset):


class TranslationInlineForm(ModelForm):
widgets = {
'CharField': forms.TextInput(),
'TextField': forms.Textarea(),
}

class Meta:
model = Translation

def __init__(self, *args, **kwargs):
res = super(TranslationInlineForm, self).__init__(*args, **kwargs)
# overwrite the widgets for each form instance depending on the object and widget dict
if self.widgets and self.instance and hasattr(self.instance, 'content_type'):
model = self.instance.content_type.model_class()
field = model._meta.get_field(self.instance.field)
# get widget for field if any
widget = self.widgets.get(field.get_internal_type())
if widget:
translation = self.fields['translation']
# overwrite widget to field
translation.widget = widget
return res

def clean_translation(self):
"""
Do not allow translations longer than the max_lenght of the field to
Expand Down
2 changes: 1 addition & 1 deletion klingon/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.cache import cache
from django.test import TestCase
from django.test.utils import override_settings
from testapp.models import Book, Library
from .testapp.models import Book, Library
from klingon.models import Translation, CanNotTranslate


Expand Down
2 changes: 1 addition & 1 deletion klingon/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase
from testapp.models import Book
from .testapp.models import Book
from klingon.models import Translation


Expand Down
1 change: 1 addition & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
('pt_br', 'Brazilian Portuguese'),
('es', 'Spanish'),
),
MIDDLEWARE_CLASSES=(),
)


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def read_file(filename):
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Framework :: Django',
'Development Status :: 4 - Beta',
'Operating System :: OS Independent',
Expand Down
8 changes: 6 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
[tox]
downloadcache = {toxworkdir}/_download/
envlist = py27-1.7.X,py27-1.6.X,py27-1.5.X,py27-1.4.X
envlist = py34-1.7.X,py27-1.7.X,py27-1.6.X,py27-1.5.X,py27-1.4.X

[testenv]
commands = {envpython} runtests.py

[testenv:py34-1.7.X]
basepython = python3.4
deps = https://www.djangoproject.com/download/1.7.1/tarball/

[testenv:py27-1.7.X]
basepython = python2.7
deps = https://www.djangoproject.com/download/1.7a2/tarball/
deps = https://www.djangoproject.com/download/1.7.1/tarball/

[testenv:py27-1.6.X]
basepython = python2.7
Expand Down

0 comments on commit c9a0b8a

Please sign in to comment.