1

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()
Martin Thorsen Ranang
  • 2,394
  • 1
  • 28
  • 43

1 Answers1

1

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).

Dag Høidahl
  • 7,873
  • 8
  • 53
  • 66
  • 1
    That looks useful. I'll look into it, but after a first glance on Curling's requirements, it seems to require an awful lot for a client library, including Django itself and a PyJWT-mozilla, which in turn requires M2Crypto that depends on Swig during its build. – Martin Thorsen Ranang Nov 07 '14 at 08:01
  • And, even if `by_url()` looks nice, slumber already supports the syntax `api.get('/api/v1/projects/10/').get()`, which in my example above would correspond to `api.get(new_project['resource_uri']).get()`. – Martin Thorsen Ranang Nov 07 '14 at 08:08
  • Actually, I've now discovered that the slumber API object accepts any attribute name, so even `api.by_uri('/api/v1/projects/10/').get()` works. ;) – Martin Thorsen Ranang Nov 07 '14 at 09:25
  • Agreed, those are some heavy requirements. – Dag Høidahl Nov 07 '14 at 09:33
  • After wasting too many hours on trying to get Pootle behave appropriately, I guess the answer is really to use another translation server, like [Weblate](http://weblate.org/). – Martin Thorsen Ranang Nov 11 '14 at 09:47