Commits

Donald Stufft  committed 5905820

Port the home view to Werkzeug

  • Participants
  • Parent commits cd0c21d
  • Branches werkzeug

Comments (1)

Files changed (4)

 
 
 User = namedtuple("User", ["username", "authenticated", "last_login"])
+
+AnonymousUser = User(authenticated=False, username=None, last_login=None)

File templates/home.pt

 <metal:fill fill-slot="body">
 <p>The Python Package Index is a repository of software for the Python
 programming language. There are currently
-<strong tal:content="app/store/count_packages">12343</strong>
+<strong tal:content="store/count_packages">12343</strong>
 packages here.
 <br />
 To contact the PyPI admins, please use the
 <table class="list">
 <tr><th>Updated</th><th>Package</th><th>Description</th></tr>
 
-<tal:for-release repeat="release app/store/latest_releases">
+<tal:for-release repeat="release store/latest_releases">
 <tal:let define="name python: release[0];
  version python: release[1];
  date python: release[2];

File templates/standard_template.pt

                     (<a tal:attributes="href python:app.link_action('logout')">Logout</a>)
                   </li>
 
-                  <tal:let define="packages python:app.store.user_packages(request.user.username)">
+                  <tal:let define="packages python:store.user_packages(request.user.username)">
                     <tal:if-packages condition="packages">
                       <li>
 			Your packages:
 import raven.utils.wsgi
 
 # local imports
-from models import User
+from models import AnonymousUser, User
 import store, config, versionpredicate, verify_filetype, rpc
 import MailingLogger, openid2rp, gae
 from mini_pkg_resources import safe_name
         """
         Figure out what the request is, and farm off to the appropriate handler
         """
-        request.user = User(
-            authenticated=False,
-            username="Anonymous",
-            last_login=None,
-        )
+        request.user = AnonymousUser
 
         with store.Store(self.config, queue=self.queue) as database:
             # Determine if we need to do XMLRPC
                     dbuser = database.get_user(username)
 
             # Store the user on the request
-            request.user = User(
-                authenticated=True,
-                username=username,
-                last_login=dbuser.get("last_login"),
-            )
-
-            # Determine if we need to store the users last login, as we only
-            #   want to do this once a minute.
-            update_last_login = not request.user.last_login or (
-                time.time() - time.mktime(
-                    request.user.last_login.timetuple(),
-                ) > 60
-            )
-            database.set_user(username, request.remote_addr, update_last_login)
+            if username:
+                request.user = User(
+                    authenticated=True,
+                    username=username,
+                    last_login=dbuser.get("last_login"),
+                )
+
+                # Determine if we need to store the users last login, as we
+                #   only want to do this once a minute.
+                update_last_login = not request.user.last_login or (
+                    time.time() - time.mktime(
+                        request.user.last_login.timetuple(),
+                    ) > 60
+                )
+                database.set_user(
+                    username,
+                    request.remote_addr,
+                    update_last_login,
+                )
+            else:
+                request.user = AnonymousUser
 
         # Commit all user-related changes made up to here
         if request.user.authenticated:
 
         context = {
             "request": request,
+            "store": database,
             "data": options,
             "app": self,
             "standard_template": PyPiPageTemplate(
 
         return Response(headers=headers)
 
-    def home(self, nav_current='home'):
-        self.write_template('home.pt', title='PyPI - the Python Package Index',
-                            headers={'X-XRDS-Location':self.url_machine+'/id'})
+    def do_home(self, request, database, nav_current='home'):
+        return Response(
+            self.render_template(request, database, "home.pt",
+                title="PyPI - the Python Package Index",
+            ),
+            content_type="text/html",
+            headers={
+                "X-XRDS-Location": urlparse.urljoin(request.host_url, "/id"),
+            }
+        )
 
     def about(self, nav_current='home'):
         self.write_template('about.pt', title='About PyPI')