{# -*- coding: utf-8 -*- #}
{% extends "schsys/db_field_edt.html" %}
{% load exfiltry %}
{% load exsyntax %}
{% block pythoncodeinit %}
def init_form(self):
self.save_btn.Disable()
self.modified = False
self.tuser = wx.Timer(self)
self.tuser.Start(1000)
self.Bind(wx.EVT_TIMER, self.on_timer_user, self.tuser)
self.EDITOR.SetSavePoint()
self._insert_txt.Bind(wx.EVT_BUTTON, self.on_insert_click)
self.save_btn.Bind(wx.EVT_BUTTON, self.on_click)
atab=self.get_acc_tab()
atab.append((0, wx.WXK_INSERT, self.on_panel_focus))
atab.append((wx.ACCEL_ALT, ord('I'), self.on_panel_focus))
self.set_acc_key_tab(self,atab)
self._panel.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.on_insert_click)
self._insert_icon.Bind(wx.EVT_BUTTON, self.on_insert_image_click)
self.EDITOR.SetCurrentPos(0)
self.EDITOR.SetSelection(0,0)
wx.CallAfter(self.EDITOR.SetFocus)
class ChoiceDialog(wx.Dialog):
def __init__(self, parent, title, choices):
wx.Dialog.__init__(self)
self.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
self.Create(parent, -1, title, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_DIALOG_STYLE, name='dialog')
self.choices = []
p = wx.Panel(self, -1, style = wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN | wx.FULL_REPAINT_ON_RESIZE)
self.sizer1 = wx.GridBagSizer(vgap=5, hgap=5)
row = 0;
for obj in choices:
self.sizer1.Add( wx.StaticText(p, -1, obj['title']), (row, 0), (1,1), wx.ALIGN_LEFT | wx.ALL, 5)
if len(obj['values'])>0 and type(obj['values'][0]) in (str, bytes):
choice = wx.Choice(p, -1, size=(600, -1), choices = obj['values'])
elif len(obj['values'])>0:
choice = wx.adv.BitmapComboBox(p, -1, size=(600, -1))
for c in obj['values']:
if type(c) == str:
choice.Append(c)
elif type(c) == bytes:
choice.Append(c.decode('utf-8'))
else:
choice.Append(c[0], c[1])
else:
choice = wx.TextCtrl(p, -1, size=(600, -1))
#wx.Choice(p, -1, size=(600, -1), choices = obj['values'])
self.choices.append(choice)
self.sizer1.Add(choice, (row, 1), (1,1), wx.ALIGN_LEFT | wx.ALL, 5)
row+=1;
self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
cancel = wx.Button(p, wx.ID_CANCEL, "Cancel")
ok = wx.Button(p, wx.ID_OK, "OK")
self.sizer2.Add( cancel, 1, wx.EXPAND|wx.ALL, 5)
self.sizer2.Add( ok, 1, wx.EXPAND|wx.ALL, 5)
ok.SetDefault()
#self.Bind(wx.EVT_BUTTON, self.on_ok, ok)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.sizer1, 0, wx.EXPAND)
box.Add(self.sizer2, 0, wx.EXPAND | wx.ALL, 5)
p.SetSizerAndFit(box)
self.SetClientSize(p.GetSize())
def on_ok(self, event):
print(event)
self.dialog = ChoiceDialog
from schbuilder.autocomplete import ACTIONS
self.actions = ACTIONS
from django.template import Template, Context
self.template_class = Template
self.context_class = Context
#from pytigon_lib.schhttptools.httpclient import HttpClient
#self.http_client_class = HttpClient
# aTable = [
# (wx.ACCEL_ALT, ord('I'), self.on_panel_focus),
# (wx.ACCEL_ALT, ord(';'), self.on_),
# ]
# self.set_acc_key_tab(aTable)
self.EDITOR.Bind(wx.EVT_KEY_DOWN, self.on_key_pressed)
def on_key_pressed(self, event):
if not event.AltDown() and not event.ControlDown() and ((event.KeyCode in ( ord('.'), ord(' ') ) and not event.ShiftDown()) or ((event.KeyCode in ( ord('\\'),)) and event.ShiftDown())):
def _fun():
ret_str = self.on_dialog("default")
if ret_str:
self.insert_txt(ret_str)
wx.CallAfter(_fun)
event.Skip()
event.Skip()
def on_panel_focus(self, event):
self._panel.SetFocus()
def on_insert_image_click(self, event):
self.insert_txt(self._icon.GetValue())
def on_dialog(self, key):
dialog_title = "Chose items"
parameters = []
template_str = ""
return_str = ""
if key == 'default':
line, pos = self.EDITOR.GetCurLine()
s = line[:pos].strip()
if s.endswith('%%'):
key = "blocks"
elif s.endswith('%'):
key = "tags"
elif s.endswith('|'):
key = 'filters'
elif s.endswith('{{'):
key = 'vars'
elif s.endswith('object.'):
key = "object fields and methods"
elif s.endswith('form.'):
key = "object fields and methods"
elif s.endswith('object_list.'):
key = "object fields and methods"
else:
return
if key in self.actions or ( '((' in key and '))' in key ):
if key in self.actions:
key2 = key
else:
key2 = key.split('((')[1].split('))')[0].replace(' ', '_')
obj = self.actions[key2]
if 'title' in obj:
dialog_title = obj['title']
if 'choices' in obj:
parameters = obj['choices']
for obj2 in parameters:
if "source_of_values" in obj2:
client = wx.GetApp().get_http(self)
response = client.get(self,"{{base_path}}schbuilder/autocomplete/{{object.id}}/%s/" % obj2['source_of_values'])
obj3 = response.json()
if len(obj3['choices'])>0:
obj2['values'] = obj3['choices'][0]['values']
if 'template' in obj:
template_str = obj['template']
elif key in ('object fields', 'object methods', 'object fields and methods', 'blocks', 'vars',) or key.endswith('filters') or key.endswith('tags'):
client = wx.GetApp().get_http(self)
response = client.get(self,"{{base_path}}schbuilder/autocomplete/{{object.id}}/%s/" % key.replace(' ', '_'))
obj = response.json()
if 'title' in obj:
dialog_title = obj['title']
if 'choices' in obj:
parameters = obj['choices']
if 'template' in obj:
template_str = obj['template']
else:
return key
if parameters and template_str:
if dialog_title or len(parameters)>1:
dlg = self.dialog(self, dialog_title, parameters)
dlg.CenterOnScreen()
val = dlg.ShowModal()
if val == wx.ID_OK:
choice = []
for ctrl in dlg.choices:
if type(ctrl) == wx.TextCtrl:
choice.append(ctrl.GetValue())
else:
choice.append(ctrl.GetStringSelection())
t = self.template_class(template_str)
c = self.context_class({"choice": choice})
ret = t.render(c)
return ret
else:
return None
else:
kw = parameters[0]['values']
kw.sort()
self.EDITOR.SetFocus()
self.EDITOR.AutoCompSetIgnoreCase(True)
self.EDITOR.AutoCompShow(0, (" ").join(kw))
elif template_str:
t = self.template_class(template_str)
c = self.context_class({})
ret = t.render(c)
return ret
return return_str
def on_insert_click(self, event):
item = self._panel.GetSelection()
if item.IsOk():
key = self._panel.GetItemText(item)
ret_str = self.on_dialog(key)
if ret_str:
self.insert_txt(ret_str)
def on_auto_comp_cmd(self, edt_ctrl, pos):
ret_str = self.on_dialog("default")
if ret_str:
self.insert_txt(ret_str)
def insert_txt(self, txt):
pos = self.EDITOR.GetCurrentPos()
self.EDITOR.InsertText(pos, txt)
pos+=len(txt)
self.EDITOR.SetCurrentPos(pos)
self.EDITOR.SetFocus()
{% endblock %}
{% block all %}
{% with form_width=800 form_height=1200 title=tab|add:"."|add:verbose_field_name %}
{{ block.super }}
{% endwith %}
{% endblock %}
{% block title %}
TEM:{{object.name}}
{% endblock %}
{% block body %}
{% if standard_web_browser %}
{{ block.super }}
{% else %}
{% endif %}
{% endblock %}
{% block content %}
{% if standard_web_browser %}
{% block edit_area %}
{% endblock %}
{% else %}
{{ block.super }}
{% endif %}
{% endblock %}