1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
====================
Question collections
====================
The IQuestionSet utility is used to retrieve and search for questions no
matter which question target they were created for.
>>> from lp.services.webapp.testing import verifyObject
>>> from lp.answers.interfaces.questioncollection import IQuestionSet
>>> question_set = getUtility(IQuestionSet)
>>> verifyObject(IQuestionSet, question_set)
True
Retrieving questions
====================
The get() method can be used to retrieve a question with a specific id.
>>> question_one = question_set.get(1)
>>> print question_one.title
Firefox cannot render Bank Site
If no question exists, a default value is returned.
>>> default = object()
>>> question_nonexistant = question_set.get(123456, default=default)
>>> question_nonexistant is default
True
If no default value is given, None is returned.
>>> print question_set.get(123456)
None
Searching questions
===================
The IQuestionSet interface defines a searchQuestions() method that is used to
search for questions defined in any question target.
Search text
-----------
The search_text parameter will return questions matching the query using the
regular full text algorithm.
# Because not everyone uses a real editor <wink>
>>> from lp.services.encoding import ascii_smash
>>> for question in question_set.searchQuestions(search_text='firefox'):
... print ascii_smash(question.title), question.target.displayname
Problemas de Impressao no Firefox Mozilla Firefox
Firefox loses focus and gets stuck Mozilla Firefox
Firefox cannot render Bank Site Mozilla Firefox
mailto: problem in webpage mozilla-firefox in Ubuntu
Newly installed plug-in doesn't seem to be used Mozilla Firefox
Problem showing the SVG demo on W3C site Mozilla Firefox
AINKAFSEEN ALEFLAMTEHGHAINYEHYEHREHALEFTEH ... Ubuntu
Status
------
By default, expired and invalid questions are not searched for. The status
parameter can be used to select the questions in the status you are interested
in.
>>> from lp.answers.enums import QuestionStatus
>>> for question in question_set.searchQuestions(
... status=QuestionStatus.INVALID):
... print question.title, question.status.title, (
... question.target.displayname)
Firefox is slow and consumes too ... Invalid mozilla-firefox in Ubuntu
The status parameter can also take a list of statuses.
>>> for question in question_set.searchQuestions(
... status=[QuestionStatus.SOLVED, QuestionStatus.INVALID]):
... print question.title, question.status.title, (
... question.target.displayname)
mailto: problem in webpage Solved mozilla-firefox in Ubuntu
Firefox is slow and consumes too ... Invalid mozilla-firefox in Ubuntu
Language
--------
The language parameter can be used to select only questions written in a
particular language.
>>> from lp.services.worlddata.interfaces.language import ILanguageSet
>>> spanish = getUtility(ILanguageSet)['es']
>>> for t in question_set.searchQuestions(language=spanish):
... print ascii_smash(t.title)
Problema al recompilar kernel con soporte smp (doble-nucleo)
Combinations
------------
The returned set of questions is the intersection of the sets delimited by
each criteria.
>>> for question in question_set.searchQuestions(
... search_text='firefox',
... status=(QuestionStatus.OPEN, QuestionStatus.INVALID)):
... print ascii_smash(question.title), question.status.title, (
... question.target.displayname)
Problemas de Impressao no Firefox Open Mozilla Firefox
Firefox is slow and consumes too much ... mozilla-firefox in Ubuntu
Firefox loses focus and gets stuck Open Mozilla Firefox
Firefox cannot render Bank Site Open Mozilla Firefox
Problem showing the SVG demo on W3C site Open Mozilla Firefox
AINKAFSEEN ALEFLAMTEHGHAINYEHYEHREHALEFTEH ... Ubuntu
Sort order
----------
When using the search_text criteria, the default is to sort the results by
relevancy. One can use the sort parameter to change the order. It takes one
of the constant defined in the QuestionSort enumeration.
>>> from lp.answers.enums import QuestionSort
>>> for question in question_set.searchQuestions(
... search_text='firefox', sort=QuestionSort.OLDEST_FIRST):
... print question.id, ascii_smash(question.title), (
... question.target.displayname)
14 AINKAFSEEN ALEFLAMTEHGHAINYEHYEHREHALEFTEH ... Ubuntu
1 Firefox cannot render Bank Site Mozilla Firefox
2 Problem showing the SVG demo on W3C site Mozilla Firefox
4 Firefox loses focus and gets stuck Mozilla Firefox
6 Newly installed plug-in doesn't seem to be used Mozilla Firefox
9 mailto: problem in webpage mozilla-firefox in Ubuntu
13 Problemas de Impressao no Firefox Mozilla Firefox
When no text search is done, the default sort order is by newest first.
>>> for question in question_set.searchQuestions(
... status=QuestionStatus.OPEN)[:5]:
... print question.id, ascii_smash(question.title), (
... question.target.displayname)
13 Problemas de Impressao no Firefox Mozilla Firefox
12 Problema al recompilar kernel con soporte smp (doble-nucleo) Ubuntu
11 Continue playing after shutdown Ubuntu
5 Installation failed Ubuntu
4 Firefox loses focus and gets stuck Mozilla Firefox
Question languages
==================
The getQuestionLanguages() method returns the set of languages in which
questions are written in launchpad.
>>> print ', '.join(
... sorted(language.code
... for language in question_set.getQuestionLanguages()))
ar, en, es, pt_BR
Active projects
===============
Set Up
------
The test assume some database values have been set for usage enums, so first
we'll set those up.
>>> import transaction
>>> from lp.app.enums import ServiceUsage
>>> from lp.registry.interfaces.distribution import IDistributionSet
>>> from lp.registry.interfaces.product import IProductSet
>>> firefox = getUtility(IProductSet).getByName('firefox')
>>> ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
>>> login('admin@canonical.com')
>>> firefox.answers_usage = ServiceUsage.LAUNCHPAD
>>> ubuntu.answers_usage = ServiceUsage.LAUNCHPAD
>>> transaction.commit()
This method can be used to retrieve the projects that are the most actively
using the Answer Tracker in the last 60 days. By active, we mean that the
project is registered as officially using Answers and had some questions asked
in the period. The projects are ordered by the number of questions asked
during the period.
Initially, no projects are returned.
>>> list(question_set.getMostActiveProjects())
[]
Then some recent questions are created on a number of projects.
>>> from lp.answers.testing import QuestionFactory
>>> from lp.registry.interfaces.person import IPersonSet
>>> firefox = getUtility(IProductSet).getByName('firefox')
>>> landscape = getUtility(IProductSet).getByName('landscape')
>>> launchpad = getUtility(IProductSet).getByName('launchpad')
>>> no_priv = getUtility(IPersonSet).getByName('no-priv')
>>> ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
>>> login('no-priv@canonical.com')
>>> QuestionFactory.createManyByProject((
... ('ubuntu', 3),
... ('firefox', 2),
... ('landscape', 1),
... ))
A question is created just before the time limit on Launchpad.
>>> from datetime import datetime, timedelta
>>> from pytz import UTC
>>> question = launchpad.newQuestion(
... no_priv, 'Launchpad question', 'A question',
... datecreated=datetime.now(UTC) - timedelta(days=61))
>>> login(ANONYMOUS)
The method returns only projects which officially use the Answer Tracker. The
order of the returned projects is based on the number of questions asked
during the period.
>>> print ubuntu.answers_usage.name
LAUNCHPAD
>>> print firefox.answers_usage.name
LAUNCHPAD
>>> print landscape.answers_usage.name
UNKNOWN
>>> print launchpad.answers_usage.name
LAUNCHPAD
# Launchpad is not returned because the question was not asked in
# the last 60 days.
>>> for project in question_set.getMostActiveProjects():
... print project.displayname
Ubuntu
Mozilla Firefox
The method accepts an optional limit parameter limiting the number of
project returned:
>>> for project in question_set.getMostActiveProjects(limit=1):
... print project.displayname
Ubuntu
Counting the open questions
===========================
getOpenQuestionCountByPackages() allow you to get the count of open questions
on a set of IDistributionSourcePackage packages.
>>> question_set.getOpenQuestionCountByPackages([])
{}
It returns the number of open questions for each given package.
>>> ubuntu_evolution = ubuntu.getSourcePackage('evolution')
>>> ubuntu_pmount = ubuntu.getSourcePackage('pmount')
>>> debian = getUtility(IDistributionSet).getByName('debian')
>>> debian_evolution = debian.getSourcePackage('evolution')
>>> debian_pmount = debian.getSourcePackage('pmount')
>>> login('foo.bar@canonical.com')
>>> QuestionFactory.createManyByTarget(ubuntu_pmount, 4)
[...]
>>> QuestionFactory.createManyByTarget(debian_evolution, 3)
[...]
>>> open_question, closed_question = QuestionFactory.createManyByTarget(
... ubuntu_evolution, 2)
>>> closed_question.setStatus(
... closed_question.owner, QuestionStatus.SOLVED, 'no comment')
<QuestionMessage at ...>
>>> packages = (
... ubuntu_evolution, ubuntu_pmount, debian_evolution, debian_pmount)
>>> package_counts = question_set.getOpenQuestionCountByPackages(packages)
>>> len(packages)
4
>>> for package in packages:
... print "%s: %s" % (
... package.bugtargetname,
... package_counts[package])
evolution (Ubuntu): 1
pmount (Ubuntu): 4
evolution (Debian): 3
pmount (Debian): 0
|