Archive for the ‘Geeky’ Category.

Tubecaster 3 UI Mockup

Here’s a mockup of the main window for the upcoming Tubecaster 3.  The release will include support for multiple downloads at once, playlist download support and built-in media conversion tools.  Stay tuned!

Tubecaster 3 main window

Pre-populate Django ModelForm with Specific Queryset

I just had a situation where I was trying to filter the queryset for a ModelMultipleChoiceField based on the currently logged-on user.  I was going crazy trawling through the Django docs and eventually Google.  It seemed like something which should be so simple, but there was no obvious way to do it.  Eventually I found the answer, and it IS simple!  As an example, let’s say you have the following two models as part of a simple photo gallery app:

?View Code PYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Photo(Model):
    name = CharField(max_length=100)
    caption = CharField(max_length = 150)
    user = ForeignKey(User)
    upload_timestamp = DateTimeField(auto_now_add=True)
    image = ImageField(upload_to="user_images/%Y/%m/%d/")
    thumbnail = ImageField(upload_to="user_images/%Y/%m/%d")
    def __unicode__(self):
        return self.name
 
class Album(Model):
    name = CharField(max_length=100)
    caption = CharField(max_length = 150)
    user = ForeignKey(User)
    photos = ManyToManyField(Photo, related_name="albums", blank=True)
    creation_timestamp = DateTimeField(auto_now_add=True)
    cover_photo = ForeignKey(Photo, related_name="cover_photos")
    def __unicode__(self):
        return self.name

We then define a ModelForm based on the Album model, which allows users to create albums with photos they’ve previously uploaded (pretend we’ve already made that possible). We only expose the “name”, “caption” and “photos” fields because we’ll fill in the others automatically as part of our view:

?View Code PYTHON
1
2
3
4
class AlbumCreationForm(ModelForm):
    class Meta:
        model = Album
        fields = ("name", "caption", "photos")

Now here’s the real magic. Ordinarily, when first showing the form (pre-POST) we would create it like this and pass it to the template:

?View Code PYTHON
form = AlbumCreationForm()

The problem here is that by default we’ll get all photo objects, i.e. the result of “Photo.objects.all()”. That’s a problem because in this case we just want to list the photos belonging to the current user. To do this, just add the following line:

?View Code PYTHON
form.fields["photos"].queryset = Photo.objects.filter(user=request.user)

It turns out that form fields can be accessed as a dictionary attached to the form instance, and that if the field is model-related, like “photos” in the example, you can update its queryset dynamically.

Here’s a partial view which uses the last two code samples, to provide some context:

?View Code PYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
def create_album(request):
    if request.method == "POST":
        # Process form.
    else:
        form = AlbumCreationForm()
        # Get just the photos belonging to this user.
        form.fields["photos"].queryset = Photo.objects.filter(user=request.user)
 
    template_vars = RequestContext(request, {
        "form": form,
        "user": request.user
    })
    return render_to_response("create_album.html", template_vars)

That’s it! Adding the one extra line to the view gives us the filtering we need.

Server Fault Private Beta

Following on from the wildly successful Stack Overflow programming questions and answers website, Jeff Atwood and Joel Spolsky have launched Server Fault, a questions and answers site following the same format but now for “system administrators and IT professionals” (currently in Private Beta, click here for sign-up instructions).

stackoverflow_logo

serverfault_logo

I’ve been using Stack Overflow for about 5 months and I absolutely love it. It has become the definitive, and I dare say de facto standard, questions and answers one-stop-shop for programmers. You can post a question for just about any programming topic and receive an answer within minutes, sometimes even seconds. This removes an old hassle with posting questions on Web 1.0-style forums where you had to wait impractically long lengths of time to receive answers.

These sites don’t just follow any standard Q&A format either.  Just take a look at the diagram on the about page of either site and you’ll see that they’re a deliciously freakish Wiki/Blog/Forum blend.

One of the things I love about boths sites is their innovative karma and badge system, where users are rewarded by the community for making valuable contributions.  This gives users a reputation score which appears next to their name wherever it appears on the site.  This can be used as an assessment of someone’s overall standing in the community.  Badges are awarded for special achievements like providing an especially highly-rated answer or for being recognised as an expert in a particular technology.

Of course possibly the most important feature of boths sites is that you don’t need to be logged in to ask or answer questions.  This removes yet another annoying hurdle for the casual help-seeker.

I encourage everyone in IT, be it programming or otherwise, to sign up to at least one of these – you won’t look back.

-Wayne

Tubecaster.org live

Tubecaster.org homepage

.

I’ve just launched a new website for Tubecaster.  The new site was created with Django, with which I’m extremely impressed.

The new site also contains bug reporting and feature request links.

-Wayne

Balsamiq Mockups, donated license

The wonderful folks at Balsamiq have graciously donated a license for their Mockups user interface design software to the Tubecaster project to help with future improvements.  I just want to say a huge thank you to Mariah and the Balsamiq team for this, and to encourage everyone to check out this fantastic piece of software.

Tubecaster Balsamiq interface

Tubecaster Balsamiq interface