Basic features used in all PSU Django apps
Project description
PSU-Base
Django add-on for PSU apps which includes common utilities, such as:
- PSU Single Sign-On (SSO)
- Reference to static content server (e.g. for Bootstrap, FontAwesome4, and JQuery)
- Displaying the name and version of the app
More documentation in Confluence.
Quick Start
Dependencies
The following dependencies are REQUIRED and must be installed manually in your app:
django-cas-ng
andcrequest
pip install django-cas-ng pip install crequest
Add to your app'ssettings.py
INSTALLED_APPS = [ ... 'django_cas_ng', 'crequest', ]
The following dependencies are REQUIRED in your system:
libpq-dev
sudo apt install libpq-dev
Installation
Inside your Django app (we recommend you use a virtual environment)
-
Set up your virtual envionment:
cd my_django_project # Create a virtual environment directory 'env' python -m venv env
-
Until PSU-Base is hosted somewhere, for now you can install PSU-Base to your app as follows:
-
Clone this repository
git clone git@github.com:PSU-OIT-ARC/django-psu-base.git
-
Build the thing
cd django-psu-base python setup.py sdist bdist_wheel
This will build two files under
django-psu-base/dist/
.psu-base-0.1.tar.gz
psu_base-0.1-py3-none-any.whl
Note that
0.1
will change according to the version. -
Copy either the
.tar.gz
or.whl
file to your appcp dist/psu-base-0.1.tar.gz path/to/your/app
OR
cp dist/psu_base-0.1-py3-none-any.whl path/to/your/app
-
cd
into your app and install PSU-Basecd my_django_project source env/bin/activate # make sure virtual environment is active pip install psu-base-0.1.tar.gz
OR
pip install psu_base-0.1-py3-none-any.whl
You get the idea...
-
-
(Once published to PyPi or PSU-hosted PyPi/Poetry (to be done soon)...) Install PSU-Base:
# First, activate the virtual environment source env/bin/activate # with the environment activated... pip install django-psu-base
Configuring Your App
-
Add PSU-Base to your INSTALLED_APPS in
settings.py
:INSTALLED_APPS = [ ... 'django_cas_ng', 'crequest', 'psu_base', ]
-
Copy
docs/app_settings_template.py
to your app, name itapp_settings.py
and update the values as needed. -
Copy
docs/local_settings_template.py
to your app, name itlocal_settings.py
and update the values as needed. -
Import your app and local settings files at the end of your settings.py file:
settings.py
# Get app-specific settings
from .app_settings import *
# Override settings with values for the local environment
from .local_settings import *
-
Run
python manage.py migrate
to create the PSU-Base models -
Configure your app's top-level
urls.py
to include urls with PSU namespace# my_app/urls.py from django.conf.urls import url from django.urls import path, include ... urlpatterns = [ ... # PSU and CAS views are defined in psu_base app url(settings.URL_CONTEXT+'/psu/', include(('psu_base.urls', 'psu_base'), namespace='psu')), url(settings.URL_CONTEXT+'/accounts/', include(('psu_base.urls', 'psu_base'), namespace='cas')), ]
Using PSU-Base in Your App
Template
- Extend the
psu_base.html
for your base template. This will add a 'PSU' header and footer. The header will include links to login to PSU SSO. Use the blockpagecontent
to add your own content. E.g. in your app'sindex.html
:{% extends 'psu_base.html' %} {% block pagecontent %} <h1>Hello PSU Base!</h1> {% endblock %}
Templatetags
-
Identity:
{% load base_taglib %} {% display_name %} <!-- displays user name when logged in -->
-
Static content:
{% load base_taglib %} <head> ... {% jquery %} <!-- import jquery --> {% bootstrap %} <!-- import bootstrap --> {% font_awesome version='4' %} <!-- import FontAwesome --> </head>
-
Utilities:
{% load base_taglib %} {% app_name %} <!-- display app name --> {% app_version %} <!-- display app version -->
PSU-Base Classes
-
Log - provide logging interface (Confluence docs)
... # other imports from psu_base.classes.Log import Log log = Log() def method(param1, param2): log.trace([param1, param2]) ... # do something try ... catch Exception as e: log.error(e) log.end(returned_value) # Will log some metrics e.g. call duration if log.trace() was called first
-
Finti - helper/wrapper to handle JWT transactions in Finti API calls Confluence docs
NOTE: You must have
FINTI_URL
andFINTI_TOKEN
configured for your app inlocal_settings.py
.There are two methods available to make Finti calls:
-
jwt_get(self, request, path=None, parameters=None, include_metadata=False)
Use to call Finti APIs that use Javascript Web Tokens (JWT). When using this method, you must supply the JWT in the request META attribute:
request.META['HTTP_AUTHORIZATION']
-
get(self, path, parameters=None, include_metadata=False)
Use to call Finti APIs that do not use JWT. You must supply a
FINTI_TOKEN
in thelocal_settings.py
file.-
path
: the API URI path without the Finti base URL i.e.<scope>/<version>/<app_name>/[endpoint/args/querystrings]
For example:
wdt/v1/buildings/AB
. Where:wdt
is scope,v1
is version,buildings
is app_name,AB
is an endpoint.
-
-
Non-JWT Finti APIs have different response format; see: Finti Docs. The
django-psu-base
Finti module wraps these reseponses into the format used by the Finti APIs which use JWT as shown below.The original response content is placed in the
'message'
attribute without modifications. For these kind of response content, the values for'version'
and'jwt'
are alwaysNone
. Responses from APIs that use JWT are returned as-is.Displays returned response with format:
{ 'status': ..., 'result': ..., 'version': ..., 'jwt': ..., 'message': ... }
status
: HTTP status coderesult
: description of status code e.g. 200 ='success'
version
: API versionjwt
: JWT token (when available)message
: information/data requested or error message
If PSU-Base urls were added to the app (see Configuring Your App step 4), you can test calling Finti APIs from the testing page at URL:
base/finti
-
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.