forked from iamkroot/erp-gcal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcms.py
59 lines (39 loc) · 1.35 KB
/
cms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from functools import lru_cache
import requests
from utils import config, pprint_json
REST_URL = config['MOODLE']['address'] + "/webservice/rest/server.php"
def make_req(verb, params={}, data=None):
query_params = {
"wstoken": config['MOODLE']['wstoken'],
"moodlewsrestformat": "json",
}
query_params.update(params)
resp = requests.request(verb, REST_URL, params=query_params, data=data)
resp = resp.json()
if 'exception' in resp:
raise Exception('CMS error: ' + resp['message'])
return resp
def get(wsfunc, params={}):
params['wsfunction'] = wsfunc
return make_req('get', params)
def post(wsfunc, params={}, data={}):
params['wsfunction'] = wsfunc
return make_req('post', params, data)
@lru_cache()
def get_siteinfo():
return get('core_webservice_get_site_info')
@lru_cache()
def get_userid():
return get_siteinfo()['userid']
@lru_cache()
def get_enrolled_courses():
return get('core_enrol_get_users_courses', {'userid': get_userid()})
def search_course(name):
data = get('core_course_search_courses',
{'criterianame': 'search', 'criteriavalue': name})
if data['total']:
return data['courses'][0]
def enrol(courseid):
return post('enrol_self_enrol_user', data={'courseid': courseid})
if __name__ == '__main__':
pprint_json(get_enrolled_courses())