from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
import logging
logger = logging.getLogger('info_logger')
def fetch_pdf_resources(uri, rel):
if uri.find(settings.MEDIA_URL) != -1:
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ''))
elif uri.find(settings.STATIC_URL) != -1:
path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ''))
else:
path = None
logger.info('path TEST')
return path
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result, encoding='utf-8',
link_callback=fetch_pdf_resources)
logger.info(fetch_pdf_resources)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
2019-04-12 15:13:36,958 INFO 1234 ./blog/utils.py render_to_pdf 26 <function fetch_pdf_resources at 0x7f6dd3e57730>
LOGGING = {
'version':1,
'disable_existing_loggers': False,
'formatters':{
'large':{
'format':'%(asctime)s %(levelname)s %(process)d %(pathname)s %(funcName)s %(lineno)d %(message)s '
},
'tiny':{
'format':'%(asctime)s %(message)s '
}
},
'handlers':{
'info_file':{
'level':'INFO',
'class':'logging.handlers.TimedRotatingFileHandler',
'when':'midnight',
'interval':1,
'filename':'/home/django/pro/djangopt/logs/debug.log',
'formatter':'large',
},
},
'loggers':{
'info_logger':{
'handlers':['info_file'],
'level':'INFO',
'propagate':False,
},
},
}
*** Starting uWSGI 2.0.18 (64bit) on [Thu Apr 11 19:00:43 2019] ***
compiled with version: 5.4.0 20160609 on 11 April 2019 09:33:54
os: Linux-4.4.0-137-generic #163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018
nodename: cs547318
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/django/pro/djangopt
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 1800
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
Python version: 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609]
*** starting uWSGI Emperor ***
*** has_emperor mode detected (fd: 6) ***
[uWSGI] getting INI configuration from django.ini
*** Starting uWSGI 2.0.18 (64bit) on [Thu Apr 11 19:00:43 2019] ***
compiled with version: 5.4.0 20160609 on 11 April 2019 09:33:54
os: Linux-4.4.0-137-generic #163-Ubuntu SMP Mon Sep 24 13:14:43 UTC 2018
nodename: cs547318
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /etc/uwsgi/vassals
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/django/pro/djangopt
your processes number limit is 1800
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Python version: 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609]
Set PythonHome to /home/django/.djenv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1785e60
your mercy for graceful operations on workers is 60 seconds
*** Operational MODE: no-workers ***
spawned uWSGI master process (pid: 3190)
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xb1f370
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 801944 bytes (783 KB) for 10 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0xb1f370 pid: 3192 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3192)
Thu Apr 11 19:00:44 2019 - [emperor] vassal django.ini has been spawned
spawned uWSGI worker 1 (pid: 3194, cores: 1)
spawned uWSGI worker 2 (pid: 3195, cores: 1)
spawned uWSGI worker 3 (pid: 3196, cores: 1)
spawned uWSGI worker 4 (pid: 3197, cores: 1)
spawned uWSGI worker 5 (pid: 3198, cores: 1)
spawned uWSGI worker 6 (pid: 3199, cores: 1)
spawned uWSGI worker 7 (pid: 3200, cores: 1)
spawned uWSGI worker 8 (pid: 3201, cores: 1)
Thu Apr 11 19:00:44 2019 - [emperor] vassal django.ini is ready to accept requests
spawned uWSGI worker 9 (pid: 3202, cores: 1)
spawned uWSGI worker 10 (pid: 3203, cores: 1)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/home/django/pro/djangopt/log/debug.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
}
}
spawned uWSGI worker 6 (pid: 2655, cores: 1)
spawned uWSGI worker 7 (pid: 2656, cores: 1)
spawned uWSGI worker 8 (pid: 2657, cores: 1)
spawned uWSGI worker 9 (pid: 2658, cores: 1)
spawned uWSGI worker 10 (pid: 2659, cores: 1)
Thu Apr 11 18:24:21 2019 - [emperor] vassal django.ini is ready to accept requests
--- no python application found, check your startup logs for errors ---
[pid: 2652|app: -1|req: -1/1] 176.49.193.179 () {46 vars in 815 bytes} [Thu Apr 11 18:24:24 2019] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
announcing my loyalty to the Emperor...
Thu Apr 11 18:24:24 2019 - [emperor] vassal django.ini is now loyal
--- no python application found, check your startup logs for errors ---
[pid: 2650|app: -1|req: -1/2] 176.49.193.179 () {46 vars in 815 bytes} [Thu Apr 11 18:25:17 2019] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
announcing my loyalty to the Emperor...
Thu Apr 11 18:25:17 2019 - [emperor] vassal django.ini is now loyal
--- no python application found, check your startup logs for errors ---
[pid: 2650|app: -1|req: -1/3] 157.7.222.32 () {34 vars in 495 bytes} [Thu Apr 11 18:25:44 2019] GET /wp-content/iindex.php?zzz=S8lM1chNMdUwMdXUtAYA => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
--- no python application found, check your startup logs for errors ---
[pid: 2650|app: -1|req: -1/4] 157.7.222.32 () {34 vars in 509 bytes} [Thu Apr 11 18:26:33 2019] GET /wp-admin/iinstall-helper.php?zzz=S8lM1chNMdUwMdXUtAYA => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
--- no python application found, check your startup logs for errors ---
[pid: 2650|app: -1|req: -1/5] 185.18.149.3 () {34 vars in 507 bytes} [Thu Apr 11 18:26:42 2019] GET /wp-admin/images/iimages.php?zzz=S8lM1chNMdUwMdXUtAYA => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
--- no python application found, check your startup logs for errors ---
[pid: 2650|app: -1|req: -1/6] 157.7.222.32 () {34 vars in 501 bytes} [Thu Apr 11 18:27:18 2019] GET /wp-includes/ID3/IID3.php?zzz=S8lM1chNMdUwMdXUtAYA => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
--- no python application found, check your startup logs for errors ---
[pid: 2650|app: -1|req: -1/7] 157.7.222.32 () {34 vars in 509 bytes} [Thu Apr 11 18:27:20 2019] GET /wp-includes/fonts/ffonts.php?zzz=S8lM1chNMdUwMdXUtAYA => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (1 switches on core 0)
--- no python application found, check your startup logs for errors ---
# django.ini file
[uwsgi]
# master
master = true
# maximum number of processes
processes = 10
# the socket (use the full path to be safe)
socket = /tmp/uwsgi.sock
# with appropriate permissions - *may* be needed
chmod-socket = 666
# the base directory
chdir = /home/django/pro/djangopt
# Django's wsgi file
module = djangopt.wsgi
# the virtualenv
home = /home/django/.djenv
logto = /home/django/pro/djangopt/log/uwsgi.log
# clear environment on exit
vacuum = true
# reload uwsgi
touch-reload = /home/django/pro/djangopt/djangopt/settings.py
touch-reload = /home/django/pro/djangopt/djangopt/urls.py