3

I have an authentication backend based off a legacy database. When someone logs in using that database and there isn't a corresponding User record, I create one. What I'm wondering is if there is some way to alert the Django system to this fact, so that for example I can redirect the brand-new user to a different page.

The only thing I can think of is adding a flag to the users' profile record called something like is_new which is tested once and then set to False as soon as they're redirected.

Basically, I'm wondering if someone else has figured this out so I don't have to reinvent the wheel.

Jordan Reiter
  • 20,467
  • 11
  • 95
  • 161
  • I would say that your solution would work well. You could also use a trigger to set to the flag so that you can add other authentication backends in the future, but that may be overkill? – Jack M. May 11 '11 at 21:05

3 Answers3

1

There's two methods that I know of to determine if an object has been created:

1) When using get_or_create a tuple is returned of the form (obj, created) where created is a boolean indicating obviously enough whether the object was created or not

2) The post_save signal passes a created paramater, also a boolean, also indicating whether the object was created or not.

At the simplest level, you can use either of these two hooks to set a session var, that you can then check and redirect accordingly.

If you can get by with it, you could also directly redirect either after calling get_or_create or in the post_save signal.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • The problem with all three of these scenarios is none of them sends information back to the view that called authenticate. – Jordan Reiter May 13 '11 at 13:46
  • On their own, no. But, if you add a value to the session store, then you can access that value from the session in your view. – Chris Pratt May 13 '11 at 15:05
  • I don't think you have access to the session store in these cases, because you need request (I realize there are hacks around this, but still) which is only provided for the `login` signal, at which point it's too late. – Jordan Reiter May 13 '11 at 17:52
  • I'll add that, no, you can't redirect in the post_save signal. – Jordan Reiter May 13 '11 at 17:54
1

I found the easiest way to accomplish this is to do exactly as you've said. I had a similar requirement on one of my projects. We needed to show a "Don't forget to update your profile" message to any new member until they had visit their profile for the first time. The client wanted it quickly so we added a 'visited_profile' field to the User's profile and defaulted that to False.

We settled on this because it was super fast to implement, didn't require tinkering with the registration process, worked with existing users, and didn't require extra queries every page load (since the user and user profile is retrieved on every page already). Took us all of 10 minutes to add the field, run the South migration and put an if tag into the template.

Alex Jillard
  • 2,792
  • 2
  • 19
  • 20
-1

You can use a file-based cache to store the users that aren't yet saved to the database. When the user logs in for the second time, you can look in the cache, find the user object, and save it to the database for good.

Here's some info on django caching: http://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs

PS: don't use Memcached because it will delete all information in the situation of a computer crash or shut down.

systemizer
  • 355
  • 2
  • 6
  • The asker already has the code to create the new user in the DB and want's to know if django used an existing user or created a new one. It seems a lot easier to add an extra field or use post_save signals. – acidjunk Sep 22 '16 at 08:56