1
1
# -*- coding: utf-8 -*-
2
- from AccessControl import getSecurityManager
3
2
# This file is part of Bika LIMS
4
3
#
5
4
# Copyright 2011-2016 by it's authors.
6
5
# Some rights reserved. See LICENSE.txt, AUTHORS.txt.
7
6
8
7
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
12
15
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
13
24
from bika .lims import logger
14
25
from bika .lims .browser import BrowserView
15
- from DateTime import DateTime
16
- from email import Encoders
17
26
from email .MIMEBase import MIMEBase
18
27
from plone .memoize import ram
19
28
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
26
31
from zope .component import queryUtility
27
32
from zope .i18n import translate
28
33
from zope .i18n .locales import locales
29
34
30
- import App
31
- import Globals
32
- import os
33
- import re
34
- import tempfile
35
- import types
36
- import urllib2
37
-
38
35
ModuleSecurityInfo ('email.Utils' ).declarePublic ('formataddr' )
39
36
allow_module ('csv' )
40
37
@@ -90,7 +87,7 @@ class js_err(BrowserView):
90
87
def __call__ (self , message ):
91
88
"""Javascript sends a string for us to place into the error log
92
89
"""
93
- self .logger .error (message );
90
+ self .logger .error (message )
94
91
95
92
96
93
class js_warn (BrowserView ):
@@ -117,6 +114,7 @@ def _cache_key_getUsers(method, context, roles=[], allow_empty=True):
117
114
key = time () // (60 * 60 ), roles , allow_empty
118
115
return key
119
116
117
+
120
118
@ram .cache (_cache_key_getUsers )
121
119
def getUsers (context , roles , allow_empty = True ):
122
120
""" Present a DisplayList containing users in the specified
@@ -134,6 +132,7 @@ def getUsers(context, roles, allow_empty=True):
134
132
pairs .sort (lambda x , y : cmp (x [1 ], y [1 ]))
135
133
return DisplayList (pairs )
136
134
135
+
137
136
def isActive (obj ):
138
137
""" Check if obj is inactive or cancelled.
139
138
"""
@@ -383,6 +382,33 @@ def isnumber(s):
383
382
return False
384
383
385
384
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
+
386
412
def createPdf (htmlreport , outfile = None , css = None , images = {}):
387
413
"""create a PDF from some HTML.
388
414
htmlreport: rendered html
@@ -420,7 +446,7 @@ def createPdf(htmlreport, outfile=None, css=None, images={}):
420
446
421
447
# render
422
448
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' )
424
450
pdf_fn = outfile if outfile else tempfile .mktemp (suffix = ".pdf" )
425
451
if css :
426
452
renderer .write_pdf (pdf_fn , stylesheets = [CSS (string = css_def )])
@@ -527,7 +553,7 @@ def format_supsub(text):
527
553
insubsup = True
528
554
for c in text :
529
555
if c == '(' :
530
- if insubsup == False :
556
+ if insubsup is False :
531
557
out .append (c )
532
558
clauses .append (')' )
533
559
else :
@@ -552,7 +578,7 @@ def format_supsub(text):
552
578
continue
553
579
554
580
elif c == ' ' :
555
- if insubsup == True :
581
+ if insubsup is True :
556
582
out .append (subsup .pop ())
557
583
else :
558
584
out .append (c )
@@ -567,7 +593,7 @@ def format_supsub(text):
567
593
568
594
while True :
569
595
if len (subsup ) == 0 :
570
- break ;
596
+ break
571
597
out .append (subsup .pop ())
572
598
573
599
return '' .join (out )
0 commit comments