GÜNGÖR BUDAK

Bioinformatics Analyst at Seven Bridges & PhD Student at METU

Blog

Bioinformatics, web programming, coding in general

Mezzanine BS Banners Translation with django-modeltranslation

Mezzanine BS Banners is a nice app for implementing Bootstrap 3 banners/sliders to your Mezzanine projects. The Banners model in BS Banners app has a title and its stacked inline Slides model has title and content for translation.

After installing and setting up Django/Mezzanine translations:

Create a translation.py inside your Mezzanine project or your custom theme/skin application and copy/paste following lines:

from modeltranslation.translator import translator
from mezzanine.core.translation import TranslatedSlugged, TranslatedRichText
from mezzanine_bsbanners.models import Banners, Slides


class TranslatedBanners(TranslatedSlugged):
   fields = ('title', )

class TranslatedSlides(TranslatedRichText):
   fields = ('title', 'content', )

translator.register(Banners, TranslatedBanners)
translator.register(Slides, TranslatedSlides)

For admin integration, create or use your existing admin.py in your Mezzanine project or your custom theme/skin application and copy/paste following lines:

from django.contrib import admin
from mezzanine_bsbanners.admin import SlidesInline, BannersAdmin
from mezzanine_bsbanners.models import Banners, Slides
from modeltranslation.admin import TranslationAdmin, TranslationStackedInline


class TranslatedSlidesInline(SlidesInline, TranslationStackedInline):
   model = Slides

class TranslatedBannersAdmin(BannersAdmin, TranslationAdmin):
   inlines = [TranslatedSlidesInline, ]

   def formfield_for_dbfield(self, db_field, **kwargs):
       field = super(TranslatedBannersAdmin, self).formfield_for_dbfield(db_field, **kwargs)
       self.patch_translation_field(db_field, field, **kwargs)
       return field
       
admin.site.unregister(Banners)
admin.site.register(Banners, TranslatedBannersAdmin)

Run following to create fields in database tables for translations:

python manage.py sync_translation_fields
python manage.py update_translation_fields

This completes it.

Django/Mezzanine Content Translation for Mezzanine Built-in Applications

As Mezzanine comes with additional Django applications such as pages, galleries and to translate their content, Mezzanine supports django-modeltranslation integration.

Install django-modeltranslation:

pip install django-modeltranslation

Add following to the INSTALLED_APPS in settings.py:

"modeltranslation",

And following in settings.py:

USE_MODELTRANSLATION = True

Also, move mezzanine.pages to the top of other Mezzanine apps in INSTALLED_APPS in settings.py like so:

"mezzanine.pages",
"mezzanine.boot",
"mezzanine.conf",
"mezzanine.core",
"mezzanine.generic",
"mezzanine.blog",
"mezzanine.forms",
"mezzanine.galleries",
"mezzanine.twitter",
"mezzanine.accounts",
"mezzanine.mobile",

Run following to create fields in database tables for translations:

python manage.py sync_translation_fieldspython manage.py update_translation_fields

Also, migrations might be needed:

python manage.py makemigrationspython manage.py migrate

And done.

Setting Up Templates and Python Scripts for Translation

Templates need following template tag:

{% load i18n %}

Then, wrapping any text with

{% trans "TEXT" %}

will make it translatable via Rosetta Django application

In Python scripts, you need to import following library:

from django.utils.translation import ugettext_lazy as _

Then wrapping any text with

_('TEXT')

will make it translatable.

Django Rosetta Translations for Django Applications

Make a directory called locale/ under the application directory:

cd app_name
mkdir locale

Add the folder in LOCAL_PATHS dictionary in settings.py:

LOCALE_PATHS = (
    os.path.join(PROJECT_ROOT, 'app_name', 'locale/'),
)

Run the following command to create PO translation file for the application:

python ../manage.py makemessages -l tr -e html,py,txt
python ../manage.py compilemessages

Option -l is for language, it should match your definition in settings.py:

LANGUAGES = (
    ('en' _('English')),
    ('tr' _('Turkish')),
    ('it' _('Italian')),
)

Repeat the last step for all languages and the go to Rosetta URL to translate.

Django Rosetta Installation

Install SciPy:

sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose

Install pymongo and nltk:

sudo pip install pymongo
sudo pip install nltk

Install Python MySQLdb:

sudo apt-get install python-mysqldb

Install Rosetta:

sudo pip install django-rosetta

Add following into INSTALLED_APPS in settings.py:

"rosetta",

Add following into urls.py:

url(r’^translations/’, include(‘rosetta.urls’)),

To also allow language prefixes, change patters to i18n_patterns in urls.py:

urlpatterns += i18n_patterns(
    ...
)

Running Script on Cluster (StarCluster)

Start a new cluster with the configuration file you modified:

starcluster start cluster_name

Send the script to the running cluster:

starcluster put cluster_name myscr.csh /home/myscr.csh

Run it using source:

starcluster sshmaster cluster_name "source /home/myscr.csh >& /home/myscr.log"

Errno 13 Permission denied Django File Uploads

Run following command to give www-data permissions to static folder and all its content:

cd path/to/your/django/project
sudo chown -R www-data:www-data static/

Do this in your production server

Configuring Mezzanine for Apache server & mod_wsgi in AWS

Install Mezzanine, Apache server and mod_wsgi:

sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi

Set up a MySQL database for your Mezzanine project

Read my post on how to set up a MySQL database for a Mezzanine project

Collect static files:

python manage.py collectstatic

Configure your Apache server configuration for the project like following:

WSGIPythonPath /home/ubuntu/www/mezzanine-project

<VirtualHost *:80>
    #ServerName example.com
    ServerAdmin admin@example.com
    DocumentRoot /home/ubuntu/www/mezzanine-project
    WSGIScriptAlias / /home/ubuntu/www/mezzanine-project/wsgi.py
    Alias /static /home/ubuntu/www/mezzanine-project/static/

    <Directory /home/ubuntu/www/mezzanine-project>
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note: Replace mezzanine-project with the name of your project

Configure wsgi.py (I don’t know why but it needs this fix - complete script):

from __future__ import unicode_literals
import os
import sys

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(PROJECT_ROOT, ".."))
settings_module = "%s.settings" % PROJECT_ROOT.split(os.sep)[-1]
os.environ["DJANGO_SETTINGS_MODULE"] = settings_module

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Enable the site and restart the server:

sudo a2ensite mezzanine-project.conf
sudo service apache2 restart

Now, when you navigate to your AWS public DNS, your Mezzanine project should be running.

Source: Vimmaniac

How to Set Up a MySQL Database for a Mezzanine Project

Install MySQL server and python-mysqldb package:

sudo apt-get install mysql-server
sudo apt-get install python-mysqldb

Run MySQL:

mysql -u root -p

Create a database:

mysql> create database mezzanine_project;

Confirm it:

mysql> show databases;

Exit:

mysql> exit

Configure local_settings.py:

cd path/to/your/mezzanine/projectnano local_settings.py

Like following:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "mezzanine_project",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "",
        "PORT": "",
        }
    }

Note: Replace your password