Login

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

Author:
shapiromatron
Posted:
October 29, 2015
Language:
Python
Version:
3.2
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

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.