Integrate with Django
The sym.django
module provides a simple request decorator which wraps your view that would otherwise establish a new user impersonation session (e.g. by setting a cookie).
When a user visits the Sym-wrapped page, they will be presented with a screen with a form to fill out (based on the fields specified in your sym_flow
definition. Upon submitting this form, an event
will be sent to the Sym API, which will trigger your flow
.


Setup
We start with some boilerplate imports.
from django.views.decorators.http import require_http_methods
from sym.django import sym_init, require_approval
from .models import Account
Next, we initialize Sym with the API token provided in your dashboard.
sym_init(token=ENV['SYM_TOKEN'])
Request Data
We create a function, build_sym_request_data
, which collects the information to be passed on to Sym when a user makes a request.
def build_sym_request_data(request):
account_id = request.query_params["account_id"]
account = Account.objects.get(id=account_id)
user_is_assigned = request.user.id in account.managers
return {
"customer_id": account_id,
"customer_plan": account.plan,
"is_assigned": user_is_assigned,
}
Wrap the Route
Finally, we wrap our impersonation route with the Sym decorator.
@require_approval(
flow="user_impersonation",
build_data=build_sym_request_data,
message="You must obtain approval to impersonate this user.",
)
@require_http_methods(["GET", "POST"])
def impersonation(request):
# User impersonation logic here
...
Updated over 1 year ago