Güngör Budak's 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.

Convert XLS/XLSX to CSV in Bash

In most of the modern Linux distributions, Libre Office is available and it can be used to convert XLS or XLSX file(s) to CSV file(s) in bash.

For XLS file(s):

for i in *.xls; do libreoffice --headless --convert-to csv "$i"; done

For XLSX file(s):

for i in *.xlsx; do libreoffice --headless --convert-to csv "$i"; done

You may get following warning but it still works fine:

javaldx: Could not find a Java Runtime Environment!<br />Warning: failed to read path from javaldx

You’ll see a standard output similar to following when the conversion is successful:

convert /home/gbudak/Downloads/cmap/cmap_instances_02.xls > /home/gbudak/Downloads/cmap/cmap_instances_02.csv using Text - txt - csv (StarCalc)

Taken from an SO answer.

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(
    ...
)

Obtaining Molecule Description using Open Babel / PyBel

Open Babel is a great tool to analyze and investigate molecular data (.MOL, .SDF files). Its Python API is particularly very nice if you are familiar with Python already. In this post, I’ll demonstrate how you can obtain molecule description such as molecular weight, HBA, HBD, logP, formula, number of chiral centers using PyBel.

Installation

$ sudo apt-get install openbabel python-openbabel

Usage for MW, HBA, HBD, logP

After reading .MOL file, we need to use calcdesc method with descnames argument for getting the descriptions. Getting formula is different, though.

gungor@gungors-mint ~/Desktop $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pybel import *
>>> mol = readfile('mol', 'benzene.mol').next()
>>> desc = mol.calcdesc(descnames=['MW', 'logP', 'HBA1', 'HBD'])
>>> desc['formula'] = mol.formula
>>> print desc<br />{'HBA1': 0.0, 'HBD': 0.0, 'MW': 78.11184, 'logP': 1.6865999999999999, 'formula': 'C6H6'}

Usage for Number of Chiral Centers

This is not included in PyBel, or I couldn’t find it but Open Babel comes with obchiral terminal command which returns the chiral atoms found in the molecules. We can parse this output to get the number of chiral centers.

>>> from subprocess import Popen, PIPE
>>> p = Popen(['obchiral', 'brg_flap_1.mol'], stdout=PIPE, stderr=PIPE)
>>> stdout, stderr = p.communicate()
>>> chiral_num = stdout.count('Is Chiral')
>>> print chiral_num
1

So you can use above codes to get molecule description including molecular weight, HBA, HBD, logP, formula, number of chiral centers.

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