Login

Middleware for fixing Microsoft Office (Word, Excel, Powerpoint) hyperlinks to views requiring authentication

Author:
shapiromatron
Posted:
October 29, 2015
Language:
Python
Version:
Not specified
Tags:
middleware office word excel powerpoint
Score:
0 (after 0 ratings)

Hyperlinks to views requiring authentication in Microsoft Office (Word, Excel, Powerpoint) can fail based on how Office handles rendering. This middleware sends a refresh return to the client, which will allow the page to be opened as normal, instead of the "Unable to open ... Cannot download the information you requested."

This is a port from the ruby middleware project fix microsoft links.

To enable, add to your middleware stack in the django project settings.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.http import HttpResponse
import re


class MicrosoftOfficeLinkMiddleware(object):
    # https://support.microsoft.com/en-us/kb/899927
    # https://github.com/spilliton/fix_microsoft_links

    OFFICE_AGENTS = re.compile(r"(Word|Excel|PowerPoint|ms-office)")
    OUTLOOK_AGENTS = re.compile(r"(Microsoft Outlook)")
    RESPONSE_TEXT = """<html><head><meta http-equiv="refresh" content="0"/></head><body></body></html>"""

    def process_request(self, request):
        agent = request.META.get("HTTP_USER_AGENT", "")
        if (re.findall(self.OFFICE_AGENTS, agent) and
           not re.findall(self.OUTLOOK_AGENTS, agent)):
                return HttpResponse(self.RESPONSE_TEXT)

More like this

Comments

Please login first before commenting.