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
|
IFAQCollection Interface
========================
The Launchpad Answer Tracker can be used to track answers to commonly
asked questions. Regular user questions can then be answered with a
referral to the FAQ document.
Objects that represents collection of FAQs provides the IFAQCollection
interface. (The test harness is responsible for providing an object
providing this interface in the 'collection' variable of the test name
space. That way we can verify that multiple implementation provide the
interface correctly.)
>>> from zope.interface.verify import verifyObject
>>> from lp.answers.interfaces.faqcollection import IFAQCollection
>>> verifyObject(IFAQCollection, collection)
True
Population FAQs collection
--------------------------
The IFAQCollection interface is a read-only interface. The IFAQTarget
interface is used for creating FAQs (see faqtarget.txt for details).
Since not all IFAQCollections are IFAQTarget, we rely on the harness to
provide us with a newFAQ() function that can be used to add a FAQ to the
collection.
>>> from lp.registry.interfaces.person import IPersonSet
>>> personset = getUtility(IPersonSet)
>>> no_priv = personset.getByName('no-priv')
>>> foo_bar = personset.getByEmail('foo.bar@canonical.com')
>>> sample_person = personset.getByEmail('test@canonical.com')
>>> login('foo.bar@canonical.com')
>>> faq_specifications = [
... (no_priv, 'How do I install Foo?',
... 'See http://www.foo.org/install', 'foo install'),
... (sample_person, 'What is The Meaning of Life?',
... 'A Monty Python film!', 'film monty python'),
... (foo_bar, 'How do I make money quickly off the Internet?',
... 'Install this really nice software that you can find at '
... 'http://www.getrichquick.com/.', None),
... (no_priv, 'How do I play the Game of Life?',
... 'Keep breathing!', 'install'),
... (sample_person, 'Who really shot JFK?',
... 'You decide: there were at least six conspiracies going on '
... 'in Dallas on Nov 22nd 1963.', None),
... (no_priv, 'What were the famous last words?',
... 'Who turned off the light?', None)
... ]
>>> from datetime import datetime, timedelta
>>> from pytz import UTC
>>> now = datetime.now(UTC)
>>> faq_set = []
>>> for owner, title, content, keywords in faq_specifications:
... date = now + timedelta(minutes=len(faq_set))
... faq_set.append(newFAQ(owner, title, content, keywords, date))
>>> login(ANONYMOUS)
getFAQ()
--------
It is possible to retrieve a FAQ in a collection using its id by using
the getFAQ() method.
>>> faq = faq_set[0]
>>> collection.getFAQ(faq.id) == faq
True
It returns None when there is FAQ with that ID in the context:
>>> print collection.getFAQ(12345)
None
It also returns None when using the ID of a FAQ that isn't in the
requested collection:
>>> from lp.services.webapp.interfaces import ILaunchBag
>>> from lp.registry.interfaces.distribution import IDistributionSet
>>> ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
>>> ubuntu != collection
True
>>> login('foo.bar@canonical.com')
>>> foo_bar = getUtility(ILaunchBag).user
>>> ubuntu_faq = ubuntu.newFAQ(
... foo_bar, 'Ubuntu Installation HowTo',
... 'Ubuntu installation procedure can be found at: '
... 'https://help.ubuntu.com/community/Installation')
>>> login(ANONYMOUS)
>>> print collection.getFAQ(ubuntu_faq.id)
None
searchFAQs
----------
The searchFAQs() method is used to select a set of FAQs in the
collection matching various criteria.
When no criteria are given, all the FAQs in the collection are returned.
(The default sort order is most recent first.)
>>> for faq in collection.searchFAQs():
... print faq.title
What were the famous last words?
Who really shot JFK?
How do I play the Game of Life?
How do I make money quickly off the Internet?
What is The Meaning of Life?
How do I install Foo?
search_text
...........
The first criteria is search_text. It will select FAQs matching the
keywords specified. Keywords are looked for in the title, content and
keywords field of the FAQ.
>>> for faq in collection.searchFAQs(search_text='install'):
... print faq.title
How do I install Foo?
How do I play the Game of Life?
How do I make money quickly off the Internet?
By default, the results are sorted by relevancy. In the above example,
the first result is more relevant because the keyword appear in the
title, the second because it appears in the keywords.
owner
.....
The other filtering criteria is 'owner'. It will select only FAQs that
were created by the specified user.
>>> for faq in collection.searchFAQs(owner=no_priv):
... print faq.title
What were the famous last words?
How do I play the Game of Life?
How do I install Foo?
Again, the default sort order is most recent first.
Combination
...........
You can combine multiple criterias. Only FAQs matching all the criterias
will be returned.
>>> for faq in collection.searchFAQs(
... search_text='install', owner=no_priv):
... print faq.title
How do I install Foo?
How do I play the Game of Life?
sort
....
The sort parameter can be used to control the sort order of the results.
It takes a value from the FAQSort enumerated type. For example, the
FAQSort.NEWEST_FIRST can be used to sort the results of a text search by
date of creation (most recent first):
>>> from lp.answers.interfaces.faqcollection import FAQSort
>>> for faq in collection.searchFAQs(
... search_text='install', sort=FAQSort.NEWEST_FIRST):
... print faq.title
How do I play the Game of Life?
How do I make money quickly off the Internet?
How do I install Foo?
The FAQSort.OLDEST_FIRST can be used to have the oldest FAQs sorted
first:
>>> for faq in collection.searchFAQs(sort=FAQSort.OLDEST_FIRST):
... print faq.title
How do I install Foo?
What is The Meaning of Life?
How do I make money quickly off the Internet?
How do I play the Game of Life?
Who really shot JFK?
What were the famous last words?
|