Tag: Python

The Pinax Tutorial #5: Adding a comment system

Posted by on December 21, 2009

In this part we will improve the Bookstore application: what I am going to show is how easy it is to add support for enabling comments for a book without writing from scratch another comment system (as you may already now, avoid rewriting things is the core philosophy of Pinax and Django).

Basically, you could add this feature by using one of these two approaches (there may be other ones, but these are the most common):

You will implement a commenting system in the application you are developing by using the django-threadedcomments system.

Setup

You need to make sure that, in your settings file, you have the django-threadedcomments system enabled:

PROJECT_ROOT/settings.py:

INSTALLED_APPS = (
...
'threadedcomments',
'threadedcomments_extras',
...
'bookstore',
)

if you didn’t customize things, you should have this two applications already enabled (not commented).

More…

The Pinax Tutorial #4: Using avatars, pagination and profiles

Posted by on December 19, 2009

In this part we will improve the Bookstore application and we will see how easy it is to add support for three core features that any web 2.0 sites is supposed to offer:

  • support for avatars
  • support for pagination
  • support for user profiles

Adding support for avatars

In Pinax you may use two kind of avatars for your users (in both case you may manage them from the Avatar page: http://localhost:8000/avatar/change/):

In the first case Pinax will render your avatar using the gravatar you have associated to your email from the Gravatar web service. In the latter Pinax will use a custom avatar you have uploaded to the Pinax web site.

For managing avatars in the Pinax application you are building, you will use the Avatar application (be sure you are importing it in your settings file, the default for this is true so you shouldn’t do anything there).

Adding support for avatars in your Pinax application is just as easy as to do the following two things in your templates:

  • import the avatar tags library {% load avatar_tags %}
  • use the {% avatar %} tag in your templates

For the Bookstore application it will be enough to use the avatar tag in this way: {% avatar book.adder 40 %}

Where the first parameter is the user for which you want to render the avatar, and the second parameter is the avatar size in pixels.

So you need to do this in your templates (books.html, book.html):

PROJECT_ROOT/bookstore/templates/bookstore/books.html template

{% extends "bookstore/base.html" %}
{% load i18n %}
{% load avatar_tags %}
...
        <!-- alternate -->
        {% if books %}
            <table class="bookstore">
                {% for book in books %}
                    <tr class="{% cycle odd,even %}">
                        <!-- meta -->
                        <td class="meta" >
                                <div class="avatar" >{% avatar book.adder 40 %}</div>
                            <div class="details">{% blocktrans %}added by{% endblocktrans %} <a href="{% url profile_detail book.adder.username %}">{{ book.adder }}</a></div>
...

More…

The Pinax Tutorial #3: Internationalization of the application

Posted by on December 18, 2009

(Note that, if you completed the other parts of this tutorial before the date of this post, you need to move the PROJECT_ROOT/templates/bookstore directory in PROJECT_ROOT/bookstore/templates: so now you will have a PROJECT_ROOT/bookstore/templates/bookstore directory. I did so for a better deployment experience, and doing so I am following the Django best practices. If you are reading the pdf version of the tutorial or the reST documentation, then this documentation is already updated with the templates directory in the right place).

In this part of the tutorial I will show how easy it is to enable your application for internationalization with Pinax.

Enabling internationalization for a Pinax application is just the same procedure for enabling it for a Django application.

You can find a complete discussion about Django’s internationalization mechanism here
Be sure to read this discussion before going on with this tutorial.

To enable internationalization for your application you will need to complete three steps:

  • use translation strings in Python code and templates
  • create language files
  • change settings

Let’s see how to apply them to the Bookstore application you are developing.

More…

Using MongoDb to store geographic data

Posted by on December 6, 2009

The NoSQL movement

In the last months there has been a plenty of activity in the non relational (NoSQL) database world.

NoSQL database tries to solve 3 main RDBMS problems:

Basically there are several different kinds of NoSQL database:

  • Key/Value (Scalaris, Tokio Cabinet, Voldemort): store data in key/value pairs: very efficient for performance and higly scalable, but difficult to query and to implement real world problems
  • Tabular (Cassandra, HBase, Hypertable, Google BigTable): store data in tabular structures, but columns may vary in time and each row may have only a subset of the columns
  • Document Oriented (CouchDb, MongoDb, Riak, Amazon SimpleDb): like Key/Value but they let you nest more values for a key. This is a nice paradigm for programmers as it becomes easy, specially with script languages (Python, Ruby, PHP…), to implement a one to one mapping relation between the code objects and the objects (documents) in the database
  • Graph (Neo4J, InfoGrid, AllegroGraph): stores objects and relationships in nodes and edges of a graph. For situations that fit this model, like hierarchical data, this solution can be much much faster than the other ones

MongoDb is a document oriented NoSQL database. With such a database it is very easy to map the programming objects (documents) we want to store to the database. JSON is a very viable standard to do this mapping, and so MongoDb does: it stores JSON documents in the database.

To makes performance better JSON is stored by MongoDb in a efficient binary format called BSON. BSON is a binary serialization of JSON-like documents and stands for Binary JSON.

For getting more information on the topic I highly recommend reading this interesting article: NoSQL Ecosystem

More…

The Pinax Tutorial #2: Developing the basic application and plugging it in Pinax

Posted by on November 29, 2009

In this part of the tutorial you are going to create the core of the bookstore application, with all the pages that gives access to the CRUD (Create, Read, Update, Delete) features. And you are goint to plugin this basic application into Pinax.

After finishing with this part you will have the core of the bookstore application working as desired. You will be able to:

  • see a list with all the books in the bookstore
  • add a new book
  • update and delete your books
  • see a list of all books added by you
  • see a list of all books added by a user

In the following parts you will add some more goodness like localization, pagination, avatars, profiles, voting, tagging, feeds, comments, notifications, and flags for contents, but basically this part is the most important one and it is the way you can create from scratch an application and make it enabled for Pinax.

Creating the application

Before doing anything, give execute permissions to the manage.py command:

chmod 777 manage.py

Now create the bookstore application:

./manage.py startapp bookstore

Now that you have created the application, it it time to add the models, installing it in the Pinax project and syncing the database.

More…

Geocoding with GeoPy

Posted by on October 14, 2009

There are now may geocoding web services out there, like Google geocoder, Yahoo geocoder, geocoder.us (only for US address), Microsoft MapPoint, GeoNames and MediaWiki.

Normally you may access this web services API directly with HTTP REST request, and get a response in common formats like xml, json, kml.
For example you may geocode with the Google geocoder an address like this one: “1071 5th Avenue, New York, NY” with a request like this one:

http://maps.google.com/maps/geo?q=1071+5th+Avenue,+New+York,+NY&output=json&oe=utf8&sensor=true_or_false=&key=your_apy_key

In this case we are querying the Google geocoder web service to get a response in json via the output parameter in the query string. This is the json response we get back from the web service:

More…

The Pinax Tutorial #1: Installing Pinax and making basic customisation

Posted by on October 10, 2009

I will assume you are installing Pinax v0.7 on a Ubuntu 9.04 box, but this procedure - with a few modifications, should work well on every Linux box. For Windows please refer to the Pinax official site or - rather I highly reccomend to use VirtualBox, and to create an Ubuntu 9.04 Virtual Machine, so you will be able to follow step by step this tutorial.
There are ready images like this one, to make things even easier.

As suggested from the official installation procedure, the release bundle has everything you need for running Pinax.
What is not included is:

Ubuntu 9.04 includes Python 2.6, PIL 1.1.6 and SQLite 3.6.10, so you have everything. If you are using another Ubuntu version or a different Linux distribution, please verify to have all of this software. You will install and use for this tutorial Pinax v0.7 (latest release at the date of this post).

More…

The Pinax Tutorial, Introduction

Posted by on October 3, 2009

In the last weeks I was studing Pinax , an open source platform for building Django applications. While I enjoyed a lot learning how to develop software with this framework, and I am going to happily use it for a series of projects, I found a bit difficult to get documentation about it, if not reading the source code and the (few - at this time) documentation on the project web site.

When I started, I decided to write a test application for understanding the Pinax philosophy before going for real development projects. I think it can be very useful for other developers new to Pinax to write down my experience. This is why I am going to write this tutorial, hoping it will be really useful for the Django/Pinax community.

While reading my posts, if you find errors or you have suggestions to give, please don’t hesitate to put some comment in my blog’s posts: I will try to update the tutorial, trying to make it a good resource for people willing to learn to develop with Pinax.

Intended audience

You may easily follow this tutorial if you have some Python and Django experience.
But even if you are new to Python and Django, you may reasonably follow this tutorial picking a bit of Python and Django confidence from the following resources (note, however, that you should have programming experience and you should not be new to web development).

More…

A day with GeoDjango

Posted by on April 1, 2009

This time i will introduce a really brilliant framework that every serious Web/GIS developers should be aware of: GeoDjango.

Django is a Python Web framework for agile developers. It implements best web frameworks practices like coding by convention, MVC, ORM, REST, URL dispatcher and so on. Django is for Python what Rails is for Ruby, Grails is for Java, and MonoRail (and now ASP.NET MVC) is for .NET.

GeoDjango is a Django application that is now included in the Django trunk with a lot of excellent stuff for developing GIS web application.

GeoDjango Building Blocks

GeoDjango installation is based on Python, Django and two kinds of components: a Spatial Database and Geospatial libraries

1) Spatial Database
A spatial database like PostGis (recommended), MySQL Spatial and Oracle Spatial (and since some day also SpatiaLite, that is an excellent option for development purposes)

More…

A day with TileCache: generating KML Super-Overlays

Posted by on August 6, 2008

My friend Diego Guidi is the smartest GIS/.NET developer I personally know here in Italy. He is the developer of NetTopologySuite, the port in the .NET world of the popular Java’s JTS Topology Suite from VIVID Solutions. I wanted, sooner or later, write some stuff here about WMS and TMS, and now I am very happy that Diego asked me to publish this brilliant article about this topic.

First of all, let me thanks Paolo for hosting this post! I hope that this article can be interesting and useful like other stuff that you can find here

Introduction

There are a lot of discussions out there about how to define Google Earth, Google Maps, and related apps… are they GIS? Viewers? Video games? Even a neologism was created: Neogeography. I think that all the folks out there have the same idea in mind: maybe Google don’t make the same business as ESRI, but Google Earth is cool, and it’s funny to play with it!

More…