Nov 21, 2011

Django: Instalation of Dev environment on a clear Lion Mac OS X

Like lots of Apple fans around the world I've finally decided to upgrade my OS to Lion release. I have bought a new laptop and it was the case. I've waited for a weekend to prepared myself for sex. And it was it. Now I want to share my experience with you guys. For you not to spend to much time on those issues, I've did.

Anyway what I'm usually using is a Fresh 2.7 Python, Django, PIL, virtualenv and many many minor tools which exceed scopes of this article. I also use Eclipse IDE for Python Dev. So all this I had to install and make spinning till monday.

If anybody tells me that Macs do not have good tools for this. He will not be right. Maybe there some itches and scratches of head about:
"Why is not those things working" or "Why do they work not as preferred". But there always is a good "Googled" decision to handle that.
Anyway lets go to installing stuff.

Short brief Plan of out work:
1. Install Apple Development SDK + Xcode (free from Apple App Store).
2. Install Python (from
3. Install Libraries needed for PIL (Libjpeg and Freetype support).
4. Install Django.

1. First of all! Mother of all Gods! We need Xcode and Mac Development tools provided from AppStore for Free. Let's download it and install it. If you have an apple store Account, of course. Actually here we do not need the whole bunch of stuff like Xcode or Dev tools except for GCC. And if you're advanced enough and have time to handle it... You should probably just install GCC, compiling it from sources for e.g. I'm not gonna spend time at those "low levels" and will simply install a set of packages that I may (or may not) need in future for dev.

After Xcode is Downloaded you should go to Applications of Lunchpad and hit Xcode icon. (They are actually only downloaded not installed yet. What a long time period,  waiting for 1,6 Gigs App download form App Store.) Hit 'next', 'yes' and so on... Agree to everything and probably close some apps like for e.g. iTunes somewhat weirdly but true. Anyway lets continue to our next and main part. Those actions should already give us required GCC compiler for all our needs.

2. Next step is installing Python. We should get it from here, for e.g.: I've chosen Python 2.7.2 Mac OS X 64-bit/32-bit x86-64/i386 Installer (for Mac OS X 10.6 and 10.7). It gave latest stable 2.7.2 Python with idle for about 17 Mb more downloading.
DMG file has a simple enough installer that will ask you for root password and do all the stuff you need for you...

3. Support of jpeg, freetype and littlecms is missing, by default. You need to compile PIL with support of at least jpeg and freetype. In order to make ImageField in your django project support jpeg formats and Have ability to handle Captcha fields or different text with picture manipulations.
You may need to switch to 'sh' interpreter from standart 'bash' typing sh in command line. It handles ./congure and make install commends more gracefully (IMHO). And you may need 'sudo' to gain permissions to some system stuff here.
Will try to be brief here:
  • Install JPEG support libraries:
    • Download JPEG library source curl -O
    • Extract tar -xvzf jpegsrc.v8c.tar.gz
    • Switch to jpeg directory cd jpeg-8c
    • Run ./confgure (I only made it work at 'sh' command prompt someway)
    • Run make
    • Run sudo make install
  •  Install Freetype support:
    • Download Freetype library source curl -O
    • Extract tar -xvzf freetype-2.4.5.tar.gz
    • Enter to freetype directory cd freetype-2.4.5
    • Run ./confgure (I only made it work at 'sh' command prompt someway)
    • Run make
    • Run sudo make install
  • Actually install PIL:
    • Type: sudo easy_install PIL
    • End!
Now PIL Should compile with support of FreeType Fonts and LibJpeg. I also used easy_install pip first. Because I simply like PIP more and it's much less to type pip install. But it's a matter of taste... Anyway we must see something like this after PIL installs:

This will indicate successful installation of PIL and components.

4. Installing Django is easy now. Jut do it whatever way you prefer. Like sudo pip install django or
sudo easy_install django or even sudo pip install django==1.1, if you prefer oldies. :)

I prefer to use virtualenv 'your virtual env name here' --no-site-packages for every app of mine... So I'm not even installing Django to main system path as well... Btw if you've used virtualenv with you projects, like I did, prepare yourself.  They need to be recreated. So be sure to save them with pip freeze. Maybe they will work if you pu them back to the same system path as they where... But there might be daemons here :).

I'll try to cover installation of MySQL environment on a "clean" Lion install in my next articles.

Anyway if you've found this article useful or think that I'm not right somewhere or even something less important... Feel free to drop me a comment here or contact me any available way.  Thanks for reading so far and hope this will shorten your time while installing some things.

Nov 16, 2011

Python: Debugging/Developing SMTP mail things

You often have 'situations' when you developing Contact forms, or any other forms which have to send emails with python. You have to install or have working SMTP server on your Dev machine like say 'monstrous' Sendmail or something like it. It can cause errors in Django/Python like [Errno 61] Connection refused:

[Errno 61] Connection refused

But python has it handled for you. Python's standard distribution has a test SMTP server that you can test sending mail with. It actually does not sends anything. But you can examine outgoing email message like you've actually send it.
Type something like:

python -m smtpd -n -c DebuggingServer localhost:1025

It will start a command line server on this console and it will print everything your server should send through SMTP.
Also make sure to modify mail sending code to use your non standart port number like so:

server = smtplib.SMTP(SERVER, 1025)
server.sendmail(FROM, TO, message)

Or with 'sudo' (if you have admin permissions) you can start a test SMTP server like you are actually using sendmail simply changing port number of your server.  Or just change 1025 to 25 (SMTP default port) if you are working under root permissions user. Like so:

sudo python -m smtpd -n -c DebuggingServer localhost:25

Found interesting? Have a better/simpler way or I am wrong?
Please drom a comment here. 

Nov 10, 2011

Django: Adding news archive. Querying models with date and time filtering.

Today I had some practice with filtering Django models by date.
Task was to archive old entries of the news. I've made 2 separate views for those. I was displaying actually news and another was ment to display archived items. Quite common task I guess. Also those 2 views are under wired buggy CMS, but we are not talking about that now.
Will save some examples for future of mine/someones usage.

Time. Subtracting a year from today with Python dateutil:

from datetime import date, timedelta - timedelta(days=days_to_subtract)

This example returns 'd' that has less days than specified in 'days_to_subtract'. timedelta function also can be fed with 'months', 'years' and so on. Becomes handy during time calculations. If you change this minus into plus you can calculate future time.

Filtering Querysets. Filtering by date ranges.

samples = Sample.objects.filter(, 1, 1),
                      , 1, 31)

samples = Sample.objects.filter(, 1, 1),
                      , 1, 31)

Django queryset filters is quite interesting things to discover. They also can cover almost any simple database query. In this example we are filtering model objects that are between dates range. This filters go through model's date fields and look for items in this date range. Only difference here that firs example includes 1-31 days and second includes 2-30 days range (excluding specified days).

  • '__gte' means '>='
  • '__lte' means '<='
  • '__gt' means '>'
  • '__lt' means '<'
Easy to remember right?
Found interesting, Helped,  Know a better way, I'm not right somewhere. Please drop me a comment below.