Thursday, June 19, 2014

Setting up a web project using Grunt, Bower and Yeoman

Grunt: task runner for minification, compilation, unit testing, linting, etc

Bower: Bower is a package manager for the front-end. It offers a generic, unopinionated solution to the problem of front-end package management. It installs the requested package with all dependencies.

Yeoman: is web project scafolding tool. we need to install its genarators for different projects. For example: angular-genarator, or melonjs-generator

brew install npm
npm install -g bower
bower install melonJS
npm install -g yeoman
npm install -g grunt
npm install -g grunt-cli
npm install -g generator-melonjs

mkdir project-folder
cd path/to/project-folder
yo melonjs

Answer to the questions then edit package.json add the followings to dependencies

"grunt-contrib-less": "latest",
"grunt-contrib-watch": "latest"
Run the following command:
npm install

Edit Gruntfile.js as the following

--and edit the paths:
less: {
      style: {
        files: {
          "public/css/style.css": "less/style.less"
    watch: {
      js: {
        files: ['javascript/*.js'],
        tasks: ['concat:js', 'uglify:js'],
        options: {
          livereload: true,
      css: {
        files: ['less/*.less'],
        tasks: ['less:style'],
        options: {
          livereload: true,
Now run grunt watch 

Sunday, January 20, 2013

Monkey Patching Django Model Class

Django is shipped with some useful application, also there are lots of third party re-usable application written for Django. Sometime you need to modify or customize these applications(Models of application) to meet your software requirements. One of the usual solution is adding a Model class to have a one-to-one relationship with the third party Models. This solution has some disadvantages such as having more tables and classes which can highly adversely affect database performance, scalability and add more complexity to the software development. 
I think of another solution to this problem. I used Django South along with monkey patching of third party Model classes. I added a field to Django user class. Here is a walk through over this approach.

1. Add an application:
python startapplication userpatch

2. Add the following  code to add the new field to the user class

from django.db import models
from django.contrib.auth.models import User

User.add_to_class('open_id', models.CharField(max_length=250,blank=True))

3. Add this field to User admin interface: (This can be done in either or

from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display += ('open_id',)
UserAdmin.fieldsets[0][1]['fields'] += ('open_id',)

4. add the application to INSTALLED_APP in project settings file. 

    # Uncomment the next line to enable the admin:
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',

5. By default South stores the migration files inside the app folder. Since we might have different app using  the same Django installation we want to save these files in different location. As of South 0.7 you can define location where South access the migration files. Add the following dictionary to the project settings file change the default migration directory.

    'auth': 'userpatch.migrations',

6. Run the schemamigration and migrate for this application.
python schemamigration userpatch --auto
python migrate userpatch

NOTE: I assumed that you have already added the application to South.