A simple Django app to fetch and parse data from Dataporten.
Project description
Django-dataporten is a simple Django app to fetch data from dataporten and connect it to a user.
Setup
Add “dataporten” to your INSTALLED_APPS setting like this
INSTALLED_APPS = [
...
'dataporten',
]
Run python manage.py migrate to create the dataporten proxy models.
3. In your settings.py file, add the variable DATAPORTEN_TOKEN_FUNCTION, which
should be a dotted path to the function that will retrieve user tokens.
The function should accept a User
and return a str
.
Here is a python3.6/3.7 example that will work if you use django-allauth:
def allauth_token(user: User) -> str:
return SocialToken.objects.get(
account__user=user,
account__provider='dataporten',
).token
4. Add the dataporten middleware. This middleware adds a dataporten
attribute to request.user
for users with an associated
dataporten SocialToken
object. Take care to place it after
django.contrib.auth.middleware.AuthenticationMiddleware
.
MIDDLEWARE = (
# Other middleware...
'django.contrib.auth.middleware.AuthenticationMiddleware',
# Other middleware...
# Adds dataporten API information to request.user.dataporten
# Needs to be placed after any authentication middleware, as this
# requires inspection of request.user
'dataporten.middleware.DataportenGroupsMiddleware',
# Other middleware...
)
5. Optionally, enable caching for API queries. Take care to create the directory
set in DATAPORTEN_CACHE_PATH
before starting the Django server.
# Cache requests to the dataporten API
DATAPORTEN_CACHE_REQUESTS = True
# Where to save the sqlite3 cache backend
DATAPORTEN_CACHE_PATH = 'tmp/'
Usage
The DataportenGroupsMiddleware
adds an instance of
DataportenGroupsManager
saved to request.user.dataporten
for
every valid dataporten users.
You can check if a group is an active member of a specific dataporten group
by providing the group id
to the
DataportenGroupsManager.is_member_of
method. For instance,
assert request.user.dataporten.is_member_of(
uid='fc:org:ntnu.no:unit:167500',
active=True,
)
If active
is set to False
, the method only checks if the user
has been a member of the group at any time, not necessarily if the user is
an active member.
You can also check if a user has any affiliation to a course, only given its course code, and not its dataporten ID,
# Already finished the course
assert 'TMA4150' in request.user.dataporten.courses.finished
# Currently enrolled in the course
assert 'TMA4150' in request.user.dataporten.courses.active
# Either
assert 'TMA4150' in request.user.dataporten.courses.all
There is still lots of more undocumented (but well tested!) attributes of
DataportenGroupsManager
. Take a look at dataporten/parsers.py
.
Each parser has a class variable NAME
, and they are attached to
the user as request.user.dataporten.NAME
.
If you have a specific usecase, please open a GitHub issue, and I will document and/or implement it for you.
Run tests
export DJANGO_SETTINGS_MODULE=dataporten.settings
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_dataporten-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 242ab5a10d035c049591a313e0fa7761bdc3b8395b6c465ebfb7ac1876864ab8 |
|
MD5 | e3445fc01191d479e3db961b45c8ff68 |
|
BLAKE2b-256 | efe2f81f01e055b2f0d775427f76004d0685d8e952ef844087c573bb40336323 |