Evejob Projects will support Internationalization (I18N) and allow users to select site language in addition to auto detect browser features. Major requirement in I18N – support of right to left languages and page designs in addition to left to right (this will allow to made Hebrew sites powered by Evejob, for example).
There will be additional projects (EvejobDJG, EvejobAzNET, EvejobRoRG, EvejobGoGG) that will be specially designed to be hosted in various clouds like Google App Engine or Azure … work in progress under this post …
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!
Because I build Evejob multi-platform project with support of Cloud deployment scenarios (currently for Google App Engine and Microsoft Azure) and I am sure that best Web software must be ready to be deployed in Cloud(s), it was very interesting to read latest announcement from Amazon: “… AWS Software Development Kit (SDK) for .NET Now Available… “.
Sure it’s just a beginning for Amazon in development of real .NET SDK, but even with this version we can quickly build .NET applications that tap into AWS Cloud. Yes, even before developers have good libraries to build such applications, but now looks like we have first “standard” API from Amazon and this fact is very important!
For example, for Ruby there is no such official SDK (or Ruby gem), and developers can choice from few available gems (right_aws, amazon-ec2 or AWS:S3 to name just few). The same situation for Java - available a lot of “community” libraries, but no official SDK… (ok, ok, for Java we have at least official AWS Toolkit for Eclipse, but Toolkit it is not SDK!!!).
But why it is important to have official “language specific” SDK?
Well, first of all, now because Amazon have few BIG rivals, like Google App Engine (and we know that Google DO have official SDKs, more so, for both Python and Java!) and Microsoft Azure (sure Microsoft always have SDKs – it a BIG plus for Microsoft!).
Also it is important for developers, so they know that if they take some library and put this library as “base” for communications with a Cloud, they will not need to dial with changes in API in case of library author decide to drop development! It is important to have SDK that will be up to date with company services (Amazon in our case), just because we developers want to be SAFE! Sure it is good if there are a lot of open source libraries that EXTEND SDK some way, but it’s just “add-ons” and can’t replace real official SDK!
So it is really BIG day for Amazon and .NET developers – first official “language specific” SDK, and for .NET!!! What will be answer from Microsoft Azure that is still in “Beta”?
Note: in this post I mean “language specific” SDK, not just common SDK that list API for Web Services with samples how to use them for example, etc… Because most of they time developers dial with some specific language, it is important for services like AWS to provide language specific SDKs so developers can really quickly and “safely” create applications using such “language specific” SDKs! Hope you understand what I mean
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!
From use cases of Sprint #1 and from our general initial understanding of business domain (Job Board) we can grab following main domain entities:
It seems like it’s enough entities to start – I highlight them in bold, totally 7 entities seems a good number actually!
So let’s start actually coding of domain models Check next posts!