1
Working with Launchpad Answers over the API
2
===========================================
4
Users can work with question targets and questions over the api to
5
search and update questions. This demonstration will use a project, it's
6
contact, and asker, and three questions.
8
>>> from zope.component import getUtility
9
>>> from canonical.launchpad.testing.pages import webservice_for_person
10
>>> from canonical.launchpad.webapp.interfaces import OAuthPermission
11
>>> from lp.app.enums import ServiceUsage
12
>>> from lp.services.worlddata.interfaces.language import ILanguageSet
13
>>> from lp.testing.sampledata import ADMIN_EMAIL
14
>>> lang_set = getUtility(ILanguageSet)
16
>>> login(ADMIN_EMAIL)
17
>>> _contact = factory.makePerson(name='contact')
18
>>> _project = factory.makeProduct(name='my-project', owner=_contact)
19
>>> _contact.addLanguage(lang_set['en'])
20
>>> _project.answers_usage = ServiceUsage.LAUNCHPAD
21
>>> success = _project.addAnswerContact(_contact, _contact)
22
>>> _team = factory.makeTeam(owner=_contact, name='my-team')
23
>>> _team_project = factory.makeProduct(name='team-project', owner=_team)
24
>>> _asker = factory.makePerson(name='asker')
25
>>> _question_1 = factory.makeQuestion(
26
... target=_project, title="Q 1", owner=_asker)
27
>>> _question_2 = factory.makeQuestion(
28
... target=_project, title="Q 2", owner=_asker)
29
>>> _question_3 = factory.makeQuestion(
30
... target=_team_project, title="Q 3", owner=_asker)
33
>>> contact_webservice = webservice_for_person(
34
... _contact, permission=OAuthPermission.WRITE_PUBLIC)
40
Users can add or remove themselves as an answer contact for a project. The
41
user must have a preferred language. Scripts should call the
42
canUserAlterAnswerContact method first to verify that the person can
45
>>> project = contact_webservice.get(
46
... '/my-project', api_version='devel').jsonBody()
47
>>> contact = contact_webservice.get(
48
... '/~contact', api_version='devel').jsonBody()
49
>>> contact_webservice.named_get(
50
... project['self_link'], 'canUserAlterAnswerContact',
51
... person=contact['self_link'], api_version='devel').jsonBody()
54
>>> contact_webservice.named_post(
55
... project['self_link'], 'removeAnswerContact',
56
... person=contact['self_link'], api_version='devel').jsonBody()
59
>>> contact_webservice.named_post(
60
... project['self_link'], 'addAnswerContact',
61
... person=contact['self_link'], api_version='devel').jsonBody()
64
User can also make the teams they administer answer contacts if the team has a
67
>>> team = contact_webservice.get(
68
... '/~my-team', api_version='devel').jsonBody()
69
>>> contact_webservice.named_get(
70
... project['self_link'], 'canUserAlterAnswerContact',
71
... person=team['self_link'], api_version='devel').jsonBody()
74
>>> contact_webservice.named_post(
75
... team['self_link'], 'addLanguage',
76
... language='/+languages/fr', api_version='devel').jsonBody()
77
>>> contact_webservice.named_post(
78
... project['self_link'], 'addAnswerContact',
79
... person=team['self_link'], api_version='devel').jsonBody()
83
Anyone can get the collection of languages spoken by at least one
86
>>> languages = anon_webservice.named_get(
87
... project['self_link'], 'getSupportedLanguages',
88
... api_version='devel').jsonBody()
89
>>> print_self_link_of_entries(languages)
90
http://.../+languages/en
91
http://.../+languages/fr
93
>>> english = languages['entries'][0]
95
Anyone can retrieve the collection of answer contacts for a language.
97
>>> contacts = anon_webservice.named_get(
98
... project['self_link'], 'getAnswerContactsForLanguage',
99
... language=english['self_link'], api_version='devel').jsonBody()
100
>>> print_self_link_of_entries(contacts)
107
Anyone can retrieve a question from a `IQuestionTarget`.
109
>>> question_1 = anon_webservice.named_get(
110
... project['self_link'], 'getQuestion', question_id=_question_1.id,
111
... api_version='devel').jsonBody()
112
>>> print question_1['title']