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 #12 from RouteAtlas/feature/inline_widgets
Browse files Browse the repository at this point in the history
Added widget customization to TranslationInlineForm
  • Loading branch information
angvp committed Nov 28, 2014
2 parents 14e84df + 22a51e2 commit 09b9c34
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
49 changes: 35 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,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 +50,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 +66,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
19 changes: 19 additions & 0 deletions klingon/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 09b9c34

Please sign in to comment.