Merge lp:~salgado/offspring/private-projects into lp:~linaro-automation/offspring/private-builds

Proposed by Guilherme Salgado
Status: Merged
Approved by: James Tunnicliffe
Approved revision: no longer in the source branch.
Merged at revision: 71
Proposed branch: lp:~salgado/offspring/private-projects
Merge into: lp:~linaro-automation/offspring/private-builds
Diff against target: 64 lines (+26/-3)
2 files modified
lib/offspring/web/queuemanager/models.py (+14/-3)
lib/offspring/web/queuemanager/tests/test_models.py (+12/-0)
To merge this branch: bzr merge lp:~salgado/offspring/private-projects
Reviewer Review Type Date Requested Status
James Tunnicliffe (community) Approve
Review via email: mp+80081@code.launchpad.net

Description of the change

While hacking on another branch I noticed that .is_private and .is_visible_to(user) would crash with an AttributeError if there was no access-controlled object associated to the receiver of .is_private or .is_visible_to(user). This branch fixes that.

To post a comment you must log in.
Revision history for this message
James Tunnicliffe (dooferlad) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/offspring/web/queuemanager/models.py'
2--- lib/offspring/web/queuemanager/models.py 2011-10-20 16:13:00 +0000
3+++ lib/offspring/web/queuemanager/models.py 2011-10-21 15:32:24 +0000
4@@ -168,11 +168,18 @@
5 return privacy_attr
6
7 def _get_access_controlled_object(self):
8+ """Return the object associated to this one that is access controlled.
9+
10+ In some cases the relationship between this model and the one which is
11+ access controlled is NULLable (e.g. Lexbuilder.current_job), and if
12+ that's the case and there's no access-controlled object linked to this
13+ one, we return None.
14+ """
15 if getattr(self, 'access_relation', None) is None:
16 return self
17 names = self.access_relation.split('__')
18 obj = self
19- while len(names):
20+ while len(names) and obj is not None:
21 name = names.pop(0)
22 obj = getattr(obj, name)
23 return obj
24@@ -180,11 +187,15 @@
25 @property
26 def is_private(self):
27 obj = self._get_access_controlled_object()
28+ if obj is None:
29+ return False
30 return getattr(obj, self._privacy_attr_name)
31
32 def is_visible_to(self, user):
33- is_visible_to = getattr(self._get_access_controlled_object(),
34- self._is_visible_method_name)
35+ obj = self._get_access_controlled_object()
36+ if obj is None:
37+ return True
38+ is_visible_to = getattr(obj, self._is_visible_method_name)
39 return is_visible_to(user)
40
41
42
43=== modified file 'lib/offspring/web/queuemanager/tests/test_models.py'
44--- lib/offspring/web/queuemanager/tests/test_models.py 2011-10-18 19:09:11 +0000
45+++ lib/offspring/web/queuemanager/tests/test_models.py 2011-10-21 15:32:24 +0000
46@@ -200,6 +200,18 @@
47 return factory.makeLexbuilder(
48 current_job=factory.makeBuildResult(project=project))
49
50+ def test_lexbuilder_without_current_job(self):
51+ # A Lexbuilder with no current_job is not considered private and thus
52+ # is visible to anyone.
53+ builder = factory.makeLexbuilder()
54+ builder.current_job = None
55+ builder.save()
56+ user = factory.makeUser()
57+ self.assertEqual(False, builder.is_private)
58+ self.assertEqual(True, builder.is_visible_to(user))
59+ self.assertEqual(
60+ [builder], list(self.model.all_objects.accessible_by_user(user)))
61+
62
63 class BuildResultTests(
64 TestCase, ReleaseOrLexbuilderOrBuildResultOrBuildRequestTestsMixin):

Subscribers

People subscribed via source and target branches