Skip to content

A very simple example

Espen Angell Kristiansen edited this page Oct 19, 2013 · 6 revisions

A very simple example (explained in detail below):

from devilryrestfullib import login, RestfulFactory
from pprint import pprint # Just like ``print``, but pretty format the output

devilry_url = 'https://devilry.example.com'

# Log in
login_url = '{0}/authenticate/login'.format(devilry_url)
logincookie = login(login_url, username='myuser', password='secret')

# Load AssignmentGroup APIs for student, examiner and admins
restful_factory = RestfulFactory(devilry_url)
StudentSimplifiedAssignmentGroup = restful_factory.make('/student/restfulsimplifiedassignmentgroup/')
ExaminerSimplifiedAssignmentGroup = restful_factory.make('/examiner/restfulsimplifiedassignmentgroup/')
AdministratorSimplifiedAssignmentGroup = restful_factory.make('/administrator/restfulsimplifiedassignmentgroup/')

# Search for AssignmentGroups that we have access to as student, examiner and admin
pprint(StudentSimplifiedAssignmentGroup.search(logincookie))
pprint(ExaminerSimplifiedAssignmentGroup.search(logincookie))
pprint(AdministratorSimplifiedAssignmentGroup.search(logincookie))

# Search for AssignmentGroups matching 'superman'. This is the same as writing
# text into the search field in the website. It matches any AssignmentGroup related to
# superman (I.E.: examiner usernames, subject names, student usernames, candidate IDs, ...)
pprint(AdministratorSimplifiedAssignmentGroup.search(logincookie, query='superman', limit=5))

# More precise search using filters.
# This filter searches for any AssignmentGroup within the period fall2011
# within the duck1080 subject.
pprint(AdministratorSimplifiedAssignmentGroup.search(logincookie,
    filters=[
        {'field': 'parentnode__parentnode__parentnode__short_name',
         'comp': 'exact',
         'value': 'duck1080'},
        {'field': 'parentnode__parentnode__short_name',
         'comp': 'exact',
         'value': 'fall2011'}
    ]
))

Example explained

Log in

The URL and parameters depends on the method used to authenticate users at your institution. This example is correct if you host devilry at https://devilry.example.com, and login using the default authentication method provided by Devilry. For local developer deployments of Devilry, the URL is usually http://localhost:8000.

The login method submits a normal HTTP POST request, so any authentication portal using this method should work with the method. You only have to send the correct name/value pairs for each form field, and the correct URL. For example, at UiO, we use:

devilry_url = 'https://devilry.ifi.uio.no'
login_url = '{0}/login'.format(devilry_url)
logincookie = login(login_url, user='myuser', password='secret')

(notice that we use user instead of username because the login form has slightly different naming than the form used by default in Devilry)

Load APIs

restful_factory.make(...) returns a Python-wrapper for the Official API documentation for the RESTful API. In this example we load the API for AssignmentGroup for student, examiner and administrator.

Use the APIs

We only show how to perform search in this example.

Clone this wiki locally