Working with Launchpad Answers over the API =========================================== Users can work with question targets and questions over the api to search and update questions. This demonstration will use a project, it's contact, and asker, and three questions. >>> from zope.component import getUtility >>> from canonical.launchpad.testing.pages import webservice_for_person >>> from canonical.launchpad.webapp.interfaces import OAuthPermission >>> from lp.app.enums import ServiceUsage >>> from lp.services.worlddata.interfaces.language import ILanguageSet >>> from lp.testing.sampledata import ADMIN_EMAIL >>> lang_set = getUtility(ILanguageSet) >>> login(ADMIN_EMAIL) >>> _contact = factory.makePerson(name='contact') >>> _project = factory.makeProduct(name='my-project', owner=_contact) >>> _contact.addLanguage(lang_set['en']) >>> _project.answers_usage = ServiceUsage.LAUNCHPAD >>> success = _project.addAnswerContact(_contact, _contact) >>> _team = factory.makeTeam(owner=_contact, name='my-team') >>> _team_project = factory.makeProduct(name='team-project', owner=_team) >>> _asker = factory.makePerson(name='asker') >>> _question_1 = factory.makeQuestion( ... target=_project, title="Q 1", owner=_asker) >>> _question_2 = factory.makeQuestion( ... target=_project, title="Q 2", owner=_asker) >>> _question_3 = factory.makeQuestion( ... target=_team_project, title="Q 3", owner=_asker) >>> logout() >>> contact_webservice = webservice_for_person( ... _contact, permission=OAuthPermission.WRITE_PUBLIC) Answer contacts --------------- Users can add or remove themselves as an answer contact for a project. The user must have a preferred language. Scripts should call the canUserAlterAnswerContact method first to verify that the person can be added. >>> project = contact_webservice.get( ... '/my-project', api_version='devel').jsonBody() >>> contact = contact_webservice.get( ... '/~contact', api_version='devel').jsonBody() >>> contact_webservice.named_get( ... project['self_link'], 'canUserAlterAnswerContact', ... person=contact['self_link'], api_version='devel').jsonBody() True >>> contact_webservice.named_post( ... project['self_link'], 'removeAnswerContact', ... person=contact['self_link'], api_version='devel').jsonBody() True >>> contact_webservice.named_post( ... project['self_link'], 'addAnswerContact', ... person=contact['self_link'], api_version='devel').jsonBody() True User can also make the teams they administer answer contacts if the team has a preferred language. >>> team = contact_webservice.get( ... '/~my-team', api_version='devel').jsonBody() >>> contact_webservice.named_get( ... project['self_link'], 'canUserAlterAnswerContact', ... person=team['self_link'], api_version='devel').jsonBody() True >>> contact_webservice.named_post( ... team['self_link'], 'addLanguage', ... language='/+languages/fr', api_version='devel').jsonBody() >>> contact_webservice.named_post( ... project['self_link'], 'addAnswerContact', ... person=team['self_link'], api_version='devel').jsonBody() True Anyone can get the collection of languages spoken by at least one answer contact. >>> languages = anon_webservice.named_get( ... project['self_link'], 'getSupportedLanguages', ... api_version='devel').jsonBody() >>> print_self_link_of_entries(languages) http://.../+languages/en http://.../+languages/fr >>> english = languages['entries'][0] Anyone can retrieve the collection of answer contacts for a language. >>> contacts = anon_webservice.named_get( ... project['self_link'], 'getAnswerContactsForLanguage', ... language=english['self_link'], api_version='devel').jsonBody() >>> print_self_link_of_entries(contacts) http://.../~contact Questions --------- Anyone can retrieve a question from a `IQuestionTarget`. >>> question_1 = anon_webservice.named_get( ... project['self_link'], 'getQuestion', question_id=_question_1.id, ... api_version='devel').jsonBody() >>> print question_1['title'] Q 1