简体   繁体   中英

Creating and updating a Pootle project via REST API with Slumber

Does anyone have a working example of how to create and update a Pootle project and its template strings via Pootle's Tastypie -based REST API? I would really like to see a minimal, elegant way to do this with, for example, Slumber .

My case: I would like to create several Pootle projects from my Web application. You can think of each such project as corresponding to one article in a collection of articles in a Web service. For each of these projects, I would need to be able to

  1. create it initially with a list of extracted (template) strings (through the API),
  2. provide actual translations (human, through Pootle's Web interface [ not a problem]),
  3. retrieve translations, ideally as PO file through the API, and
  4. update the set of translatable strings (through API), so that a human can perform more translations.

I've read Pootle's Glossary , API definition , its API usage notes , the Tastypie documentation , and the Slumber documentation , but feel like there's a part I'm missing. For example, Tastypie offer great options to specify filtering parameters in the request URL, but I get the feeling I have to retrieve the whole list of projects to search for the right one in the app, which makes me wonder how others are using the API.

The following code correctly creates a new project:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import slumber

api = slumber.API('http://localhost:8000/api/v1/', auth=('admin', 'admin'))

project_data = {
    'code': 'test01',
    'fullname': 'Test #01',
    'description': 'Another test.',
    'source_language': '/api/v1/languages/2/',
    'translation_projects': [],
}

new_project = api.projects.post(project_data)

And new_project refers to the following dict:

{'backlink': 'http://localhost:8000/projects/test01/',
 'checkstyle': 'standard',
 'code': 'test01',
 'description': '<p>Another test.</p>',
 'fullname': 'Test #01',
 'ignoredfiles': u'',
 'localfiletype': 'po',
 'resource_uri': '/api/v1/projects/10/',
 'source_language': '/api/v1/languages/2/',
 'translation_projects': [],
 'treestyle': 'auto'}

Given that I keep, for example, the code value for later. What is the most efficient way of retrieving the project's id (which is 10 , from the resource_uri )? The reason I want that, is for later requests, like:

api.projects(10).get()

Have you checked out Curling ? It is a library wrapping Slumber, that specifically targets Django Tastypie APIs. It provides methods like by_url() that translates URLs like /generic/transaction/8/ to generic.transaction(8) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM