Skip to content

Commit ac6a021

Browse files
committed
Merged bika_url_fetcher from bikalims/master
1 parent 0214f58 commit ac6a021

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

bika/lims/utils/__init__.py

+51-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
# -*- coding: utf-8 -*-
2-
from AccessControl import getSecurityManager
32
# This file is part of Bika LIMS
43
#
54
# Copyright 2011-2016 by it's authors.
65
# Some rights reserved. See LICENSE.txt, AUTHORS.txt.
76

87

9-
from AccessControl import ModuleSecurityInfo, allow_module
10-
11-
import math
8+
import os
9+
import re
10+
import tempfile
11+
import types
12+
import urllib2
13+
from email import Encoders
14+
from time import time
1215

16+
from AccessControl import ModuleSecurityInfo
17+
from AccessControl import allow_module
18+
from AccessControl import getSecurityManager
19+
from DateTime import DateTime
20+
from Products.Archetypes.public import DisplayList
21+
from Products.CMFCore.utils import getToolByName
22+
from Products.CMFPlone.utils import safe_unicode
23+
from bika.lims import api as api
1324
from bika.lims import logger
1425
from bika.lims.browser import BrowserView
15-
from DateTime import DateTime
16-
from email import Encoders
1726
from email.MIMEBase import MIMEBase
1827
from plone.memoize import ram
1928
from plone.registry.interfaces import IRegistry
20-
from Products.Archetypes.public import DisplayList
21-
from Products.CMFCore.utils import getToolByName
22-
from Products.CMFPlone.utils import safe_unicode
23-
from socket import timeout
24-
from time import time
25-
from weasyprint import HTML, CSS
29+
from weasyprint import CSS, HTML
30+
from weasyprint import default_url_fetcher
2631
from zope.component import queryUtility
2732
from zope.i18n import translate
2833
from zope.i18n.locales import locales
2934

30-
import App
31-
import Globals
32-
import os
33-
import re
34-
import tempfile
35-
import types
36-
import urllib2
37-
3835
ModuleSecurityInfo('email.Utils').declarePublic('formataddr')
3936
allow_module('csv')
4037

@@ -90,7 +87,7 @@ class js_err(BrowserView):
9087
def __call__(self, message):
9188
"""Javascript sends a string for us to place into the error log
9289
"""
93-
self.logger.error(message);
90+
self.logger.error(message)
9491

9592

9693
class js_warn(BrowserView):
@@ -117,6 +114,7 @@ def _cache_key_getUsers(method, context, roles=[], allow_empty=True):
117114
key = time() // (60 * 60), roles, allow_empty
118115
return key
119116

117+
120118
@ram.cache(_cache_key_getUsers)
121119
def getUsers(context, roles, allow_empty=True):
122120
""" Present a DisplayList containing users in the specified
@@ -134,6 +132,7 @@ def getUsers(context, roles, allow_empty=True):
134132
pairs.sort(lambda x, y: cmp(x[1], y[1]))
135133
return DisplayList(pairs)
136134

135+
137136
def isActive(obj):
138137
""" Check if obj is inactive or cancelled.
139138
"""
@@ -383,6 +382,33 @@ def isnumber(s):
383382
return False
384383

385384

385+
def bika_url_fetcher(url):
386+
"""Basically the same as the default_url_fetcher from WeasyPrint,
387+
but injects the __ac cookie to make an authenticated request to the resource.
388+
"""
389+
from weasyprint import VERSION_STRING
390+
from weasyprint.compat import Request
391+
from weasyprint.compat import urlopen_contenttype
392+
393+
request = api.get_request()
394+
__ac = request.cookies.get("__ac", "")
395+
396+
if request.get_header("HOST") in url:
397+
result, mime_type, charset = urlopen_contenttype(
398+
Request(url,
399+
headers={
400+
'Cookie': "__ac={}".format(__ac),
401+
'User-Agent': VERSION_STRING,
402+
'Authorization': request._auth,
403+
}))
404+
return dict(file_obj=result,
405+
redirected_url=result.geturl(),
406+
mime_type=mime_type,
407+
encoding=charset)
408+
409+
return default_url_fetcher(url)
410+
411+
386412
def createPdf(htmlreport, outfile=None, css=None, images={}):
387413
"""create a PDF from some HTML.
388414
htmlreport: rendered html
@@ -420,7 +446,7 @@ def createPdf(htmlreport, outfile=None, css=None, images={}):
420446

421447
# render
422448
htmlreport = to_utf8(htmlreport)
423-
renderer = HTML(string=htmlreport, encoding='utf-8')
449+
renderer = HTML(string=htmlreport, url_fetcher=bika_url_fetcher, encoding='utf-8')
424450
pdf_fn = outfile if outfile else tempfile.mktemp(suffix=".pdf")
425451
if css:
426452
renderer.write_pdf(pdf_fn, stylesheets=[CSS(string=css_def)])
@@ -527,7 +553,7 @@ def format_supsub(text):
527553
insubsup = True
528554
for c in text:
529555
if c == '(':
530-
if insubsup == False:
556+
if insubsup is False:
531557
out.append(c)
532558
clauses.append(')')
533559
else:
@@ -552,7 +578,7 @@ def format_supsub(text):
552578
continue
553579

554580
elif c == ' ':
555-
if insubsup == True:
581+
if insubsup is True:
556582
out.append(subsup.pop())
557583
else:
558584
out.append(c)
@@ -567,7 +593,7 @@ def format_supsub(text):
567593

568594
while True:
569595
if len(subsup) == 0:
570-
break;
596+
break
571597
out.append(subsup.pop())
572598

573599
return ''.join(out)

0 commit comments

Comments
 (0)