GithubHelp home page GithubHelp logo

gmcd / django-slim Goto Github PK

View Code? Open in Web Editor NEW

This project forked from barseghyanartur/django-slim

0.0 2.0 0.0 282 KB

Simple implementation of multi-lingual models for Django. Django-admin integration works out of the box. Supports django-localeurl integration.

Home Page: https://pypi.python.org/pypi/django-slim

Shell 1.96% Python 96.04% HTML 2.00%

django-slim's Introduction

django-slim

Simple implementation of multi-lingual models for Django. Django-admin integration works out of the box. Supports django-localeurl integration.

Prerequisites

  • Django 1.5, 1.6, 1.7
  • Python 2.7.+, 3.3.+

Installation

Note, that Django 1.5 is required. Earlier versions are not supported.

  1. Installation

Latest stable version on PyPI:

$ pip install django-slim

Latest stable version on bitbucket:

$ pip install -e hg+https://bitbucket.org/barseghyanartur/django-slim@stable#egg=django-slim

Latest stable version on github:

$ pip install -e git+https://github.com/barseghyanartur/django-slim/@stable#egg=django-slim
  1. Add slim to INSTALLED_APPS of you settings module.

Usage and examples

An extensive example project is available at https://github.com/barseghyanartur/django-slim/tree/stable/example directory.

Screenshots are present in documentation on PythonHosted (http://pythonhosted.org/django-slim/#screenshots).

Demo

In order to be able to quickly evaluate the django-slim, a demo app (with a quick installer) has been created (Debian only). Follow the instructions below for having the demo running within a minute.

Grab the latest django_slim_example_app_installer.sh

$ wget https://raw.github.com/barseghyanartur/django-slim/stable/example/django_slim_example_app_installer.sh

Create a new- or switch to existing- virtual environement, assign execute rights to the installer and run the django-slim-example-app-install.sh.

$ chmod +x django_slim_example_app_installer.sh

$ ./django_slim_example_app_installer.sh

Go to the front/back -end and test the app.

Let's now step-by-step review our imaginary example app.

settings.py

Add slim to installed apps.

>>> INSTALLED_APPS = (
>>>     # ...
>>>     'slim',
>>>     # ...
>>> )

Add languages.

>>> LANGUAGES = (
>>>     ('en', gettext("English")), # Main language!
>>>     ('hy', gettext("Armenian")),
>>>     ('nl', gettext("Dutch")),
>>>     ('ru', gettext("Russian")),
>>> )

example/models.py

>>> from django.db import models
>>>
>>> from slim import LanguageField, Slim
>>>
>>> class FooItem(models.Model, Slim):
>>>     title = models.CharField(_("Title"), max_length=100)
>>>     slug = models.SlugField(unique=True, verbose_name=_("Slug"))
>>>     body = models.TextField(_("Body"))
>>>     language = LanguageField()

example/admin.py

>>> from django.contrib import admin
>>>
>>> from slim.admin import SlimAdmin
>>>
>>> class FooItemAdmin(SlimAdmin):
>>>     list_display = ('title',)
>>>     fieldsets = (
>>>         (None, {
>>>             'fields': ('title', 'slug', 'body')
>>>         }),
>>>     )
>>>
>>> admin.site.register(FooItem, FooItemAdmin)

example/views.py

We assume that language code is kept in the request object (django-localeurl behaviour, which you're advised to use).

>>> from slim import get_language_from_request
>>>
>>> from example.models import FooItem
>>>
>>> def browse(request, template_name='foo/browse.html'):
>>>     language = get_language_from_request(request)
>>>     queryset = FooItem._default_manager.filter(language=language)
>>>
>>>     # The rest of the code

More on ORM filtering

>>> from example.models import FooItem
>>> foo = FooItem._default_manager.all()[0]
<FooItem: Lorem ipsum>

Let's assume, we have such record and it has been translated to Armenian (hy) and Dutch (nl). Original translation is named Lorem ipsum. Other translations have the language code appended to the title.

>>> armenian_foo = foo.get_translation_for('hy')
<FooItem: Lorem ipsum HY>
>>> dutch_foo = foo.get_translation_for('nl')
<FooItem: Lorem ipsum NL>

If we have a translated object, we can always get the main translation.

>>> armenian_foo.original_translation == foo
True

All available translations for foo:

>>> foo.available_translations()
[<FooItem: Lorem ipsum HY>, <FooItem: Lorem ipsum NL>]

All available translations for Armenian foo.

>>> armenian_foo.available_translations()
[<FooItem: Lorem ipsum>, <FooItem: Lorem ipsum NL>]

See https://github.com/barseghyanartur/django-slim/tree/stable/example directory for a working example.

django-localeurl integration

Installation

django-localeurl integration is fully supported for Python 2.6.* and 2.7.* and installs automatically when installing django-slim. If you are using Python 3, install a forked version of django-localeurl (since official version does not yet have support for Python 3).

Forked version from bitbucket:

$ pip install -e hg+https://bitbucket.org/barseghyanartur/django-localeurl@stable#egg=localeurl

Integration

Use slim.models.decorators.auto_prepend_language decorator in order to have it working.

Example (have in mind our FooItem model.

>>> from django.core.urlresolvers import reverse
>>>
>>> from slim.models.decorators import auto_prepend_language
>>>
>>> class FooItem(models.Model):
>>>     # Some other code; have in mind previous pieces.
>>>     @auto_prepend_language
>>>     def get_absolute_url(self):
>>>         kwargs = {'slug': self.slug}
>>>         return reverse('foo.detail', kwargs=kwargs)

Do not forget to add the LocaleURLMiddleware to the MIDDLEWARE_CLASSES (as first).

>>> MIDDLEWARE_CLASSES = (
>>>     'localeurl.middleware.LocaleURLMiddleware',
>>>     # The rest...
>>> )

Also, add localeurl to INSTALLED_APPS.

>>> INSTALLED_APPS = (
>>>     # Some apps...
>>>     'localeurl',
>>>     # Some more apps...
>>> )

License

GPL 2.0/LGPL 2.1

Support

For any issues contact me at the e-mail given in the Author section.

Author

Artur Barseghyan <[email protected]>

Bitdeli badge

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.