Because Evejob Project developed in few frameworks, we need to make clear “terminology” differences used by different frameworks as well as different strategies to develop MVC applications using such frameworks. Let’s start from MVC frameworks: 1) ASP.NET MVC as we see from name is “Model-View-Controller” framework (powered by .NET framework), so use actually concepts of “Models”, “Views” and “Controllers”. “Views” and “Controllers” concepts defined by framework itself (i.e. you need to inherit controllers from System.Web.Mvc.Controller class and inherit views from System.Web.Mvc.ViewPage to use build-in view engine). It is also possible in ASP.NET MVC to attach third-party View Engine (just google and you will found at least few). But “Models” concept in ASP.NET MVC is true “concept” – you don’t need to inherit from some base class or implement some interface to made a models in ASP.NET MVC! Instead framework provide you will full flexibility to create your own domain model and select any persistent framework if you need to persist your model entities. You can use for example Entity Framework, Linq to SQL, any other ORM or even POCO to implement your models! 2) Ruby On Rails is true “Model-View-Controller” framework and more so it uses “convention over configuration” principle. This mean that you actually have already defined conventions where to put your model entities, where to put controllers and where to put views (templates) and how to implement them (from what class you need to inherit etc). Sure while it is still possible to made some changes and even use another ORMs etc, build-in (OK, read instead “installed by default with Rails”) ActiveRecord ORM is enough for most cases (OK, at least in most simple cases ). The same things with templates (views) – you can use build in template engines (provided by ActiveView module) or add your own – at the time I write this exists not less then 20! different third-party template engines in addition to build-in (sure you can always build your own if need and made 21-th)! 3) Grails is really “Model-View-Controller” framework, was build with same principles like RoR, but because Grails work on top of Spring and Hibernate, developers initially get BEST build-in enterprise infrastructure from Java world! And sure in Grails infrastructure was extended so much (thanks to power of Groovy), that it looks like it most powerful MVC framework to the moment (this is just my personal opinion – I don’t want to start a war here )!
So frameworks listed above all follow MVC terminology where you actually create your own Models (Domain Models possible with Persistence), implement Controller (with actions) and create presentation using Views (that is actually powered by some build-in or custom template engine).
One framework – Django – that we use for Evejob project made use of a little different terminology (instead of MVC): Model-View-Template. Model term is used for same purposes like Model in MVC – it is simply domain model (provided usually by build-in to Django ORM, but also possible to use your own ORM… just note that it require little more “tuning / hacking” than in other frameworks). View in Django play the same role as Controller in MVC. And “Template” term in MVT actually used instead of “View” from MVC (while Template terms actually also used in MVC in sense that view CAN be represented as result of Template rendering) What I found from my experience that this difference in terminology does NOT make any actually impact on development process! More so, actually most MVCs that available in the market used mostly in “MTC” mode – i.e. “Model-Template-Controller”. What does it means? It means that Controllers actually render Templates in MOST of the cases! Sure in case if you use MVC framework to build for example Web Services, you will probably not render any Templates and your controllers (controllers in MVC or your Views in MVT) actually more likely will return JSON or XML directly (OK… XML it is possible also to build using Templates sometimes – look how it is done in Grails for example! But let’s think here that we want to return raw XML from Controller without any template engine). Sometimes you also can return JavaScript or any other format etc directly from Views in MVT or from Controllers in MVC. What is good in MVT is “order” of letters that give better understanding for developers that first time here about this pattern – “Model” <-> “View” <-> “Template”. This way logical flow going – i.e. views are in the “middle” of this logical flow! (using this principle it was better initially probably call MVC as MCV, i.e. “Model” <-> “Controller” <-> “View” , so “Controllers” become in the middle, center of logic). It related only to logical structure, because in MVC pattern, Model accessible (and used) actually in both Controller and View, same like in MVT both Views and Templates share same Model! You can read a short overview of MVC at Wikipedia article for example if want little more information) But as I tell before – main thing is “understanding” how pattern works, not how we call it! And “idea” of implementation of this patter in Django and other frameworks is exactly same even so name is not!
Some interesting Django related links:
OK, in Part #1 we already figure out what Models we need to create. Because Django support concept of “applications” inside one project, we can actually start creating models for 3 applications:
Django internally support following concepts that will be used in Accounts application:
cd c:\evejob\EvejobDJ python manage.py startapp accounts
The same way, you can create now “common” and “companies” applications in EvejobDJ project.
Let’s take a look into possible simple “accounts” application model.py file:
from django.db import models from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ class UserProfile(models.Model): # required field - user that own profile user = models.ForeignKey(User, unique=True) # optional Job Title job_title = models.CharField(_("Job Title"), max_length=128, null = True, blank = True) # optional Phone Number phone = models.CharField(_("Phone"), max_length=16, null = True, blank = True) # Boolean stored as tinyint in MySQL database, but it's ok for now allow_site_to_contact_by_email = models.BooleanField(_("Allow site to contact me by email")) allow_site_partners_to_contact_by_email = models.BooleanField(_("Allow site partners to contact me by email")) allow_site_to_contact_by_phone = models.BooleanField(_("Allow site to contact me by phone")) allow_site_partners_to_contact_by_phone = models.BooleanField(_("Allow site partners to contact me by phone")) def __unicode__(self): return self.user.username
Let’s review now most important aspects of this code:
from django.contrib.auth.models import User
Line above import already defined in Django User Model. We not going to create our own User Model and will try to reuse as much as possible from what is available in Django framework. But because we need to hold additional user details (like job title or phone for example), we create “UserProfile” model, that reference User as Foreign Key:
# required field - user that own profile user = models.ForeignKey(User, unique=True)
Also we are going to use “Internationalization” (I18N) build in into Django functionality to made our site available in few languages:
from django.utils.translation import ugettext_lazy as _
After such import, to translate string from one language into another we need just use _(”String to translate”) pattern (sure and also define translations some way – more about it in dedicated article)
Other code just define different fields of UserProfile that we want actually to store in Database (some of them are strings, some booleans).
Last two lines used to get a “string” representation of the model that will be used in Django Admin for example (same idea like you can “override ToString()” in C#):
def __unicode__(self): return self.user
That’s it for accounts application regarding models!
For “common” application we will have to define two model classes (City and Country):
from django.db import models from django.contrib.auth.models import User import EvejobDJ.settings as settings from django.utils.translation import ugettext_lazy as _ class Country(models.Model): # required Unique Company Name name = models.CharField(_("Name"), max_length=255, unique = True) def __unicode__(self): return '%s' % (self.name) class Meta: verbose_name = _('Country') verbose_name_plural = _('Countries') class City(models.Model): # required Unique Company Name name = models.CharField(_("Name"), max_length=255, unique = True) country = models.ForeignKey(Country, unique = False) def __unicode__(self): return '%s' % (self.name) class Meta: verbose_name = _('City') verbose_name_plural = _('Cities')
Exactly same logic like for Profiles, with small addition to made correct “plural” name for entity using definitions for “verbose_name_plural” field on Meta class. This will allow to see “Cities” or “Countries” in Admin, instead of automatic “Citys” or “Countrys”
class Meta: verbose_name = _('City') verbose_name_plural = _('Cities')
And last models that we need to produce in Sprint #1 is for Companies – Company and CompanyLogo. Let’s review following code:
from django.db import models from django.contrib.auth.models import User import EvejobDJ.settings as settings from django.utils.translation import ugettext_lazy as _ class Company(models.Model): # required Unique Company Name name = models.CharField(_("Name"), max_length=255, unique = True) # user that create this company first time. creation_by = models.ForeignKey(User, unique = False) # when user create this company first time. Django Automatically set the field to now when Company created for the first time creation_date = models.DateTimeField(_("Create Date"), auto_now_add = True) city = models.ForeignKey('common.City', unique = False, null = True, blank = True) country = models.ForeignKey('common.Country', unique = False, null = True, blank = True) # logo = models.OneToOneField('CompanyLogo', related_name='logo', verbose_name = "company logo") # But this is not needed, as you always can get access to logo using something like # (read more http://docs.djangoproject.com/en/dev/topics/db/queries/#one-to-one-relationships) # e = Company.objects.get(id=2) # e.companylogo def __unicode__(self): return '%s' % (self.name) class Meta: verbose_name = _('Company') verbose_name_plural = _('Companies') class CompanyLogo(models.Model): company = models.OneToOneField(Company, primary_key=True) # user that create this logo first time. creation_by = models.ForeignKey(User, unique = False) # when user create this logo first time. Django Automatically set the field to now when logo created for the first time creation_date = models.DateTimeField(_("Create Date"), auto_now_add = True) caption = models.CharField(blank=True, null = True, max_length=100) image = models.ImageField(upload_to=settings.COMPANY_LOGO_RELATIVE_ROOT) def __unicode__(self): return '%s' % (self.company.name) class Meta: verbose_name = _('Company Logo') verbose_name_plural = _('Companies Logos')
Let’s take a look into some interesting things here… First of all, we will store currently company logo images in file system (but later we can use another storage provider). Thanks to Django, it have special field type for this: “ImageField”. Ones we add field with this type, we need to make sure that we install PIL library. You can read about some installation issues of it on dedicated post). Second take a look into “auto_now_add” parameter in creation_date field – this will instruct Django to automatically set the field to current date when user create new company logo! And last most important thing in this models is One To One relation (using models.OneToOneField) between CompanyLogo and Company entities! You can read more information about Django support of “One To One” relations” if you interesting in details.
That’s all regarding Models itself. Let me know if have any questions!
Notes: To run code that you see above (at least to be able to create database using ‘python manage.py syncdb’ command), you need to made some settings in settings.py file. At minimum you need to add “accounts”, “companies” and “common” into Django Applications list:
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.flatpages', 'common', 'accounts', 'companies', )
Also you probably need to define some settings like COMPANY_LOGO_RELATIVE_ROOT in same settings.py file. In any case, I recommend to wait a little and read next post to get more information how to actually run EvejobDJ models and create views! So keep reading!
P.S. And last thing: I want to let you know that Python is NOT my “primary language of choice”. Means that I DON’T do everyday coding on it (instead of C# for example), so if you see (and feel) that I do something wrong, or that it can be done in Python better please let me know!
We will need to use MySQL for Python project for connection from Django Python code to MySQL database. Because I (and hope you also) use Windows 7 64 bit with Python 64 bit, needs to found and install 64 bit version of such library. On original project site, for some reason I could not found (at least in the moment when I write post), so I get it from different site: http://www.codegood.com/download/3/. Just download and install it (it will automatically found your Python installation if you follow all steps that I write in previous posts).
Let’s configure now, EvejobDJ project to use MySQL database.
First, needs to create “EvejobDJ” database schema using one of the administration tools, for example, described in Part #7 – just create it visually or issue following SQL command:
CREATE DATABASE EvejobDJ;
Open “settings.py” file in root folder of EvejobDJ project inside IntelliJ IDEA IDE and change database engine parameters to following:
DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. DATABASE_NAME = 'evejobdj' # Or path to database file if using sqlite3. DATABASE_USER = 'root' # Not used with sqlite3. DATABASE_PASSWORD = '' # Not used with sqlite3. DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
Now it is time to test connection and create default tables in database (for authentication, sessions etc):
cd C:\Evejob\EvejobDJ python manage.py syncdb
Hopefully you will get something like this in response:
As you see you will need to create default “superuser” (I just put “admin” as his username and “info at evejob.com” as his email) because Django authentication app enabled by default.
Now you can check in MySQL database administration tool that you really create tables (screenshot from free MySQL Administrator that you probably install with MySQL GUI Tools):
To use MySQL in Ruby On Rails (RoR) we need to install MySQL Adapter, for example:
ruby -S gem install mysql
But here we get into trouble – binaries of this gem (mysql) does not work correctly on Windows 64 bit and on project site there is no version for 64 bit OS… While on some “Unix” OS it is possible to fix this issue (for example build gem from source…), on Windows it is simply not possible… Ah, OK, OK… it is possible, but just too complex / time consuming – don’t want to spend time on this NOW and leave this time to focus on coding of Evejob… maybe somebody or author of gem will fix this issue later!
Note: other available adapters for MySQL from Ruby seems also have same problems… at least by now.
So let’s just temporary switch to PostgreSQL for Ruby on Rails version of Evejob.
To setup PostgreSQL for EvejobRoR development it takes just few minutes:
development: adapter: postgresql encoding: utf8 reconnect: false database: EvejobRoR_development pool: 5 username: postgres password: admin host: localhost test: adapter: postgresql encoding: utf8 reconnect: false database: EvejobRoR_test pool: 5 username: postgres password: admin host: localhost production: adapter: postgresql encoding: utf8 reconnect: false database: EvejobRoR_production pool: 5 username: postgres password: admin host: localhost
ruby -S gem install postgres-pr
This will install PostgreSQL adapter for Ruby (gem “postgres-pr” works on Windows x64)
Now you can try to execute database “migration” that will hopefully create some stuff in development database:
cd c:\Evejob\EvejobRoR c:\ruby\bin\rake db:migrate
It will be to simple if this will work for you from first attempt! On my machine, I was need to made small “hack” to made this work, i.e. to add following code into new_rails_defaults.rb file in “config\initializers” folder and run db:migrate again:
def PGconn.quote_ident(name) %("#{name}") end
(look for more info about this “hack” here)
If you complete everything, you can check that db:migrate actually create one table (”schema_migrations”) in your development database:
Let’s configure last version of Evejob which going to use MySQL database – EvejobGoG.
First, needs to create “evejobGoG_development”, “evejobGoG_test” and “evejobGoG_production” empty databases. You can do this trivial task using MySQL Administrator GUI Tool for example.
Next step is to let know Grails how to connect to just created MySQL databases. Because Grails runs on Groovy and Groovy itself runs in JVM we can use MySQL Connector/J from MySQL site: http://dev.mysql.com/downloads/connector/j/
Download “zip” file, unpack it to temporary folder and copy mysql-connector-java-x.x.xx-bin.jar file to the C:\evejob\EvejobGoG\lib folder.
Now open EvejobGoG project in IntelliJ IDEA and found DataSource.groovy file located in “EvejobGoG\Grails-app\conf” folder. Let’s change some settings in this file:
dataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" //"org.hsqldb.jdbcDriver" username = "root" password = "" dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" } hibernate { cache.use_second_level_cache=true cache.use_query_cache=true cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider' } // environment specific settings environments { development { dataSource { dbCreate = "update" // one of 'create', 'create-drop','update' url = "jdbc:mysql://localhost/evejobGoG_development" // "jdbc:hsqldb:mem:devDB" } } test { dataSource { dbCreate = "update" url = "jdbc:mysql://localhost/evejobGoG_test" // "jdbc:hsqldb:mem:testDb" } } production { dataSource { dbCreate = "update" url = "jdbc:mysql://localhost/evejobGoG_production" // "jdbc:hsqldb:file:prodDb;shutdown=true" } } }
Now Grails know how to connect to MySQL. Unfortunately, there is no build-in database migrations functionality in Grails framework yet, so our next step is to install some plug-ins that can help us to maintain database schema versions and migrations. While exists few of such plug-ins, I think most “featured” is LiquiBase (but also you can take a look into “Autobase” if you prefer to dial with DSL instead of XML… just take a note that this plugin works only with Grails, while LiquiBase can be used with Ant, Maven, Spring etc or even independently as database change management solution)
To install LiquiBase do following (you can read more regarding Grails integration with LiquidBase here):
cd C:\evejob\EvejobGoG grails install-plugin liquibase
Now check that you have following folder (that is empty by now):
C:\evejob\EvejobGoG\grails-app\migrations
If you don’t have folder yet, just create it before continue with migrations – this folder will be used to save migrations change log etc..
Now it’s time to generate change log and migrate database (that is empty for now) to initial version:
grails generate-changelog grails-app\migrations\changelog.xml grails migrate
After first command (”generate-changelog”) LiquiBase will create initial version of database change log and save it to the “C:\evejob\EvejobGoG\grails-app\migrations\changelog.xml” file. It will looks something like following:
Second command (”grails migrate”) will actually made migration in database. In our case because our domain is empty, LiquiBase will just create changes maintenance tables: “databasechangelog” and “databasechangeloglock”. You can check now this using MySQL Administrator:
If all steps above completed successfully you finish setup EvejobGoG Grails project to use MySQL Database and have ability to migrate databases!