# -*- coding: utf-8 -*- from statistics import OracleSelect, Chart, Table select_traf = OracleSelect('user/password@DB', """select DAY, NSS_TRAF, BSS_TRAF from DAY_TRAFFIC where DAY >= trunc(sysdate,'dd')-32""") chart_traf = Chart(selector=select_traf, x_column='DAY', y_columns=[('NSS_TRAF', u'NSS '), ('BSS_TRAF', u'BSS ')]) table_traf = Table(selector=select_traf, columns=['DAY', 'NSS_TRAF', 'BSS_TRAF']) template = """ {{ chart_traf }} {{ table_traf }} """
In fact, there are much more options for the Chart and Table widgets, but I don’t see any sense in the demo code to list them all. import os from django.template import RequestContext, Template from django.http import HttpResponse, Http404 from settings import PROJECT_ROOT # , __file__ settings.py def dynamic_page(request, report_path): ctx_dict = {} execfile(os.path.join(PROJECT_ROOT, 'reports', report_path + '.py'), ctx_dict) templ_header = '{% extends "base.html" %}{% block content %}' templ_footer = '{% endblock %}' template = Template(templ_header + ctx_dict['template'] + templ_footer) context = RequestContext(request) context.autoescape = False context.update(ctx_dict) return HttpResponse(template.render(context))
(r'^reports/(?P<report_path>.+)$', 'statistics.views.dynamic_page'),
def dynamic_page(request, report_path): ctx_dict = {'get': request.GET.get} ...
chart_name = Chart(select, x_col, config, ..., html_id='chart_name')
... execfile(os.path.join(PROJECT_ROOT, 'reports', report_path + '.py'), ctx_dict) for (name, obj) in ctx_dict.items(): if isinstance(obj, (Chart, Table)): obj.html_id = name ...
bs = get('bs') if bs is not None: TITLE = u' %s' % bs
import os from django.template import RequestContext, Template from django.http import HttpResponse, Http404 from settings import PROJECT_ROOT from functools import partial def get_param(request, key=None, default=None, as_list=False): if key: if as_list: return request.GET.getlist(key) else: return request.GET.get(key, default) else: return request.GET.lists() class DynamicPage(object): " " # view def __init__(self, subpath, # , , parent_template = "base.html", load_tags = (), # block_name = 'content', pre_calc = lambda request, context: None, # execfile post_calc = lambda request, context: None): # execfile self.templ_header = ('{% extends "' + parent_template + '" %}' + DynamicPage.loading_tags(load_tags) + DynamicPage.block_top(block_name)) self.templ_footer = DynamicPage.block_foot(block_name) self.subpath = subpath self.pre_calc = pre_calc self.post_calc = post_calc @staticmethod def block_top(block_name): if block_name: return "{% block " + block_name + " %}" else: return '' @staticmethod def block_foot(block_name): if block_name: return "{% endblock %}" else: return '' @staticmethod def loading_tags(tags): return ''.join(['{% load ' + tag + ' %}' for tag in tags]) @property def __name__(self): return self.__class__.__name__ # view def __call__(self, request, pagepath): ctx_dict = self.get_context(request, pagepath) if 'response' in ctx_dict and isinstance(ctx_dict['response'], HttpResponse): return ctx_dict['response'] # response # , , html- else: template = Template(self.templ_header + ctx_dict['template'] + self.templ_footer) context = RequestContext(request) context.autoescape = False context.update(ctx_dict) return HttpResponse(template.render(context)) def get_context(self, request, pagepath): fullpath = os.path.join(PROJECT_ROOT, self.subpath, pagepath + '.py') if not os.path.exists(fullpath): raise Http404 ctx_dict = {'get': partial(get_param, request), 'request': request} self.pre_calc(request, ctx_dict) execfile(fullpath, ctx_dict) self.post_calc(request, ctx_dict) return ctx_dict
def add_html_id(request, context): for (name, obj) in context.items(): if isinstance(obj, (Chart, Table)): obj.html_id = name show_report = DynamicPage('stat_tech/pages', parent_template='stat_tech/base.html', load_tags=['adminmedia', 'jquery', 'chapters'], post_calc=add_html_id)
show_weekly = DynamicPage('stat_weekly/pages', parent_template = 'stat_weekly/base.html', load_tags = ['chapters', ' employees'], block_name=None)
template = """ {% block chart %} {{ costs_monthly }} {{ costs_weekly }} {% endblock %} {% block responsible %} {% employee vasily_pupkin %}, {% employee ivan_ivanov %} {% endblock %} """
def add_division(request, context): div = Division.get_by_user(request.user) context['DIVISION'] = div context['SUBMENU'] = calc_goal_submenu(request.path, div) show_goal = DynamicPage('stat_goals/pages', load_tags = ['chapters'], block_name='report', parent_template = 'stat_goals/base.html', pre_calc = add_division)
(r'^stat/(?P<pagepath>.+)$', 'stat_tech.views.show_report'), (r'^weeklyreport/(?P<pagepath>.+)$', 'stat_weekly.views.show_weekly'), (r'^goals/(?P<pagepath>.+)$', 'stat_goals.views.show_goal'),
Source: https://habr.com/ru/post/185180/
All Articles