You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2012/12/07 23:26:36 UTC

[35/49] git commit: [#5354] create missing user-projects on demand

[#5354] create missing user-projects on demand

The `except S.Invalid` code removed is very old transitional code
that is no longer needed


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/6cead113
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/6cead113
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/6cead113

Branch: refs/heads/db/4803
Commit: 6cead11307c8a263d3fb6dca9c76e331e6c94d69
Parents: 2751e0e
Author: Dave Brondsema <db...@geek.net>
Authored: Thu Nov 29 20:40:07 2012 +0000
Committer: Cory Johns <jo...@geek.net>
Committed: Mon Dec 3 21:56:29 2012 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/project.py               |    6 ++++
 Allura/allura/lib/plugin.py                        |   13 +++++++++
 Allura/allura/model/auth.py                        |   21 +++++----------
 .../allura/tests/functional/test_neighborhood.py   |    5 +++
 Allura/allura/tests/model/test_auth.py             |    8 +++++
 5 files changed, 39 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/6cead113/Allura/allura/controllers/project.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/project.py b/Allura/allura/controllers/project.py
index ef88b51..f1c0ada 100644
--- a/Allura/allura/controllers/project.py
+++ b/Allura/allura/controllers/project.py
@@ -65,7 +65,13 @@ class NeighborhoodController(object):
         if not h.re_path_portion.match(pname):
             raise exc.HTTPNotFound, pname
         project = M.Project.query.get(shortname=self.prefix + pname, neighborhood_id=self.neighborhood._id)
+        if project is None and self.prefix == 'u/':
+            # create user-project if it is missing
+            user = M.User.query.get(username=pname)
+            if user:
+                project = self.neighborhood.register_project('u/' + user.username, user=user, user_project=True)
         if project is None:
+            # look for neighborhood tools matching the URL
             project = self.neighborhood.neighborhood_project
             c.project = project
             return ProjectController()._lookup(pname, *remainder)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/6cead113/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 495fdfe..93f4e3a 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -514,6 +514,19 @@ class ProjectRegistrationProvider(object):
                 project_template['icon']['filename'], icon_file,
                 square=True, thumbnail_size=(48, 48),
                 thumbnail_meta=dict(project_id=p._id, category='icon'))
+
+        if user_project:
+            # Allow for special user-only tools
+            p._extra_tool_status = ['user']
+            # add user project informative text to home
+            from forgewiki import model as WM
+            home_app = p.app_instance('wiki')
+            home_page = WM.Page.query.get(app_config_id=home_app.config._id)
+            home_page.text = ("This is the personal project of %s."
+            " This project is created automatically during user registration"
+            " as an easy place to store personal data that doesn't need its own"
+            " project such as cloned repositories.") % user.display_name
+
         # clear the RoleCache for the user so this project will
         # be picked up by user.my_projects()
         g.credentials.clear_user(user._id)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/6cead113/Allura/allura/model/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index b5c8567..349e05f 100644
--- a/Allura/allura/model/auth.py
+++ b/Allura/allura/model/auth.py
@@ -409,15 +409,6 @@ class User(MappedClass, ActivityNode, ActivityObject):
         if user and make_project:
             n = M.Neighborhood.query.get(name='Users')
             p = n.register_project('u/' + user.username, user=user, user_project=True)
-            # Allow for special user-only tools
-            p._extra_tool_status = ['user']
-            # add user project informative text to home
-            home_app = p.app_instance('wiki')
-            home_page = WM.Page.query.get(app_config_id=home_app.config._id)
-            home_page.text = ("This is the personal project of %s."
-            " This project is created automatically during user registration"
-            " as an easy place to store personal data that doesn't need its own"
-            " project such as cloned repositories.") % user.display_name
         return user
 
     def private_project(self):
@@ -425,11 +416,13 @@ class User(MappedClass, ActivityNode, ActivityObject):
         Returns the personal user-project for the user
         '''
         from .project import Project
-        try:
-            return Project.query.get(shortname='u/%s' % self.username, deleted=False)
-        except S.Invalid:
-            log.exception('Error retrieving private_project for %s', self.username)
-            return None
+        p = Project.query.get(shortname='u/%s' % self.username, deleted=False)
+        if not p:
+            # create user-project on demand if it is missing
+            from allura import model as M
+            n = M.Neighborhood.query.get(name='Users')
+            p = n.register_project('u/' + self.username, user=self, user_project=True)
+        return p
 
     @property
     def script_name(self):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/6cead113/Allura/allura/tests/functional/test_neighborhood.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_neighborhood.py b/Allura/allura/tests/functional/test_neighborhood.py
index 2999dc0..537731b 100644
--- a/Allura/allura/tests/functional/test_neighborhood.py
+++ b/Allura/allura/tests/functional/test_neighborhood.py
@@ -728,6 +728,11 @@ class TestNeighborhood(TestController):
         r = self.app.get('/u/test-user/', extra_environ=dict(username='test-user')).follow()
         assert '<a href="/u/test-user/profile/" class="ui-icon-tool-home">' in r
 
+    def test_user_project_creates_on_demand(self):
+        M.User.register(dict(username='donald-duck'), make_project=False)
+        ThreadLocalORMSession.flush_all()
+        self.app.get('/u/donald-duck/')
+
     def test_more_projects_link(self):
         r = self.app.get('/adobe/adobe-1/admin/')
         link = r.html.find('div', {'class':'neighborhood_title_link'}).find('a')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/6cead113/Allura/allura/tests/model/test_auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_auth.py b/Allura/allura/tests/model/test_auth.py
index f9ab925..e32349c 100644
--- a/Allura/allura/tests/model/test_auth.py
+++ b/Allura/allura/tests/model/test_auth.py
@@ -85,6 +85,14 @@ def test_user():
     assert not provider._validate_password(u, 'foo')
 
 @with_setup(setUp)
+def test_user_project_creates_on_demand():
+    u = M.User.register(dict(username='foobar123'), make_project=False)
+    ThreadLocalORMSession.flush_all()
+    assert not M.Project.query.get(shortname='u/foobar123')
+    assert u.private_project()
+    assert M.Project.query.get(shortname='u/foobar123')
+
+@with_setup(setUp)
 def test_project_role():
     role = M.ProjectRole(project_id=c.project._id, name='test_role')
     c.user.project_role().roles.append(role._id)