|
|
|
@ -1,9 +1,14 @@ |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
import os |
|
|
|
|
|
|
|
import glob |
|
|
|
from datetime import datetime |
|
|
|
from datetime import datetime |
|
|
|
|
|
|
|
import tempfile |
|
|
|
from email.header import Header |
|
|
|
from email.header import Header |
|
|
|
from time import time |
|
|
|
from time import time |
|
|
|
import simplejson as json |
|
|
|
import simplejson as json |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import ghostscript |
|
|
|
|
|
|
|
|
|
|
|
from django.shortcuts import render, get_object_or_404, redirect |
|
|
|
from django.shortcuts import render, get_object_or_404, redirect |
|
|
|
from django.http import HttpResponseServerError, HttpResponseBadRequest, HttpResponse |
|
|
|
from django.http import HttpResponseServerError, HttpResponseBadRequest, HttpResponse |
|
|
|
from django.utils.decorators import method_decorator |
|
|
|
from django.utils.decorators import method_decorator |
|
|
|
@ -33,6 +38,8 @@ from project.customer.decorators import license_required |
|
|
|
|
|
|
|
|
|
|
|
DEBUG = getattr(settings, 'DEBUG', False) |
|
|
|
DEBUG = getattr(settings, 'DEBUG', False) |
|
|
|
SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL', '') |
|
|
|
SUPPORT_EMAIL = getattr(settings, 'SUPPORT_EMAIL', '') |
|
|
|
|
|
|
|
MEDIA_ROOT = getattr(settings, 'MEDIA_ROOT', '') |
|
|
|
|
|
|
|
MEDIA_URL = getattr(settings, 'MEDIA_URL', '') |
|
|
|
|
|
|
|
|
|
|
|
ORDER_VAR = 'o' |
|
|
|
ORDER_VAR = 'o' |
|
|
|
ORDER_TYPE_VAR = 'ot' |
|
|
|
ORDER_TYPE_VAR = 'ot' |
|
|
|
@ -76,6 +83,7 @@ class BaseViews(object): |
|
|
|
TEMPLATE_EDIT = 'docs/_base/base_edit.html' |
|
|
|
TEMPLATE_EDIT = 'docs/_base/base_edit.html' |
|
|
|
TEMPLATE_DELETE = 'docs/_base/base_delete.html' |
|
|
|
TEMPLATE_DELETE = 'docs/_base/base_delete.html' |
|
|
|
TEMPLATE_FORM = 'docs/_base/base_form.html' |
|
|
|
TEMPLATE_FORM = 'docs/_base/base_form.html' |
|
|
|
|
|
|
|
TEMPLATE_IMG = 'docs/_base/preview.html' |
|
|
|
TEMPLATE_FORM_JS = 'docs/stub_js.html' |
|
|
|
TEMPLATE_FORM_JS = 'docs/stub_js.html' |
|
|
|
|
|
|
|
|
|
|
|
TEMPLATE_EMAIL = 'docs/email/base_email.html' |
|
|
|
TEMPLATE_EMAIL = 'docs/email/base_email.html' |
|
|
|
@ -368,6 +376,50 @@ class BaseViews(object): |
|
|
|
print '%s generation time (seconds): %s' % (self.XLS_TEMPLATE, time()-c1,) |
|
|
|
print '%s generation time (seconds): %s' % (self.XLS_TEMPLATE, time()-c1,) |
|
|
|
return xls |
|
|
|
return xls |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def as_img(self, *args, **kwargs): |
|
|
|
|
|
|
|
"""Вывести превью документа.""" |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
pdf = self.get_pdf(*args, **kwargs) |
|
|
|
|
|
|
|
_, filename = tempfile.mkstemp() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tmp_media_dir = os.path.join(MEDIA_ROOT, 'tmp') |
|
|
|
|
|
|
|
if not os.path.exists(tmp_media_dir): |
|
|
|
|
|
|
|
os.makedirs(tmp_media_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tmp_dir = tempfile.mkdtemp(dir=tmp_media_dir) |
|
|
|
|
|
|
|
tmp_dirname = os.path.split(tmp_dir)[1] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f = open(filename, 'w') |
|
|
|
|
|
|
|
f.write(pdf) |
|
|
|
|
|
|
|
f.close() |
|
|
|
|
|
|
|
args = ["", |
|
|
|
|
|
|
|
"-dNOPAUSE", "-dBATCH", "-dSAFER", |
|
|
|
|
|
|
|
"-sDEVICE=png16m", |
|
|
|
|
|
|
|
"-r150", |
|
|
|
|
|
|
|
"-sOutputFile=%s" % os.path.join(tmp_dir, "page-%03d.png"), |
|
|
|
|
|
|
|
"-f", filename |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
GS = ghostscript.Ghostscript(*args) |
|
|
|
|
|
|
|
os.remove(filename) |
|
|
|
|
|
|
|
filename = '%s.pdf' % self.get_filename(*args, **kwargs) |
|
|
|
|
|
|
|
imgs = glob.glob(os.path.join(tmp_dir, '*.png')) |
|
|
|
|
|
|
|
imgs = [os.path.split(i)[1] for i in imgs] |
|
|
|
|
|
|
|
imgs = ['%stmp/%s/%s' % (MEDIA_URL, tmp_dirname, i) for i in imgs] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dictionary = { |
|
|
|
|
|
|
|
'images': imgs, |
|
|
|
|
|
|
|
'padeji': self.PADEJI, |
|
|
|
|
|
|
|
'padeji_mnoj': self.PADEJI_MNOJ, |
|
|
|
|
|
|
|
'url_prefix': self.URL_PREFIX, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return render(self.request, self.TEMPLATE_IMG, dictionary) |
|
|
|
|
|
|
|
except: |
|
|
|
|
|
|
|
if DEBUG: |
|
|
|
|
|
|
|
raise |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
return HttpResponseServerError('Server error. Try later.') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def as_pdf(self, *args, **kwargs): |
|
|
|
def as_pdf(self, *args, **kwargs): |
|
|
|
"""Вывести документ в формате PDF в HttpResponse.""" |
|
|
|
"""Вывести документ в формате PDF в HttpResponse.""" |
|
|
|
try: |
|
|
|
try: |
|
|
|
|