title: Djangae AppEngine misbehaves with IAP
body: <p>Code that I have used to handle user login and admin login:</p>
  
  <pre><code>    # if not logged in, divert to login page
      if users.get_current_user() is None:
          return redirect('whoami')
      # if logged in, Check if user is an admin
      elif not users.is_current_user_admin():
          return render(request, 'template.html', {'heading': 'Bad Request (400)',
                                                   'message': ['You are not logged in as administrator'],
                                                   'user_email': users.get_current_user().email()})
  </code></pre>
  
  <p>Everything works fine when I use the <code>Djangae appengine with Datastore (Django with datastore support)</code> back-end but when I enable the <code>Google Identity Aware Protocol (Google IAP)</code> everything starts failing. When I checked the logs, It says that there was an IntegrityError in <code>djangae_gaedatastoreuser</code> on the field email_lower</p>
  
  <pre><code>IntegrityError: Unique constraint violation for kind djangae_gaedatastoreuser on fields: email_lower
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Mezij.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Mezij.jpg" alt="Integrity Error"></a>
  The datastore kind has two empty entries in the <code>email_lower</code> field. 
  <a href="https://i.stack.imgur.com/xSr0l.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xSr0l.jpg" alt="email_lower"></a></p>
  
  <p>Even the <code>google.appengine.api.users</code> module starts misbehaving. On first attempt to login, I can login to the AppEngine normally but I cannot logout of the appengine as a google account user, I see that I have logged out of my Google account(that's great but). When I try logging in, I see that no authentication was required to login (Google Sign In).</p>
  
  <p>When I login from another browser instance, It shows</p>
  
  <pre><code>DatabaseError: save with update_fields did not affect any rows.
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/9PMAn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9PMAn.jpg" alt="Database Error"></a></p>
  
  <p>Can someone please explain why this is happening and what I must do to avoid this. </p>