Catching User's First Log in

1,440 views
Skip to first unread message

Wesley Childs

unread,
Apr 5, 2010, 3:50:26 PM4/5/10
to django...@googlegroups.com
Hi all,

I'm trying to catch the first time someone logs in so I can present them with some options. I was wondering whether there is something readily available in Django for this or whether I'll need to go down the route of having a boolean field on a user table that becomes un-checked after they log in?

Thanks in advance

Wes

Owen Nelson

unread,
Apr 5, 2010, 3:54:53 PM4/5/10
to django...@googlegroups.com
That would be the strategy I'd employ - however I'd recommend you not
add the field to the user itself, but rather its profile.
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

Dennis Kaarsemaker

unread,
Apr 5, 2010, 3:55:19 PM4/5/10
to django...@googlegroups.com

A userprofile with that boolean flag sounds like the best way forward.
--
Dennis K.

They've gone to plaid!

ShawnMilo

unread,
Apr 5, 2010, 4:02:22 PM4/5/10
to django...@googlegroups.com
How about the last_login and date_joined fields in the User model? They're both set to the current date/time when the user is created.

So, if the user's "first login" is when the account is created, you could check to see if they're less than a second apart. If the user is created automatically, then you can do the same check, and if they're still that close together then you know they've never logged in.

I don't think there could really be any problem with race conditions, because both dates are initialized at the same time, when the user is created, so a delta of, say, two seconds should be more than enough. However, it does feel a little bit dirty to me. Not as dirty as having a boolean in a join table that has to be checked on every log in for the rest of the life of the application just for a single use, though.

Shawn


Owen Nelson

unread,
Apr 5, 2010, 4:06:34 PM4/5/10
to django...@googlegroups.com
> it does feel a little bit dirty to me.
Me too, that way leads to deciding something is a witch just because it floats. Can't argue with a Bool, and also, it might eventually be nice to be able to flip the bit on a whim (say a user wanted that "out of the box" experience all over again).

Wesley Childs

unread,
Apr 5, 2010, 4:25:00 PM4/5/10
to django...@googlegroups.com
Thanks for the advice.

I'm going for boolean as I like the idea of flipping back on in the future for special events.

Thanks

Wes

On 5 April 2010 21:06, Owen Nelson <one...@gmail.com> wrote:
> it does feel a little bit dirty to me.
Me too, that way leads to deciding something is a witch just because it floats.  Can't argue with a Bool, and also, it might eventually be nice to be able to flip the bit on a whim (say a user wanted that "out of the box" experience all over again).

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Tim Shaffer

unread,
Apr 5, 2010, 6:45:21 PM4/5/10
to Django users
This might be slightly off topic, but why is the last_login set to the
current date when a user is created?

Wouldn't it make more sense for that date to be null until the user
actually logs in?

On Apr 5, 4:25 pm, Wesley Childs <childs.wes...@gmail.com> wrote:
> Thanks for the advice.
>
> I'm going for boolean as I like the idea of flipping back on in the future
> for special events.
>
> Thanks
>
> Wes
>

> On 5 April 2010 21:06, Owen Nelson <onel...@gmail.com> wrote:
>
> > > it does feel a little bit dirty to me.
> > Me too, that way leads to deciding something is a witch just because it
> > floats.  Can't argue with a Bool, and also, it might eventually be nice to
> > be able to flip the bit on a whim (say a user wanted that "out of the box"
> > experience all over again).
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > django-users...@googlegroups.com<django-users%2Bunsu...@googlegroups.com>

Owen Nelson

unread,
Apr 5, 2010, 7:23:53 PM4/5/10
to django...@googlegroups.com
I guess it depends on the implementation of the auth backend you're using.
Really though, it seems like using a bool flag involves less "magic", and is perhaps more descriptive than making assumptions based on timestamps.

if user.is_new: redirect(somewhere)
rather than...
if user.last_login is None or user.last_login == user.join_date: redirect(somewhere)

I'd value readability over reusing 2 timestamp columns in an effort to avoid adding a tinyint.

Owen Nelson

unread,
Apr 5, 2010, 7:28:03 PM4/5/10
to django...@googlegroups.com
Although, I guess you could argue that a proxy class with a @property method could achieve the same thing.

from django.contrib.auth.models import User as DjangoUser
class User(DjangoUser):
    class Meta:
        proxy = True
    @property
    def is_new(self):
        return None == self.last_login # or whatever you want
Reply all
Reply to author
Forward
0 new messages