diff --git a/BaseModels/wkhtmltopdf/bin/wkhtmltoimage.exe b/BaseModels/wkhtmltopdf/bin/wkhtmltoimage.exe new file mode 100644 index 0000000..e26e1ac Binary files /dev/null and b/BaseModels/wkhtmltopdf/bin/wkhtmltoimage.exe differ diff --git a/BaseModels/wkhtmltopdf/bin/wkhtmltopdf.exe b/BaseModels/wkhtmltopdf/bin/wkhtmltopdf.exe new file mode 100644 index 0000000..af5283b Binary files /dev/null and b/BaseModels/wkhtmltopdf/bin/wkhtmltopdf.exe differ diff --git a/BaseModels/wkhtmltopdf/bin/wkhtmltox.dll b/BaseModels/wkhtmltopdf/bin/wkhtmltox.dll new file mode 100644 index 0000000..98a007b Binary files /dev/null and b/BaseModels/wkhtmltopdf/bin/wkhtmltox.dll differ diff --git a/BaseModels/wkhtmltopdf/include/wkhtmltox/dllbegin.inc b/BaseModels/wkhtmltopdf/include/wkhtmltox/dllbegin.inc new file mode 100644 index 0000000..4f623f8 --- /dev/null +++ b/BaseModels/wkhtmltopdf/include/wkhtmltox/dllbegin.inc @@ -0,0 +1,52 @@ +/* + * Copyright 2010 wkhtmltopdf authors + * + * This file is part of wkhtmltopdf. + * + * wkhtmltopdf is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wkhtmltopdf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wkhtmltopdf. If not, see . + */ + +#ifndef __WKHTMLTOPDF_DLLBEGIN__ +#define __WKHTMLTOPDF_DLLBEGIN__ + +#if defined _WIN32 || defined __CYGWIN__ + #ifdef BUILDING_DLL + #define DLL_PUBLIC __declspec(dllexport) + #else + #define DLL_PUBLIC __declspec(dllimport) + #endif + #define DLL_LOCAL +#else + #if __GNUC__ >= 4 + #define DLL_PUBLIC __attribute__ ((visibility("default"))) + #define DLL_LOCAL __attribute__ ((visibility("hidden"))) + #else + #define DLL_PUBLIC + #define DLL_LOCAL + #endif +#endif + +#if defined _WIN32 +#define CALLTYPE __stdcall +#else +#define CALLTYPE +#endif + +#ifdef __cplusplus + #define CAPI(type) extern "C" DLL_PUBLIC type CALLTYPE +#else + #define CAPI(type) DLL_PUBLIC type CALLTYPE +#endif + +#endif /*__WKHTMLTOPDF_DLLBEGIN__*/ diff --git a/BaseModels/wkhtmltopdf/include/wkhtmltox/dllend.inc b/BaseModels/wkhtmltopdf/include/wkhtmltox/dllend.inc new file mode 100644 index 0000000..5024fb4 --- /dev/null +++ b/BaseModels/wkhtmltopdf/include/wkhtmltox/dllend.inc @@ -0,0 +1,28 @@ +/* + * Copyright 2010 wkhtmltopdf authors + * + * This file is part of wkhtmltopdf. + * + * wkhtmltopdf is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wkhtmltopdf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wkhtmltopdf. If not, see . + */ + +#ifdef __WKHTMLTOPDF_DLLBEGIN__ + +#undef __WKHTMLTOPDF_DLLBEGIN__ +#undef DLL_PUBLIC +#undef DLL_LOCAL +#undef CAPI +#undef CALLTYPE + +#endif /*__WKHTMLTOPDF_DLLBEGIN__*/ diff --git a/BaseModels/wkhtmltopdf/include/wkhtmltox/image.h b/BaseModels/wkhtmltopdf/include/wkhtmltox/image.h new file mode 100644 index 0000000..a53d8a4 --- /dev/null +++ b/BaseModels/wkhtmltopdf/include/wkhtmltox/image.h @@ -0,0 +1,64 @@ +/* + * Copyright 2010 wkhtmltopdf authors + * + * This file is part of wkhtmltopdf. + * + * wkhtmltopdf is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wkhtmltopdf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wkhtmltopdf. If not, see . + */ + +#ifndef __IMAGE_H__ +#define __IMAGE_H__ +#include + +struct wkhtmltoimage_global_settings; +typedef struct wkhtmltoimage_global_settings wkhtmltoimage_global_settings; + +struct wkhtmltoimage_converter; +typedef struct wkhtmltoimage_converter wkhtmltoimage_converter; + +typedef void (*wkhtmltoimage_str_callback)(wkhtmltoimage_converter * converter, const char * str); +typedef void (*wkhtmltoimage_int_callback)(wkhtmltoimage_converter * converter, const int val); +typedef void (*wkhtmltoimage_void_callback)(wkhtmltoimage_converter * converter); + +CAPI(int) wkhtmltoimage_init(int use_graphics); +CAPI(int) wkhtmltoimage_deinit(); +CAPI(int) wkhtmltoimage_extended_qt(); +CAPI(const char *)wkhtmltoimage_version(); + +CAPI(wkhtmltoimage_global_settings *) wkhtmltoimage_create_global_settings(); + +CAPI(int) wkhtmltoimage_set_global_setting(wkhtmltoimage_global_settings * settings, const char * name, const char * value); +CAPI(int) wkhtmltoimage_get_global_setting(wkhtmltoimage_global_settings * settings, const char * name, char * value, int vs); + +CAPI(wkhtmltoimage_converter *) wkhtmltoimage_create_converter(wkhtmltoimage_global_settings * settings, const char * data); +CAPI(void) wkhtmltoimage_destroy_converter(wkhtmltoimage_converter * converter); + +CAPI(void) wkhtmltoimage_set_warning_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_str_callback cb); +CAPI(void) wkhtmltoimage_set_error_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_str_callback cb); +CAPI(void) wkhtmltoimage_set_phase_changed_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_void_callback cb); +CAPI(void) wkhtmltoimage_set_progress_changed_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_int_callback cb); +CAPI(void) wkhtmltoimage_set_finished_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_int_callback cb); +CAPI(int) wkhtmltoimage_convert(wkhtmltoimage_converter * converter); +/* CAPI(void) wkhtmltoimage_begin_conversion(wkhtmltoimage_converter * converter); */ +/* CAPI(void) wkhtmltoimage_cancel(wkhtmltoimage_converter * converter); */ + +CAPI(int) wkhtmltoimage_current_phase(wkhtmltoimage_converter * converter); +CAPI(int) wkhtmltoimage_phase_count(wkhtmltoimage_converter * converter); +CAPI(const char *) wkhtmltoimage_phase_description(wkhtmltoimage_converter * converter, int phase); +CAPI(const char *) wkhtmltoimage_progress_string(wkhtmltoimage_converter * converter); +CAPI(int) wkhtmltoimage_http_error_code(wkhtmltoimage_converter * converter); +CAPI(long) wkhtmltoimage_get_output(wkhtmltoimage_converter * converter, const unsigned char **); + +#include +#endif /*__IMAGE_H__*/ diff --git a/BaseModels/wkhtmltopdf/include/wkhtmltox/pdf.h b/BaseModels/wkhtmltopdf/include/wkhtmltox/pdf.h new file mode 100644 index 0000000..f757bae --- /dev/null +++ b/BaseModels/wkhtmltopdf/include/wkhtmltox/pdf.h @@ -0,0 +1,76 @@ +/* + * Copyright 2010 wkhtmltopdf authors + * + * This file is part of wkhtmltopdf. + * + * wkhtmltopdf is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wkhtmltopdf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wkhtmltopdf. If not, see . + */ + +#ifndef __PDF_H__ +#define __PDF_H__ +#include + +struct wkhtmltopdf_global_settings; +typedef struct wkhtmltopdf_global_settings wkhtmltopdf_global_settings; + +struct wkhtmltopdf_object_settings; +typedef struct wkhtmltopdf_object_settings wkhtmltopdf_object_settings; + +struct wkhtmltopdf_converter; +typedef struct wkhtmltopdf_converter wkhtmltopdf_converter; + +typedef void (*wkhtmltopdf_str_callback)(wkhtmltopdf_converter * converter, const char * str); +typedef void (*wkhtmltopdf_int_callback)(wkhtmltopdf_converter * converter, const int val); +typedef void (*wkhtmltopdf_void_callback)(wkhtmltopdf_converter * converter); + +CAPI(int) wkhtmltopdf_init(int use_graphics); +CAPI(int) wkhtmltopdf_deinit(); +CAPI(int) wkhtmltopdf_extended_qt(); +CAPI(const char *) wkhtmltopdf_version(); + +CAPI(wkhtmltopdf_global_settings *) wkhtmltopdf_create_global_settings(); +CAPI(void) wkhtmltopdf_destroy_global_settings(wkhtmltopdf_global_settings *); + +CAPI(wkhtmltopdf_object_settings *) wkhtmltopdf_create_object_settings(); +CAPI(void) wkhtmltopdf_destroy_object_settings(wkhtmltopdf_object_settings *); + +CAPI(int) wkhtmltopdf_set_global_setting(wkhtmltopdf_global_settings * settings, const char * name, const char * value); +CAPI(int) wkhtmltopdf_get_global_setting(wkhtmltopdf_global_settings * settings, const char * name, char * value, int vs); +CAPI(int) wkhtmltopdf_set_object_setting(wkhtmltopdf_object_settings * settings, const char * name, const char * value); +CAPI(int) wkhtmltopdf_get_object_setting(wkhtmltopdf_object_settings * settings, const char * name, char * value, int vs); + + +CAPI(wkhtmltopdf_converter *) wkhtmltopdf_create_converter(wkhtmltopdf_global_settings * settings); +CAPI(void) wkhtmltopdf_destroy_converter(wkhtmltopdf_converter * converter); + +CAPI(void) wkhtmltopdf_set_warning_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_str_callback cb); +CAPI(void) wkhtmltopdf_set_error_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_str_callback cb); +CAPI(void) wkhtmltopdf_set_phase_changed_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_void_callback cb); +CAPI(void) wkhtmltopdf_set_progress_changed_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_int_callback cb); +CAPI(void) wkhtmltopdf_set_finished_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_int_callback cb); +/* CAPI(void) wkhtmltopdf_begin_conversion(wkhtmltopdf_converter * converter); */ +/* CAPI(void) wkhtmltopdf_cancel(wkhtmltopdf_converter * converter); */ +CAPI(int) wkhtmltopdf_convert(wkhtmltopdf_converter * converter); +CAPI(void) wkhtmltopdf_add_object( + wkhtmltopdf_converter * converter, wkhtmltopdf_object_settings * setting, const char * data); + +CAPI(int) wkhtmltopdf_current_phase(wkhtmltopdf_converter * converter); +CAPI(int) wkhtmltopdf_phase_count(wkhtmltopdf_converter * converter); +CAPI(const char *) wkhtmltopdf_phase_description(wkhtmltopdf_converter * converter, int phase); +CAPI(const char *) wkhtmltopdf_progress_string(wkhtmltopdf_converter * converter); +CAPI(int) wkhtmltopdf_http_error_code(wkhtmltopdf_converter * converter); +CAPI(long) wkhtmltopdf_get_output(wkhtmltopdf_converter * converter, const unsigned char **); + +#include +#endif /*__PDF_H__*/ diff --git a/BaseModels/wkhtmltopdf/uninstall.exe b/BaseModels/wkhtmltopdf/uninstall.exe new file mode 100644 index 0000000..8410b17 Binary files /dev/null and b/BaseModels/wkhtmltopdf/uninstall.exe differ diff --git a/DocsApp/views.py b/DocsApp/views.py index dd25bfc..8d057ef 100644 --- a/DocsApp/views.py +++ b/DocsApp/views.py @@ -9,6 +9,7 @@ from GeneralApp.funcs import get_inter_http_respose from django.utils.translation import gettext_lazy as _ from django.urls import reverse from collections import OrderedDict +from django.template.loader import render_to_string def get_tree_arts(art, arts, sel_arts_ids): @@ -27,22 +28,59 @@ def get_tree_arts(art, arts, sel_arts_ids): return docs_tree -# def get_tree_arts(art, arts): -# -# docs_tree = OrderedDict() -# docs_tree.update({ -# art['name']: { -# 'url': art['url'], -# 'id': art['id'], -# 'children': get_tree_arts(art, DocArt.objects.filter(parent=art)), -# } for art in arts.filter(parent=None) -# }) -# return docs_tree +import os +def link_callback(uri, rel): + if settings.STATIC_URL and uri.startswith(settings.STATIC_URL): + path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, "")) + elif settings.MEDIA_URL and uri.startswith(settings.MEDIA_URL): + path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, "")) + else: + path = os.path.join(settings.STATIC_ROOT, uri) + return path + + +def _response_pdf(path): + + filePDF = open(path, 'rb') + + content = filePDF.read() + # content = str(content) + filePDF.close() + + response = HttpResponse(content, content_type='application/pdf') + response['Content-Disposition'] = 'filename="{fn}"'.format(fn=os.path.basename(path)) + return response + + +def pdf_render(request, template, context, file_name): + from BaseModels.functions import url_translit + + filename = url_translit(file_name) + + html = render_to_string(template, context, request) + f = open(f'{settings.PDF_FOLDER}/{filename}.html', 'w') + f.write(html) + f.close() + + os.system( + u'{wkhtmltopdf} --load-error-handling ignore --default-header {pdf_folder}/{file_name}.html {pdf_folder}/{file_name}.pdf'.format( + wkhtmltopdf=settings.WKHTML_TO_PDF, pdf_folder=settings.PDF_FOLDER, file_name=filename)) + path = settings.PDF_FOLDER + '/' + filename + '.pdf' + # os.system(u'{wkhtmltopdf} {print_folder}/order{filename}.html {print_folder}/order{filename}.pdf'.format(wkhtmltopdf=settings.WKHTML_TO_PDF, print_folder=settings.PRINT_FOLDER)) + return _response_pdf(path) + def DocsView(request, version=None, art_url=None): + + if art_url and version: + + generate_pdf = False + if request.GET and 'pdf' in request.GET and request.GET['pdf'] == 'true': + generate_pdf = True + kwargs = { 'enable': True, 'url': art_url, @@ -85,10 +123,19 @@ def DocsView(request, version=None, art_url=None): 'allow_versions': versions, 'breadcrumbs': breadcrumbs, 'prev_article': art.get_prev_article(version=vers_obj), - 'next_article': art.get_next_article(version=vers_obj) + 'next_article': art.get_next_article(version=vers_obj), + 'pdf_render': generate_pdf } - t = loader.get_template('pages/p_documentation.html') - return get_inter_http_respose(t, Dict, request) + + if not generate_pdf: + tpl = 'pages/p_documentation.html' + t = loader.get_template(tpl) + return get_inter_http_respose(t, Dict, request) + else: + tpl = 'blocks/b_documentation_article.html' + # tpl = 'pages/p_documentation.html' + return pdf_render(request, tpl, Dict, art.name) + except DocArt.DoesNotExist: raise Http404 diff --git a/GeneralApp/admin.py b/GeneralApp/admin.py index 2f57f44..2267605 100644 --- a/GeneralApp/admin.py +++ b/GeneralApp/admin.py @@ -1,8 +1,10 @@ -from django import forms +import copy +from django import forms +from django.contrib import admin from sets.admin import * from .models import * -from django.contrib import admin + from django.utils.translation import gettext_lazy as _ # from nested_inline.admin import NestedStackedInline, NestedTabularInline, NestedModelAdmin, NestedInline from super_inlines.admin import SuperInlineModelAdmin, SuperModelAdmin @@ -131,6 +133,77 @@ class Admin_StackedInline_Block(Admin_Trans_GenericBaseIconStackedInline, SuperI +class Admin_PageAddingBlocksTemplate(Admin_BaseIconModel): + from ServicesApp.admin import Admin_StackedInline_BlockPluginPresentation + from SlidesApp.admin import Admin_StackedInline_Slide + + fieldsets = [ + (None, { + 'classes': ['wide'], + 'fields': ('name', + # 'url', + # 'title', + # 'description', 'text', + # 'picture', + # 'order', + ) + }), + # ('SEO', { + # 'classes': ['wide', 'collapse'], + # 'fields': ( + # 'seo_title', 'seo_description', 'seo_keywords', 'seo_text', + # ) + # }), + ] + + def get_list_display(self, request): + if request.user.is_superuser: + # self.list_display.append() + d_list = copy.copy(self.list_display) + d_list.append('name_plural') + return d_list + return self.list_display + + def get_changelist_instance(self, request): + if request.user.is_superuser: + self.list_editable.append('name_plural') + return super(Admin_PageAddingBlocksTemplate, self).get_changelist_instance(request) + + + list_display = [ + # 'image_thumb', + 'id', + 'name', + 'enable', + # 'url', + # 'title', + # 'order', + 'modifiedDT', 'createDT' + ] + + list_display_links = ['id', 'name'] + list_editable = ['enable'] + + list_filter = ['modifiedDT', 'createDT'] + search_fields = ['name'] + # filter_horizontal = ['options'] + + inlines = [ + Admin_StackedInline_Slide, + Admin_StackedInline_BlockPluginPresentation, + Admin_StackedInline_Block, + # AdminStacked_FAQitem, + ] + + def has_delete_permission(self, request, obj=None): + if request.user.is_superuser: + return True + + if not obj or obj.name_plural in ['documentation', 'contacts']: + return False + +admin.site.register(PageAddingBlocksTemplate, Admin_PageAddingBlocksTemplate) + class Admin_StaticPage(SuperModelAdmin, Admin_Trans_BaseModelViewPage): diff --git a/GeneralApp/models.py b/GeneralApp/models.py index 9410edc..4b9d4a9 100644 --- a/GeneralApp/models.py +++ b/GeneralApp/models.py @@ -12,6 +12,13 @@ from colorfield.fields import ColorField from filebrowser.fields import FileBrowseField from filebrowser.base import FileObject + +class PageAddingBlocksTemplate(BaseModel): + class Meta: + verbose_name = _('Шаблон настройки дополнительных блоков на странице') + verbose_name_plural = _('Шаблоны настройки дополнительных блоков на странице') + + class FileUnit(BaseModel): file = FileBrowseField(verbose_name=_('Файл'), max_length=250, directory="files/") diff --git a/pAerBim/settings.py b/pAerBim/settings.py index 59f8225..1c303ef 100644 --- a/pAerBim/settings.py +++ b/pAerBim/settings.py @@ -271,6 +271,9 @@ CKEDITOR_CONFIGS = { } } +PDF_FOLDER = 'media/pdf' +WKHTML_TO_PDF = 'BaseModels\wkhtmltopdf\{0}\wkhtmltopdf'.format(u'bin') + try: import db_local_sets diff --git a/templates/blocks/b_Pdocumentation_article.html b/templates/blocks/b_Pdocumentation_article.html deleted file mode 100644 index 2d533bb..0000000 --- a/templates/blocks/b_Pdocumentation_article.html +++ /dev/null @@ -1,37 +0,0 @@ -{% load static %} -{% load i18n %} -
-{#
#} -{# #} -{#
#} -{#
#} -{# {{ cur_article.text }}#} -{#
#} - {{ cur_article.text|safe }} -
-
- {% if prev_article %} - - - - - -
- {% trans "Предыдущий" %} -
- {% endif %} -
-
- {% if next_article %} -
- {% trans "Следующий" %} -
- - - - - {% endif %} -
-
- {% trans "Скачать в PDF" %} -
\ No newline at end of file diff --git a/templates/blocks/b_documentation_article.html b/templates/blocks/b_documentation_article.html new file mode 100644 index 0000000..6849a91 --- /dev/null +++ b/templates/blocks/b_documentation_article.html @@ -0,0 +1,49 @@ +{% load static %} +{% load i18n %} + +{% if pdf_render %} + + + + +{% endif %} + +
+{#
#} +{# #} +{#
#} +{#
#} +{# {{ cur_article.text }}#} +{#
#} + {{ cur_article.text|safe }} + {% if not pdf_render %} +
+
+ {% if prev_article %} + + + + + +
+ {% trans "Предыдущий" %} +
+ {% endif %} +
+ +
+ {% if next_article %} +
+ {% trans "Следующий" %} +
+ + + + + {% endif %} +
+
+ + {% trans "Скачать в PDF" %} + {% endif %} +
\ No newline at end of file diff --git a/templates/pages/p_documentation.html b/templates/pages/p_documentation.html index 4463871..e6dc915 100644 --- a/templates/pages/p_documentation.html +++ b/templates/pages/p_documentation.html @@ -117,7 +117,7 @@ {% endfor %} - {% include "blocks/b_Pdocumentation_article.html" %} + {% include "blocks/b_documentation_article.html" %} {% endblock %} \ No newline at end of file