Words Are Poisoned Darts of Pleasure

Overcoming limitations in Django: using generic foreign keys

November 4, 2009 · Leave a Comment

Django’s great. It lets you prototype a web app extremely quickly, there’s good design philosophies that rub off onto your app, there’s great documentation… but there are also quite a few limitations I came across when trying to make my app. Thankfully, like I said, there’s great documentation, which includes ways to get around these limitations. Here are the obstacles I came across when building my course management app, Whiteboard, and how I got around each of them.

Dealing with foreign keys to abstract model classes

I had an UploadedItem abstract model class and I wanted to let people comment on UploadedItems. In an ideal world, I would be able to write the Comment model class like this:

class Comment(models.Model):
    comment = models.TextField()
    uploader = models.ForeignKey(User)
    upload_date = models.DateTimeField("Date uploaded", editable=True)
    uploaded_item = models.ForeignKey(UploadedItem)

But then you’d get an error saying that you can’t specify an abstract class as a foreign key, which makes sense since Django doesn’t actually implement the abstract model class in the database.

Solution:

The contenttypes framework! More specifically, generic relations. In the end comment looks like this:

class Comment(models.Model):
    comment = models.TextField()
    uploader = models.ForeignKey(User)
    upload_date = models.DateTimeField("Date uploaded", editable=True)

    # Following fields are required for using GenericForeignKey
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    uploaded_item = generic.GenericForeignKey()

Also, to get the reverse relation from my UploadedItem instances such as Announcement, I had to include a comments GenericRelation field in each of the UploadedItem instances where I wanted to allow comments for.

class Announcement(UploadedItem):
    """Announcement"""
    announcement = models.TextField()
    comments = generic.GenericRelation(Comment)

So now, to get all the comments for an announcement, all I would have to do is:

>>> announcement.comments.all()

It might not be the simplest solution, but it really is simple enough, and after I followed instructions from the documentation, it really just worked. Pity I couldn’t have it the “ideal” way, but then again using the contenttypes framework made the final implementation simple enough.

(Next post on making models and forms dynamic)

→ Leave a CommentCategories: Geek · Programming · Uncategorized
Tagged: , ,

A Special Watch

July 6, 2009 · Leave a Comment

I came across this watch online today. I like how wearing it will remind you to live life to your fullest every time you check what time it is.

mrjones_watch

→ Leave a CommentCategories: Uncategorized

The Search for Ideas

July 5, 2009 · Leave a Comment

I’ve been working on a project of late, but sometimes I’m wondering how likely that idea and project will become worthy of starting a company with. After reading Paul Graham’s article on Ideas for Startups, I realize the search for ideas has to be something constant–I have to be constantly asking questions of things in life, finding problems and inefficiencies–at least until I find something that I think is viable to work with in the longer term.

I also just came across this article on The New York Times about a start-up that rents college textbooks. It’s incredible how such an obvious inefficiency that’s plain for all to see can be the spark for such a successful start-up. We’re often blind to such glaring problems. Don’t get me wrong, we surely notice the inefficiency, just that we try to ignore it, try to get around it and get on with our lives because it would be painful to dwell on every single problem like that since there are an uncountable number out there.

In a way, it’s harder to see problems than their solutions. Most people prefer to remain in denial about problems. It’s obvious why: problems are irritating. They’re problems! Imagine if people in 1700 saw their lives the way we’d see them. It would have been unbearable. This denial is such a powerful force that, even when presented with possible solutions, people often prefer to believe they wouldn’t work.

- Paul Graham

I’ll be looking for these problems now, and when I find them, asking the right questions about why they are problems, and asking the right questions on how to solve them. Hopefully this will get me started on something worthwhile.

→ Leave a CommentCategories: Career

No To Rape

July 4, 2009 · Leave a Comment

In Singapore, if a husband rapes his wife, the fact that it’s in marriage exempts him from being charged for rape by the law. I feel that this exemption is unacceptable as it protects rapists and it leaves wives completely unable to press criminal charges despite getting RAPED by another man, even if it is their husbands. This law is archaic and dates back to colonial times, a time where most didn’t have the privilege of acknowledging gender imbalances as we see it in the world today.

Please support this petition to repeal the exemption of marital rape from being charged as rape. Also, if you feel as strongly about this injustice as I do, please invite your friends to sign the petition as well.

No To Rape

→ Leave a CommentCategories: Singapore · Uncategorized

Death by Facebook and PHP

July 2, 2009 · 1 Comment

It’s been 6 weeks since I started the FaceTrust project. I’ve been building the front end of the Facebook Application that implements the algorithm behind FaceTrust (details for another entry) with PHP, HTML, CSS, FBJS (Facebook’s proprietary JavaScript).

I remember that at the start, it was an arduous uphill climb learning the Facebook API. In contrast, learning the basics of PHP was a piece of cake. PHP documentation is great, and coming from Java/Python, all the syntax and data structure differences that I had to learn were not too difficult. The Facebook API, on the other hand, had some documentation, but finding the help I needed through searching the developer forums and wiki was nothing close to simple. The Facebook wiki does not even tell you what are the exact names of the methods you call in PHP; thank god I found the API tool tester early on and figured out how to use it. I remember spending an entire day just trying to get the CSS to work, and probably days figuring out FBJS vs JavaScript. The general sentiment was that I missed plain ol’ web development on your own server, without having to cater to someone else’s API.

So now I trudge on with the Facebook API, but I’ve developed a good grasp of how to use the API client, how to integrate FBJS, FBML etc. PHP seemed too easy to be true and indeed it was–my PHP code has started to stink pretty badly. Gone were the days of beautiful Python with one liners that in Java would be 30 liners. Gone was the clear separation of business logic and presentation (goodbye MVC). I’ve tried my best to figure out how to keep the code clean, but I’m having trouble. Also, I feel like implementing a templating engine like Smarty now is a little late and might mess things up right before launch.

I guess I still don’t have a clear idea of how to get the logic part out of my actual served PHP pages. Now it calls functions that render different parts, but why do I feel like these functions that just return HTML strings are not exactly the best way to do things sometimes. Why do I feel like it’s still a mess? Maybe I should’ve stuck to Smarty earlier, or built classes (or used Django AHEM). But the PHP works, it’s not beautifully readable or maintainable, but it works for now. I just don’t think I could write PHP for my career.

Well, there’s 4 more weeks of Facebook and PHP. Note to self: use Python for projects, or at least use Smarty, to ensure you’re drowning in a heap of code gunk at the end of the day.

→ 1 CommentCategories: Career · Programming · School

Career Goal

June 29, 2009 · Leave a Comment

I want to make technology easier for everyone to improve their lives with.

→ Leave a CommentCategories: Career · Geek · Programming

Usability and Beauty in Web Design

June 25, 2009 · Leave a Comment

I’ve been reading a ton of Jakob Nielson’s Alertbox lately, and I’ve been wondering where do aesthetics fit into web design if usability is the most important aspect. Usability makes for users that don’t get angry at frustrated at not being able to find something on your site. In business terms, it creates clicks, people like things, people find things easily, people buy things. It’s all great. So where does beauty fit in then?

I think the importance of beauty in web design is the unquantifiable emotional impact it has on people. If I see a great looking site, let’s say Mint or Twitter, and I think whoa the guys who made this put some serious effort into their work, it makes you associate the site, the company with quality work. On an even less practical level, seeing something beautiful just makes people happy. A friend once told me that the most often overlooked positive externality is beauty. I’ll admit I’m a sucker for great looking things, be it buildings, gadgets or websites.

My point is: usability is still the primary concern in web design, but to make something that’s truly top notch in terms of design, some level of beauty is required on top of solid usability.

→ Leave a CommentCategories: Uncategorized

Where do you want to live when you grow up?

May 24, 2009 · 1 Comment

I remember visiting New York City in the summer of 2007 with my family to help my sister move here for the first time. I fell in love with the place – it was bustling with excitement and there was never going to be a day without a thing to do or a place to go. But now I’m here for a short while, and I begin to feel like this is not the environment I want to live in. I don’t like how crowded, how dirty it is, how you always feel like you’re going to knock into someone, how nature is pretty much Central Park…

Don’t get me wrong, I love the museums, I love the park, and yes I think I still love the city, but it’s just not ideal for spending my life in. I think it might be fun for a month or even a year, but after living in a nice apartment off Duke’s campus in Durham over the summer, I’ve come to appreciate having lots of space. Not just in my house, but also around where I live, so I can drive out somewhere for a more rural getaway, or some nice outdoor activities, or the city, when I feel like seeing it.

I’m not sure where I want to live, or even exactly what kind of place I’d like to live in after I graduate, but here’s a little list of where and why/why not:

  • Singapore: Really clean, cheap food, great public transport, parents, friends/ Just too damn hot for me
  • New York City (Manhattan): Great fun, always a million things to do, probably lots of friends around/ Too crowded, too dirty, too difficult to get away, way too expensive
  • San Francisco: Beautiful weather, beautiful city, not too crowded, silicon valley nearby, nice culture/ Expensive, I don’t know this place well enough to criticize…

Then again, I might not even want to live in a big city… We’ll wait and see I guess. At the rate my living preferences have been changing, I don’t think there’s anyway to tell where I’d want to be until I find a more compelling reason to be somewhere on top of the place’s physical and cultural environment.

    → 1 CommentCategories: Life

    Make Stuff

    May 24, 2009 · Leave a Comment

    I was wondering why I love programming so much and I realized that it’s really not the algorithms or even the problem solving, which seems to be a huge draw to most people, but because it gives me the power to make stuff. It makes me wonder back to when I was a kid playing with LEGO. Back then, I just built what ever I felt like building, without worrying too much about whether it made sense. I just created what I wanted so that I could afterward play with it (I guess this means pretend the little LEGO characters and structures were interacting, back in the day.) I seem to have lost that mindset where I could just do and not be afraid to not know how to do something along the way, because it sure feels that way with certain recent projects.

    Anyhow, I still love making stuff, albeit more useful things this time, but I think the reasons behind it are the same-a love for a blend of creativity and technical skill. That blend seems to be what most creative work is about-painting, architecture, programming-although they all have different blends of creativity and technical skill involved. Creating something I can call mine that I can share with the world-that seems to be what I find most meaningful, what makes me the happiest.

    → Leave a CommentCategories: Career · Geek · Hobbies · Programming

    Start up

    April 29, 2009 · Leave a Comment

    I’ve been reading Paul Graham’s essays lately and I can’t help but feel that as a programmer, the best possible thing I can do for myself is to start a company. Why? Because it can earn you quite a bit of money, doing something you really really love. I’ve been drawn in to software engineering/programming/hacking because there’s something wonderful about being to make something that’s all yours. It’s the thrill of making something – combining creativity and leveraging skills and technology, all to improve something in this world, be it making people’s lives more convenient or more enjoyable, that makes me so sure that this is what I want to do with my life. Starting a company of my own would be ideal because it really puts me out there, testing my creativity, resourcefulness and ability in all sorts of ways since I have such a direct effect on whether I fail or succeed. I would say diving into computer science was the last best decision I made, I’m wondering if starting my own company that leverages this skill will be my next.

    → Leave a CommentCategories: Career · Hobbies · Idea · Programming