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
|
# Copyright 2009 Canonical Ltd. All rights reserved.
"""Named vocabularies defined by the Answers application."""
__metaclass__ = type
__all__ = [
'FAQVocabulary',
]
from zope.interface import implements
from zope.schema.vocabulary import SimpleTerm
from canonical.launchpad.webapp.vocabulary import (
CountableIterator, IHugeVocabulary)
from lp.answers.interfaces.faq import IFAQ
from lp.answers.interfaces.faqtarget import IFAQTarget
class FAQVocabulary:
"""Vocabulary containing all the FAQs in an `IFAQTarget`."""
implements(IHugeVocabulary)
displayname = 'Select a FAQ'
def __init__(self, context):
"""Create a new vocabulary for the context.
:param context: It should adaptable to `IFAQTarget`.
"""
self.context = IFAQTarget(context)
def __len__(self):
"""See `IIterableVocabulary`."""
return self.context.searchFAQs().count()
def __iter__(self):
"""See `IIterableVocabulary`."""
for faq in self.context.searchFAQs():
yield self.toTerm(faq)
def __contains__(self, value):
"""See `IVocabulary`."""
if not IFAQ.providedBy(value):
return False
return self.context.getFAQ(value.id) is not None
def getTerm(self, value):
"""See `IVocabulary`."""
if value not in self:
raise LookupError(value)
return self.toTerm(value)
def getTermByToken(self, token):
"""See `IVocabularyTokenized`."""
try:
faq_id = int(token)
except ValueError:
raise LookupError(token)
faq = self.context.getFAQ(token)
if faq is None:
raise LookupError(token)
return self.toTerm(faq)
def toTerm(self, faq):
"""Return the term for a FAQ."""
return SimpleTerm(faq, faq.id, faq.title)
def searchForTerms(self, query=None):
"""See `IHugeVocabulary`."""
results = self.context.findSimilarFAQs(query)
return CountableIterator(results.count(), results, self.toTerm)
|