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
292
293
294
295
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# pylint: disable-msg=E0611,W0212
"""FAQ document models."""
__metaclass__ = type
__all__ = [
'FAQ',
'FAQSearch',
'FAQSet',
]
from lazr.lifecycle.event import ObjectCreatedEvent
from sqlobject import (
ForeignKey,
SQLMultipleJoin,
SQLObjectNotFound,
StringCol,
)
from sqlobject.sqlbuilder import SQLConstant
from zope.event import notify
from zope.interface import implements
from canonical.database.constants import DEFAULT
from canonical.database.datetimecol import UtcDateTimeCol
from canonical.database.nl_search import nl_phrase_search
from canonical.database.sqlbase import (
quote,
SQLBase,
sqlvalues,
)
from lp.answers.interfaces.faq import (
IFAQ,
IFAQSet,
)
from lp.answers.interfaces.faqcollection import FAQSort
from lp.registry.interfaces.distribution import IDistribution
from lp.registry.interfaces.person import (
IPerson,
validate_public_person,
)
from lp.registry.interfaces.product import IProduct
from lp.registry.interfaces.projectgroup import IProjectGroup
class FAQ(SQLBase):
"""See `IFAQ`."""
implements(IFAQ)
_table = 'FAQ'
_defaultOrder = ['date_created', 'id']
owner = ForeignKey(
dbName='owner', foreignKey='Person',
storm_validator=validate_public_person, notNull=True)
title = StringCol(notNull=True)
keywords = StringCol(dbName="tags", notNull=False, default=None)
content = StringCol(notNull=False, default=None)
date_created = UtcDateTimeCol(notNull=True, default=DEFAULT)
last_updated_by = ForeignKey(
dbName='last_updated_by', foreignKey='Person',
storm_validator=validate_public_person, notNull=False,
default=None)
date_last_updated = UtcDateTimeCol(notNull=False, default=None)
product = ForeignKey(
dbName='product', foreignKey='Product', notNull=False, default=None)
distribution = ForeignKey(
dbName='distribution', foreignKey='Distribution', notNull=False,
default=None)
related_questions = SQLMultipleJoin(
'Question', joinColumn='faq', orderBy=['Question.datecreated'])
@property
def target(self):
"""See `IFAQ`."""
if self.product:
return self.product
else:
return self.distribution
@staticmethod
def new(owner, title, content, keywords=keywords, date_created=None,
product=None, distribution=None):
"""Factory method to create a new FAQ.
Ensure that only one of product or distribution is given.
"""
if not IPerson.providedBy(owner):
raise AssertionError(
'owner parameter should be an IPerson, not %s' % type(owner))
if product is not None and distribution is not None:
raise AssertionError(
"only one of product or distribution should be provided")
if product is None and distribution is None:
raise AssertionError("product or distribution must be provided")
if date_created is None:
date_created = DEFAULT
faq = FAQ(
owner=owner, title=title, content=content, keywords=keywords,
date_created=date_created, product=product,
distribution=distribution)
notify(ObjectCreatedEvent(faq))
return faq
@staticmethod
def findSimilar(summary, product=None, distribution=None):
"""Return the FAQs similar to summary.
See `IFAQTarget.findSimilarFAQs` for details.
"""
assert not (product and distribution), (
'only one of product or distribution should be provided')
if product:
target_constraint = 'product = %s' % sqlvalues(product)
elif distribution:
target_constraint = 'distribution = %s' % sqlvalues(distribution)
else:
raise AssertionError('must provide product or distribution')
fti_search = nl_phrase_search(summary, FAQ, target_constraint)
if not fti_search:
# No useful words to search on in that summary.
return FAQ.select('1 = 2')
return FAQ.select(
'%s AND FAQ.fti @@ %s' % (target_constraint, quote(fti_search)),
orderBy=[
SQLConstant("-rank(FAQ.fti, ftq(%s))" % quote(fti_search)),
"-FAQ.date_created"])
@staticmethod
def getForTarget(id, target):
"""Return the FAQ with the requested id.
When target is not None, the target will be checked to make sure
that the FAQ is in the expected target or return None otherwise.
"""
try:
faq = FAQ.get(id)
if target is None or target == faq.target:
return faq
else:
return None
except SQLObjectNotFound:
return None
class FAQSearch:
"""Object that encapsulates a FAQ search.
It is used to implement the `IFAQCollection`.searchFAQs() method.
"""
search_text = None
owner = None
sort = None
product = None
distribution = None
project = None
def __init__(self, search_text=None, owner=None, sort=None, product=None,
distribution=None, project=None):
"""Initialize a new FAQ search.
See `IFAQCollection`.searchFAQs for the basic parameters description.
Additional parameters:
:param product: The product in which to search for FAQs.
:param distribution: The distribution in which to search for FAQs.
:param project: The project in which to search for FAQs.
"""
if search_text is not None:
assert isinstance(search_text, basestring), (
'search_text should be a string, not %s' % type(search_text))
self.search_text = search_text
if owner is not None:
assert IPerson.providedBy(owner), (
'owner should be an IPerson, not %s' % type(owner))
self.owner = owner
if sort is not None:
assert sort in FAQSort.items, (
'sort should be an item from FAQSort, not %s' % type(sort))
self.sort = sort
if product is not None:
assert IProduct.providedBy(product), (
'product should be an IProduct, not %s' % type(product))
assert distribution is None and project is None, (
'can only use one of product, distribution, or project')
self.product = product
if distribution is not None:
assert IDistribution.providedBy(distribution), (
'distribution should be an IDistribution, %s' %
type(distribution))
assert product is None and project is None, (
'can only use one of product, distribution, or project')
self.distribution = distribution
if project is not None:
assert IProjectGroup.providedBy(project), (
'project should be an IProjectGroup, not %s' % type(project))
assert product is None and distribution is None, (
'can only use one of product, distribution, or project')
self.project = project
def getResults(self):
"""Return the FAQs matching this search."""
return FAQ.select(
self.getConstraints(),
clauseTables=self.getClauseTables(),
orderBy=self.getOrderByClause())
def getConstraints(self):
"""Return the constraints to use by this search."""
constraints = []
if self.search_text:
constraints.append('FAQ.fti @@ ftq(%s)' % quote(self.search_text))
if self.owner:
constraints.append('FAQ.owner = %s' % sqlvalues(self.owner))
if self.product:
constraints.append('FAQ.product = %s' % sqlvalues(self.product))
if self.distribution:
constraints.append(
'FAQ.distribution = %s' % sqlvalues(self.distribution))
if self.project:
constraints.append(
'FAQ.product = Product.id AND Product.project = %s' % (
sqlvalues(self.project)))
return '\n AND '.join(constraints)
def getClauseTables(self):
"""Return the tables that should be added to the FROM clause."""
if self.project:
return ['Product']
else:
return []
def getOrderByClause(self):
"""Return the ORDER BY clause to sort the results."""
sort = self.sort
if sort is None:
if self.search_text is not None:
sort = FAQSort.RELEVANCY
else:
sort = FAQSort.NEWEST_FIRST
if sort is FAQSort.NEWEST_FIRST:
return "-FAQ.date_created"
elif sort is FAQSort.OLDEST_FIRST:
return "FAQ.date_created"
elif sort is FAQSort.RELEVANCY:
if self.search_text:
# SQLConstant is a workaround for bug 53455.
return [SQLConstant(
"-rank(FAQ.fti, ftq(%s))" % quote(
self.search_text)),
"-FAQ.date_created"]
else:
return "-FAQ.date_created"
else:
raise AssertionError("Unknown FAQSort value: %r" % sort)
class FAQSet:
"""See `IFAQSet`."""
implements(IFAQSet)
def getFAQ(self, id):
"""See `IFAQSet`."""
return FAQ.getForTarget(id, None)
def searchFAQs(self, search_text=None, owner=None, sort=None):
"""See `IFAQSet`."""
return FAQSearch(
search_text=search_text, owner=owner, sort=sort).getResults()
|