8687.15.11
by Karl Fogel
Add the copyright header block to files under lib/lp/answers/. |
1 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
3 |
|
4 |
"""Helper functions for Answer Tracker tests."""
|
|
5 |
||
6 |
__metaclass__ = type |
|
7 |
__all__ = [ |
|
11235.5.4
by Curtis Hovey
hush lint. |
8 |
'QuestionFactory', |
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
9 |
]
|
10 |
||
11 |
from zope.component import getUtility |
|
12 |
||
10409.5.55
by Curtis Hovey
Removed glob imports from answers. |
13 |
from canonical.launchpad.webapp.interfaces import ILaunchBag |
11403.1.4
by Henning Eggers
Reformatted imports using format-imports script r32. |
14 |
from lp.answers.interfaces.questiontarget import IQuestionTarget |
10409.5.55
by Curtis Hovey
Removed glob imports from answers. |
15 |
from lp.registry.interfaces.pillar import IPillarNameSet |
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
16 |
|
17 |
||
18 |
class QuestionFactory: |
|
19 |
"""Helper object that can be used to quickly create questions."""
|
|
20 |
||
21 |
@classmethod
|
|
4354.1.11
by Francis J. Lacoste
Simplify API to only accept a project by name. |
22 |
def _getQuestionTarget(cls, target_name): |
23 |
"""Return the `IQuestionTarget` to use.
|
|
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
24 |
|
4354.1.11
by Francis J. Lacoste
Simplify API to only accept a project by name. |
25 |
It returns the pillar with the target_name and makes sure it
|
26 |
provides `IQuestionTarget`.
|
|
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
27 |
"""
|
4354.1.11
by Francis J. Lacoste
Simplify API to only accept a project by name. |
28 |
assert isinstance(target_name, basestring), ( |
29 |
"expected a project name: %r", target_name) |
|
30 |
target = getUtility(IPillarNameSet).getByName(target_name) |
|
31 |
assert target is not None, ( |
|
32 |
'No project with name %s' % target_name) |
|
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
33 |
assert IQuestionTarget.providedBy(target), ( |
34 |
"%r doesn't provide IQuestionTarget" % target) |
|
35 |
return target |
|
36 |
||
37 |
@classmethod
|
|
38 |
def createManyByProject(cls, specification): |
|
39 |
"""Create a number of questions on selected projects.
|
|
40 |
||
4354.1.11
by Francis J. Lacoste
Simplify API to only accept a project by name. |
41 |
The function expects a sequence of tuples of the form
|
42 |
(project_name, question_count).
|
|
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
43 |
|
4354.1.11
by Francis J. Lacoste
Simplify API to only accept a project by name. |
44 |
project_name should be the name of a pillar providing
|
45 |
`IQuestionTarget`.
|
|
4785.3.7
by Jeroen Vermeulen
Removed whitespace at ends of lines |
46 |
|
4354.1.11
by Francis J. Lacoste
Simplify API to only accept a project by name. |
47 |
question_count is the number of questions to create on the target.
|
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
48 |
|
4354.1.11
by Francis J. Lacoste
Simplify API to only accept a project by name. |
49 |
Questions will appear as posted by the currently logged in user.
|
4354.1.9
by Francis J. Lacoste
Add QuestionFactory helper to create many questions on many projects. |
50 |
"""
|
51 |
for project, question_count in specification: |
|
52 |
target = cls._getQuestionTarget(project) |
|
6423.4.1
by Bjorn Tillenius
add QuestionSet.getQuestionCountsForPackages(). |
53 |
cls.createManyByTarget(target, question_count) |
54 |
||
55 |
@classmethod
|
|
56 |
def createManyByTarget(cls, target, question_count): |
|
57 |
"""Create a number of questions on a selected target
|
|
58 |
||
59 |
:param question_count: The number of questions to create on the
|
|
60 |
target.
|
|
61 |
||
62 |
Questions will appear as posted by the currently logged in user.
|
|
63 |
"""
|
|
64 |
owner = getUtility(ILaunchBag).user |
|
65 |
created_questions = [] |
|
66 |
for index in range(question_count): |
|
11235.5.4
by Curtis Hovey
hush lint. |
67 |
replacements = {'index': index, 'target': target.displayname} |
6423.4.1
by Bjorn Tillenius
add QuestionSet.getQuestionCountsForPackages(). |
68 |
created_questions.append(target.newQuestion( |
69 |
owner, |
|
70 |
'Question %(index)s on %(target)s' % replacements, |
|
71 |
'Description %(index)s on %(target)s' % replacements)) |
|
72 |
return created_questions |