Package: django-xero
Django-XeroXerointegration for Django.FeaturesThis package allows users to authenticate to Xero via a Public App that you have
registered on theXero Developer Portal.Whenever you want to trigger authentication with Xero, just redirect to the viewxero_auth_start(either directly or viaresolve('xero-auth-start')and this package
will do the rest.
The view also accepts an optionalnextparameter to control where the user will land
on successful authentication; otherwise it will redirect to/.Xero sessions last for 30 minutes, but if you want to end them earlier, POST to
the viewxero_logout(again with an optionalnext).There is also a@xero_requireddecorator for views, which will automatically
check if an active Xero session is present. If not, it will redirect to an interstitial page
asking the user to do the authentication dance. You can control that page by creating a
custom templatexero/interstitial.html(make sure it has a link toxero-auth-startsomewhere).Once authorized, in your view you will get a.xerouserattribute which you can use to do stuff like:fromdjxero.decoratorsimportxero_required@login_required@xero_requireddefmy_view(request):client=request.user.xerouser.clientcontacts=client.contacts.all()...That client is a preconfiguredxero.Xeroobject frompyxero.Some details will be exposed inUserinstances under.xerouser, but the Xero mechanisms
are such that it's all pretty meaningless - there is currently no way to know anything about the user
from the xero session alone. For that reason, note thatyou must have some other registration mechanism to create a regular Django
user first(e.g. regular login page with some other auth system); this package only extends
thatUserinstance to attach a temporary Xero session.If your User model has the same email or firstname/lastname as recorded in Xero,
you can try to guess its details withrequest.user.xerouser.guess_user()and then manually save the ID
inrequest.user.xerouser.xero_id, but the heuristic is very basic and may or may not work.Supported PlatformsPython 3.7 (should work on 3.5/3.6 too, but is untested).Django 2Requirementsdjango-xero should automatically download all necessary prerequisites, but I like
to give credit where credit is due.
We stand on the shoulders of the following giants:django-encrypted-model-fieldspyxeroInstallationInstall the packagepip install django-xeroAddencrypted_model_fieldsanddjxerotoINSTALLED_APPSin your settings.
Note that the authentication system is required.INSTALLED_APPS=['django.contrib.auth','django.contrib.sessions',...'encrypted_model_fields','djxero',...]Generate an encryption key:python-c'import base64; import os; print(base64.urlsafe_b64encode(os.urandom(32)))'and add it to your settings. NOTE: in production, you want to store this safely.FIELD_ENCRYPTION_KEY=b'A7c4T1Kx3XmttUjm2cX8ScYcUEdF7RzFziEzfoBO7x4='Run migrations to create the necessary objectspythonmanage.pymigratedjxeroSet the Xero secrets for your public app. You can do this from the admin site (easier),
or directly viamanage.py shell:fromdjxero.modelsimportXeroSecretconsumer_key=XeroSecret.objects.get(pk='xero_consumer_key')consumer_key.value='your key'consumer_key.save()consumer_secret=XeroSecret.objects.get(pk='xero_consumer_secret')consumer_secret.value='your secret'consumer_secret.save()in your projecturls.py, add the following:urlpatterns=[path('xero/',include('djxero.urls')),...]IssuesFor problems, file an issue onGitHub.
The author is available for hire (hint hint).Credits and LicenseCopyright (c) 2019Giacomo Lacava.Released under the terms of the Apache Software License, version 2.