I use a simple script to connect to a tryton server (better to use proteus for this):
import json
import requests
import configparser
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
config = configparser.ConfigParser()
config.read('tryton.cfg') # put a config file in your folder to store credentials
URL = config.get('tryton', 'URL')
USER = config.get('tryton', 'USER')
PASSWORD = config.get('tryton', 'PASSWORD')
class Tryton(object):
"""Client to make RPC requests to Tryton server.
"""
def __init__(self):
self._id = 0
def get_id(self):
self._id += 1
return self._id
def _login(self):
payload = json.dumps({
'params': [USER, {'password': PASSWORD}],
'jsonrpc': "2.0",
'method': 'common.db.login',
'id': 1,
})
headers = {'content-type': 'application/json'}
result = requests.post(
URL, data=payload, headers=headers, timeout=30, verify=False)
session = result.json()
return session
def execute(self, method, *params):
params = list(params) + [{}, ]
payload = json.dumps({
'params': params,
'method': method,
'id': self.get_id(),
})
headers = {'content-type': 'application/json'}
response = requests.post(
URL,
auth=(USER, PASSWORD),
data=payload,
headers=headers, verify=False)
result = response.json()
return result['result']
Then you can consume this with a little python script:
import json
from trytonrpc import Tryton # the script above
TC = Tryton()
# you can define a method in your tryton module to return the structure you need
# or you use one which is build in - eg search_read
res = TC.execute(
'model.your_module.your_class.search_read',
[]) # filter the data to your needs or write dedicated methods which return what you need
print(json.dumps(res)) # you must print this to stdout
And than you use this output in a php script (here it is placed in a folder the root of moodle - you must change paths to import if your stuff is placed elsewhere)
moodle_import.php:
<?php
/**
* Imports from Tryton
* PHP version 7
*
* @category Cli
* @package Moodle_Import
* @author Jan Grasnick <jan@mittelwind.de>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @link http://gitlab.internal
**/
if (php_sapi_name() === 'cli') {
define('CLI_SCRIPT', true);
}
global $CFG;
// the path to the moodle config
require_once(__DIR__ . '/../config.php');
// import the classes from moodle on which you will work
require_once $CFG->dirroot . '/lib/coursecatlib.php';
require_once $CFG->dirroot . '/course/lib.php';
require_once $CFG->dirroot . '/course/format/lib.php';
// make it a page if you want call it in the browser
$PAGE->set_context(context_system::instance());
$PAGE->set_title("Get data from Tryton");
$PAGE->set_heading("Get data from Tryton");
$PAGE->set_url('/tryton_moodle/moodle_import.php'); // the folder/name of your script
// require login if you do not run the script as cli
if (php_sapi_name() != 'cli') {
require_login();
is_siteadmin();
}
echo $OUTPUT->header();
global $DB;
$output = exec('/usr/bin/python get_courses.py'); // get data from your script above
$dict = json_decode($output);
foreach ($dict as $d) {
// now you have objects with all the keys/values you defined in tryton
$idnumber = $d->id;
// get an existing record
$hit= $DB->get_record('course_categories', array('idnumber' =>$idnumber));
$create = false;
// create a new object to put values on
$cat = new stdClass();
// add the attributes from tryton to the objects in moodle
$cat->description = $d->description;
$cat->name = $d->rec_name;
$cat->idnumber=$idnumber;
if (!$hit) {
//create new course cat
coursecat::create($cat);
} else {
// get the object and update it
$uid = $hit->id;
$cat->id = $uid;
$hit = coursecat::get($uid, MUST_EXIST, true);
$hit->update($cat)
}
// do the same with lectures, add enroles, add teachers or what ever comes from your tryton models
echo $OUTPUT->footer();
?>
This scripts are all placed in a folder in the root of moodle.