title: Django: signal when user logs in?
body: <p>In my Django app, I need to start running a few periodic background jobs when a user logs in and stop running them when the user logs out, so I am looking for an elegant way to</p>
  
  <ol>
  <li>get notified of a user login/logout</li>
  <li>query user login status</li>
  </ol>
  
  <p>From my perspective, the ideal solution would be</p>
  
  <ol>
  <li>a signal sent by each <code>django.contrib.auth.views.login</code> and <code>... views.logout</code></li>
  <li>a method <code>django.contrib.auth.models.User.is_logged_in()</code>, analogous to <code>... User.is_active()</code> or <code>... User.is_authenticated()</code></li>
  </ol>
  
  <p>Django 1.1.1 does not have that and I am reluctant to patch the source and add it (not sure how to do that, anyway).</p>
  
  <p>As a temporary solution, I have added an <code>is_logged_in</code> boolean field to the UserProfile model which is cleared by default, is set the first time the user hits the landing page (defined by <code>LOGIN_REDIRECT_URL = '/'</code>) and is queried in subsequent requests. I added it to UserProfile, so I don't have to derive from and customize the builtin User model for that purpose only.</p>
  
  <p>I don't like this solution. If the user explicitely clicks the logout button, I can clear the flag, but most of the time, users just leave the page or close the browser; clearing the flag in these cases does not seem straight forward to me. Besides (that's rather data model clarity nitpicking, though), <code>is_logged_in</code> does not belong in the UserProfile, but in the User model.</p>
  
  <p>Can anyone think of alternate approaches ?</p>