Skip to content

Known User Implementation (v.3.x) for Python (Django etc.)

License

Notifications You must be signed in to change notification settings

queueit/KnownUser.V3.Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 

Repository files navigation

KnownUser.V3.Python

Before getting started please read the documentation to get acquainted with server-side connectors.

Connector was developed and verified with Python v.2.7 and Django v.1.8. Please reach out to us if you are using another web framework, so we add it to the supported providers.

You can find the latest released version here

Implementation

The KnownUser validation must be done on all requests except requests for static and cached pages, resources like images, css files and .... So, if you add the KnownUser validation logic to a central place, then be sure that the Triggers only fire on page requests (including ajax requests) and not on e.g. image.

If we have the integrationconfig.json copied in the folder beside other knownuser files inside web application folder then the following method (using Django v.1.8) is all that is needed to validate that a user has been through the queue:

from django.http import HttpResponse

import django
import sys
import re

from queueit_knownuserv3.http_context_providers import Django_1_8_Provider
from queueit_knownuserv3.models import QueueEventConfig
from queueit_knownuserv3.known_user import KnownUser


def index(request):
    try:
        with open('integrationconfiguration.json', 'r') as myfile:
            integrationsConfigString = myfile.read()

        customerId = "" # Your Queue-it customer ID
        secretKey = "" # Your 72 char secret key as specified in Go Queue-it self-service platform

        response = HttpResponse()
        httpContextProvider = Django_1_8_Provider(request, response)
        requestUrl = httpContextProvider.getOriginalRequestUrl()
        requestUrlWithoutToken = re.sub(
            "([\\?&])(" + KnownUser.QUEUEIT_TOKEN_KEY + "=[^&]*)",
            '',
            requestUrl,
            flags=re.IGNORECASE)
        # The requestUrlWithoutToken is used to match Triggers and as the Target url (where to return the users to).
        # It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
        # behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.

        queueitToken = request.GET.get(KnownUser.QUEUEIT_TOKEN_KEY)

        validationResult = KnownUser.validateRequestByIntegrationConfig(
            requestUrlWithoutToken, queueitToken, integrationsConfigString,
            customerId, secretKey, httpContextProvider)

        if (validationResult.doRedirect()):
            response["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
            response["Pragma"] = "no-cache"
	    response["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"

            if (not validationResult.isAjaxResult):
                response.status_code = 302
                response["Location"] = validationResult.redirectUrl                                
            else:
                headerKey = validationResult.getAjaxQueueRedirectHeaderKey()
                response[headerKey] = validationResult.getAjaxRedirectUrl()
		response["Access-Control-Expose-Headers"] = validationResult.getAjaxQueueRedirectHeaderKey()
        else:
            # Request can continue, we remove queueittoken from url to avoid sharing of user specific token
            if (requestUrl != requestUrlWithoutToken and validationResult.actionType == "Queue"):
                response.status_code = 302
                response["Location"] = requestUrlWithoutToken                
            
        return response

    except StandardError as stdErr:
        # There was an error validating the request
        # Use your own logging framework to log the error
        # This was a configuration error, so we let the user continue
        print stdErr.message        

Implementation using inline queue configuration

Specify the configuration in code without using the Trigger/Action paradigm. In this case it is important only to queue-up page requests and not requests for resources. This can be done by adding custom filtering logic before caling the KnownUser.resolveQueueRequestByLocalConfig() method.

The following is an example (using Django v.1.8) of how to specify the configuration in code:

from django.http import HttpResponse

import django
import sys
import re

from queueit_knownuserv3.http_context_providers import Django_1_8_Provider
from queueit_knownuserv3.models import QueueEventConfig
from queueit_knownuserv3.known_user import KnownUser


def index(request):
    try:
        
        customerId = "" # Your Queue-it customer ID
        secretKey = "" # Your 72 char secret key as specified in Go Queue-it self-service platform

	queueConfig = QueueEventConfig()
	queueConfig.eventId = "" # ID of the queue to use
	queueConfig.queueDomain = "xxx.queue-it.net" #Domain name of the queue.
	# queueConfig.cookieDomain = ".my-shop.com" #Optional - Domain name where the Queue-it session cookie should be saved
	queueConfig.cookieValidityMinute = 15 #Validity of the Queue-it session cookie should be positive number.
	queueConfig.extendCookieValidity = true #Should the Queue-it session cookie validity time be extended each time the validation runs?
	# queueConfig.culture = "da-DK" #Optional - Culture of the queue layout in the format specified here: https://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx.  If unspecified then settings from Event will be used.
	# queueConfig.layoutName = "NameOfYourCustomLayout" #Optional - Name of the queue layout. If unspecified then settings from Event will be used.
	response = HttpResponse()
        httpContextProvider = Django_1_8_Provider(request, response)
        requestUrl = httpContextProvider.getOriginalRequestUrl()
        requestUrlWithoutToken = re.sub(
            "([\\?&])(" + KnownUser.QUEUEIT_TOKEN_KEY + "=[^&]*)",
            '',
            requestUrl,
            flags=re.IGNORECASE)
        # The requestUrlWithoutToken is used to match Triggers and as the Target url (where to return the users to).
        # It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
        # behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.

        queueitToken = request.GET.get(KnownUser.QUEUEIT_TOKEN_KEY)

	validationResult = KnownUser.resolveQueueRequestByLocalConfig(
            requestUrlWithoutToken, queueitToken, queueConfig, customerId, secretKey,
            httpContextProvider)

        if (validationResult.doRedirect()):
            response["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
            response["Pragma"] = "no-cache"
	    response["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
	    
	     if (not validationResult.isAjaxResult):
                response.status_code = 302
                response["Location"] = validationResult.redirectUrl                                
            else:
                headerKey = validationResult.getAjaxQueueRedirectHeaderKey()
                response[headerKey] = validationResult.getAjaxRedirectUrl()   
                response["Access-Control-Expose-Headers"] = validationResult.getAjaxQueueRedirectHeaderKey()  
        else:
            # Request can continue, we remove queueittoken from url to avoid sharing of user specific token
            if (requestUrl != requestUrlWithoutToken and validationResult.actionType == "Queue"):
                response.status_code = 302
                response["Location"] = requestUrlWithoutToken
            
	return response

    except StandardError as stdErr:
        # There was an error validating the request
        # Use your own logging framework to log the error
        # This was a configuration error, so we let the user continue
        print stdErr.message

Request body trigger (advanced)

The connector supports triggering on request body content. An example could be a POST call with specific item ID where you want end-users to queue up for. For this to work, you will need to contact Queue-it support or enable request body triggers in your integration settings in your GO Queue-it platform account. Once enabled you will need to update your integration so request body is available for the connector.
You need to create a new context provider similar to this one:

from queueit_knownuserv3.http_context_providers import Django_1_8_Provider


class Django_1_8_Provider_WithBody(Django_1_8_Provider):
    def __init__(self, request, response):
        super(Django_1_8_Provider_WithBody, self).__init__(request, response)

    def getRequestBodyAsString(self):
        return self.request.body

And then use it instead of Django_1_8_Provider

# The default Django_1_8_Provider class always returns empty string as request body.
# So we use Django_1_8_Provider_WithBody in order to read the body.
# If you don't need to read the request body you may use Django_1_8_Provider
# http_context_provider = Django_1_8_Provider(request, response)
http_context_provider = Django_1_8_Provider_WithBody(request, response)

About

Known User Implementation (v.3.x) for Python (Django etc.)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages