Fernando Correia’s Weblog

October 26, 2008

Exploring Pinax – Part 4

Filed under: Software Development — Fernando Correia @ 8:49 pm
Tags: ,

This is the fourth of a series of articles about my experience learning Pinax. In the previous articles I created a new option in the menu for a paste bin application and linked it to a very basic view.

My next step is to create a form for pasting text. I plan to do that using a form based on a model object. So the first thing I did was to write that model.

$ gedit apps/oxybeles/models.py
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext_lazy as _
from uuid import uuid4

class PastedItem(models.Model):
    """
    An item that was pasted.
    """

    uuid = models.CharField(_('identifier'), max_length=36, unique=True)
    text = models.TextField(_('text'))
    in_response_to = models.ForeignKey('self', related_name='responses',
        blank=True, null=True, verbose_name=_('in response to'))
    user = models.ForeignKey(User, related_name="pasted_items",
        verbose_name=_('user'))
    pasted_at = models.DateTimeField(_('pasted at'), auto_now_add=True)

    def __unicode__(self):
        return self.uuid

    def save(self):
        if not self.uuid:
            self.uuid = str(uuid4())  # random so it can't be easily guessed
        super(PastedItem, self).save()

The uuid will be used later to refer to an object in a URL. It will be a random unique identifier. The text field is what the user pasted, and is the main content of this object.

I plan to allow responses to pasted items, so that two users can share different versions of the same text. The in_response_to field will be used for that.

Since now the application has a model object, I must tell Django about it:

$ gedit settings.py

Insert the application name inside the INSTALLED_APPS list:

INSTALLED_APPS = (
...
    'oxybeles',
...
)

Then I was ready to review if the table would be created correctly:

$ python manage.py sqlall oxybeles
BEGIN;
CREATE TABLE "oxybeles_pasteditem" (
    "id" integer NOT NULL PRIMARY KEY,
    "uuid" varchar(36) NOT NULL UNIQUE,
    "text" text NOT NULL,
    "in_response_to_id" integer NULL,
    "user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
    "pasted_at" datetime NOT NULL
)
;
CREATE INDEX "oxybeles_pasteditem_in_response_to_id"
ON "oxybeles_pasteditem" ("in_response_to_id");
CREATE INDEX "oxybeles_pasteditem_user_id"
ON "oxybeles_pasteditem" ("user_id");
COMMIT;

Finally, I updated the database structure:

$ python manage.py syncdb

To test this model, I defined an administrative interface:

$ gedit apps/oxybeles/admin.py
from django.contrib import admin
from oxybeles.models import PastedItem

class PastedItemAdmin(admin.ModelAdmin):
    list_display = ('uuid', 'user', 'pasted_at',)
    fields = ('text', 'in_response_to', 'user',)

admin.site.register(PastedItem, PastedItemAdmin)

Starting the server again and browsing to http://127.0.0.1:8000/admin/oxybeles/pasteditem/, I was able to create a few pasted items to verify that all is working:

In the next step I will create the user interface to paste new items and to view stored items.

The source code is hosted on GitHub.

About these ads

2 Comments »

  1. this doesn’t really have anything to do with pinax. everything you are writing about is more clearly discussed in django’s docs.
    it would be more interesting if you actually wrote about pinax as the titles suggest.

    Comment by fx — October 27, 2008 @ 3:16 pm

  2. fx: Thank you for your feedback. You’re right, the start of this application is just basic Django stuff. I hope to get soon to Pinax features like notification, messaging, tagging, gravatar.

    Comment by fernandoacorreia — October 27, 2008 @ 7:59 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: