Wednesday, October 22, 2008

Using Gmail with Django

(permalink)
It's easy to set up django to send emails through Gmail. Just use the following settings:


DEFAULT_FROM_EMAIL = 'user@gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'user@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_PORT = 25


Gmail supports other ports, which you can use as well. This is important if you're running thee server on your local machine, and your ISP blocks port 25.

Labels: ,

Saturday, May 17, 2008

Extending the user model: profiles in Django

(permalink)
I think the documentation about using profiles & the get_profile() function isn't as clear and/or elegant as it could be. The problem with django's recommended implementation of profiles is that when a user is created, a user's profile isn't automatically created - it's not part of the same model. Managing the two can be tricky - if you mess up, you're bound to get ObjectDoesNotExist errors.

My solution was to never use get_profiles() directly - but instead add a helper method where any ObjectDoesNotExist errors are caught and resolved:


First, create a profile class in models.py:
from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    karma = models.IntegerField(default=1, core=True)
    url = models.URLField(default="", core=True)
    user = models.ForeignKey(User, unique=True)

Note, if you want to be able to create profiles using the Django admin interface, change the last line to the following:
user = models.ForeignKey(User, unique=True, edit_inline=models.TABULAR, num_in_admin=1,min_num_in_admin=1, max_num_in_admin=1,num_extra_on_change=0)


Add this line to settings.py:
AUTH_PROFILE_MODULE = 'core.UserProfile'

(change your line to 'appname.ModelName' - note, do NOT include your project name or anything else. Just your application name and model name (the model name is UserProfile, unless you change it).

Finally, add to views.py:
from django.contrib.auth.models import User
from project.core.models import UserProfile
from django.core.exceptions import ObjectDoesNotExist

def get_or_create_profile(user):
    try:
        profile = user.get_profile()
        except ObjectDoesNotExist:
        #create profile - CUSTOMIZE THIS LINE TO OYUR MODEL:
        profile = UserProfile(karma='1', url='http://example.org', user=user)
        profile.save()
    return profile


Now, in views.py, to access a user's profile, you only need two lines:
user = User.objects.get(pk = user_id)
user.userprofile = get_or_create_profile(user)


And, say if you wanted to change a value:
user.userprofile.karma += 1
user.userprofile.save()


Easy, eh?

Labels: , ,

Tuesday, May 13, 2008

One concrete tip for the self-employed programmer (that procrastinates)

(permalink)
I just read an article about procrastination that I found interesting (mostly because I agree with it). Procrastination is great - as I explained in the comments:
... procrastination paid my way through school. When you don't start studying for tests until a few hours before, you suddenly find yourself with a lot of time on your hands.


Procrastination teaches you how to work efficiently under pressure - a very important skill to have. The experienced procrastinator becomes a pro at putting out fires, and ironically, quickly getting things done. So why do procrastinators fail at life? Partially because there isn't enough that absolutely needs to get done. Procrastinators need pressure.


So what's a procrastinator to do? Start fires.


In software development this translates to setting artificial deadlines. Make promises - and judge your performance by whether you keep them. Here are some examples:

  • Promise your mom that you'll blog at least once per week (I do).
  • Promise a new feature on your website by a specified date.
  • Make promises on your blog, to whomever will listen (if you don't keep your promise, publicly apologize too).
  • Promise your clients you'll have respond to their emails within 12 hours.


And here's my favorite example: find someone important to you and make them your boss (I've chosen my girlfriend). At the beginning of every week spend an hour with that person and explain to them what you're going to do in the next week. Make promises. Then, check in with them throughout the week, and at the end of the week, conduct a "performance review." It helps if you can create some sort of incentive to meet your targets (you can be creative here... ).

Labels: , ,

Thursday, February 21, 2008

Save 6-7 GB on Leopard

(permalink)
If you have a MacBook Air, this tip is especially helpful, given the tiny size of your hard drive.

Here are some quick steps you can follow to save 6-7 GB:

1. Open up terminal and type "rm -r /Library/Printers/*" to delete 3GB worth of printer drivers. Then use the Leopard install CD to reinstall drivers for the 1-2 printers you actually own.

2. Disable safe sleep and remove the sleeimage. To do this, open your terminal and type "sudo pmset -a hibernatemode 0; sudo rm /var/vm/sleepimage"

Note: After you do this your mac will take about 3 seconds to go to sleep, as opposed to ~15. However, if your battery is exhausted while the mac is sleeping, you will loose your session data.

To re-enable safe sleep, open your terminal and type "sudo pmset -a hibernatemode 0"

3. Delete your iWork '08 trial (if you're not using it):

Drag the iWork folder (in Applications) to the trash can.

You'll also want to delete:
library > receipts > iwork08trial.pkg
library > preferences > com.apple.iwork08.plist
youraccount > library > preferences > com.apple.iWork.Keynote.plist
youraccount > library > preferences > com.apple.iWork.Numbers.plist
youraccount > library > preferences > com.apple.iWork.Pages.plist

Labels: , , , ,

Friday, January 25, 2008

Getting music off your iPod without third-party apps

(permalink)
I recently needed to get music off my iPod - something that should be easy to do, but isn't. Most How-to's suggest that you download a third-party, like iLinkPod. There's an easier way to do this on your Mac, no third-party app required.

First, plug in your iPod and make sure "Enable disk use" is checked under your iPod preferences in iTunes. If your iPod shows up on your desktop, then you're fine. Next, open up Terminal, and cd into your iPod (cd /Volumes/Ipod_name_here). You should see a directory called "iPod_Control." All your music is stored within that directory. cp to your heart's content.



If you're not comfortable with the terminal, you can easily view all your music in finder. Simply open the terminal and execute these two commands:
# defaults write com.apple.finder AppleShowAllFiles TRUE
# killall Finder


Labels: , , ,