1. Robot Mirror
  2. bugs.python.org

Commits

martin.v.loewis  committed 93cf647

Make roundup users, django users and codereview accounts
all have the same IDs.

  • Participants
  • Parent commits 2b56654
  • Branches default

Comments (0)

Files changed (3)

File rietveld/Makefile

View file
 default:
 	@echo "Run 'make all' to fetch required sources to run this example."
 
-all: static templates codereview django gae2django syncdb
+all: static templates_svn codereview django gae2django syncdb
 	@echo "Run './manage.py runserver 127.0.0.1:8000' to run Rietveld."
 
 clean: clean_local clean_external
 
 codereview:
 	svn co http://rietveld.googlecode.com/svn/trunk/codereview@$(RIETVELDREV)
+	patch -p0 < codereview.diff
 
 static:
 	svn co http://rietveld.googlecode.com/svn/trunk/static@$(RIETVELDREV)

File rietveld/codereview.diff

View file
+Index: codereview/models.py
+===================================================================
+--- codereview/models.py	(Revision 510)
++++ codereview/models.py	(Arbeitskopie)
+@@ -520,14 +520,14 @@
+   @classmethod
+   def get_account_for_user(cls, user):
+     """Get the Account for a user, creating a default one if needed."""
+-    email = user.email()
+-    assert email
+-    key = '<%s>' % email
+     # Since usually the account already exists, first try getting it
+     # without the transaction implied by get_or_insert().
+-    account = cls.get_by_key_name(key)
++    # Roundup adjustment: accounts will have the same IDs as users
++    account = cls.get_by_id(user.id)
+     if account is not None:
+       return account
++    # Not found, don't auto-create in any case
++    raise engine.FetchError('account not found')
+     nickname = cls.create_nickname_for_user(user)
+     return cls.get_or_insert(key, user=user, email=email, nickname=nickname,
+                              fresh=True)

File rietveld/roundup_helper/middleware.py

View file
 from models import Session, User
+from codereview.models import Account
 from django.contrib import auth
 from django.contrib.auth.backends import RemoteUserBackend
 
 class UserBackend(RemoteUserBackend):
-    def configure_user(self, user):
-        roundup_user = User.objects.filter(_username=user.username)[0]
-        user.email = roundup_user._address
-        user.save()
-        from codereview import models
-        account = models.Account.get_account_for_user(user)
-        account.nickname = user.username
-        account.save()
-        return user
+    # auto-creation of django users should not be necessary,
+    # as they should have been created before, so suppress it here.
+    create_unknown_user = False
 
 class LookupRoundupUser(object):
 
         username = eval(session[0].session_value)['user']
         # the username comes from the cookie, so it really ought to exist
         roundup_user = User.objects.filter(_username=username)[0]
-        if not roundup_user._address:
-            # Rietveld insists that user objects must have email addresses
-            return
-        # Taken from RemoteUserMiddleware: auto-create the user if it's new
+        # if we already have a user from the session, we are done.
         if request.user.is_authenticated():
             if request.user.username == username:
                 return
-        # We are seeing this user for the first time in this session, attempt
-        # to authenticate the user.
+        # We see the user for the first time. Authenticate it, and create
+        # codereview account if none exists.
         user = auth.authenticate(remote_user=username)
-        if user:
-            # User is valid.  Set request.user and persist user in the session
-            # by logging the user in.
-            request.user = user
-            auth.login(request, user)
+        if not user:
+            return
+        # User is valid.  Set request.user and persist user in the session
+        # by logging the user in.
+        request.user = user
+        account = Account.get_by_id(user.id)
+        if not account:
+            account = Account(id=user.id, user=user, email=user.email,
+                              nickname=username, fresh=True)
+            account.put()
+        auth.login(request, user)
         
     def logout(self, request):
         # Clear django session if roundup session is gone.