coding utf-8 import gtk import gobject import socket import string hos

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
import gtk
import gobject
import socket
import string
host = '77.123.255.64'
port = 3333
class Interface():
def __init__(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
str = '{"len": 74 ,"name": "hello", "dbname": "vkomat", "user": "dima", "password": "qpa9j1"}'
self.sock.send(str)
str = self.sock.recv(256)
print str
self.candidate_active = True;
self.candidate_def_query = "SELECT * FROM candidate_view;"
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("delete_event", lambda w, e: gtk.main_quit())
notebook = gtk.Notebook()
notebook.show()
results = self.get_results(self.candidate_def_query[:-1] + ' WHERE expire_date IS NULL')
types = [gobject.TYPE_STRING] * len(results[0])
candidate_store = gtk.ListStore(*types)
for el in results[1:]:
print el
candidate_store.append(el)
scr_window = gtk.ScrolledWindow()
renderer = gtk.CellRendererText()
candidate_table = gtk.TreeView(candidate_store)
candidate_table.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH)
scr_window.add(candidate_table)
scr_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
for i, el in enumerate(results[0]):
column = gtk.TreeViewColumn(el, renderer, text = i)
column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
column.set_expand(True)
column.set_resizable(True)
candidate_table.append_column(column)
candidate_table.show()
scr_window.show()
hbox = gtk.HBox(False, 10)
hbox.set_border_width(10)
hbox.pack_start(scr_window, True, True, 0)
hbox.show()
vbox = gtk.VBox(False, 10);
vbox.set_border_width(10)
vbox.show()
hbox.pack_end(vbox, False, False, 0)
button = gtk.Button('Оновити')
button.connect("clicked", self.refresh_candidate, candidate_store)
button.show()
vbox.pack_start(button, False, False, 0)
check_button = gtk.CheckButton('Лише наявні')
check_button.connect("toggled", self.set_candidate_activity, None)
vbox.pack_start(check_button, False, False, 0)
check_button.set_active(True)
check_button.show()
button = gtk.Button('Видалити')
button.connect("clicked", self.delete_candidate, (candidate_table, candidate_store))
button.show()
vbox.pack_start(button, False, False, 0)
button = gtk.Button('Створити')
button.connect("clicked", self.create_candidate, window)
button.show()
vbox.pack_start(button, False, False, 0)
button = gtk.Button('Вихід')
button.show()
vbox.pack_start(button, False, False, 0)
button.connect("clicked", lambda w, e: gtk.main_quit(), None)
window.add(notebook)
notebook.append_page(hbox, gtk.Label('Кандидати'))
hbox = gtk.HBox(False, 10)
hbox.show()
notebook.append_page(hbox, gtk.Label('Працівники'))
window.show()
gtk.main()
def set_candidate_activity(self, w, e):
self.candidate_active = w.get_active()
print self.candidate_active
def refresh_candidate(self, widget, ptr):
store = ptr
store.clear()
if self.candidate_active:
query = self.candidate_def_query[:-1] + ' WHERE expire_date IS NULL;'
else:
query = self.candidate_def_query
results = self.get_results(query)
for el in results[1:]:
store.append(el)
def get_results(self, query):
packet = '{"len":%-12d,"name":"query","query":"%s"}' % ((len(query) + 26), query)
print packet
self.sock.send(packet)
header = self.sock.recv(20)
length = string.atoi(header[7:19])
body = ''
str = self.sock.recv(length - len(body))
body += str
while(len(body) < length):
str = self.sock.recv(length - len(body))
print body
body += str
packet = header + body
packet = eval(packet)
res = []
res.append(packet["names"])
for el in packet["items"]:
res.append(el)
return res
def create_candidate(self, widget, parent):
dialog = gtk.Dialog('Створити кандидата', parent)
dialog.set_modal(True)
dialog.set_has_separator(False)
dialog.set_has_frame(False)
notebook = gtk.Notebook()
notebook.show()
vbox = gtk.VBox(False, 10)
vbox.show()
notebook.append_page(vbox, gtk.Label('Кандидат'))
vbox1 = gtk.VBox(False, 10)
vbox1.show()
vbox.pack_start(vbox1, True, True, 10)
entry_names = ['Ідентифікаційний код:', 'Прізвище:', 'Ім''я:', 'По-батькові:', 'Номер телефону']
for el in entry_names:
hbox1 = gtk.HBox(False, 10)
hbox1.show()
vbox1.pack_start(hbox1, False, False, 10)
label = gtk.Label(el)
label.show()
hbox1.pack_start(label, False, False, 0)
entry = gtk.Entry()
entry.set_max_length(10)
entry.set_size_request(100, -1)
entry.show()
hbox1.pack_end(entry, False, False, 0)
label.show()
dialog.vbox.pack_start(notebook, True, True, 0)
dialog.show()
def delete_candidate(self, widget, ptr):
table = ptr[0]
store = ptr[1]
selection = table.get_selection()
res = selection.get_selected()
id = res[0].get_value(res[1], 0)
query = 'DELETE FROM candidate WHERE id = %s' % id
packet = '{"len":%-12d,"name":"query","query":"%s"}' % ((len(query) + 26), query)
self.sock.send(packet)
header = self.sock.recv(20)
length = string.atoi(header[7:19])
body = ''
str = self.sock.recv(length - len(body))
body += str
while(len(body) < length):
str = self.sock.recv(length - len(body))
print body
body += str
packet = header + body
self.refresh_candidate(widget, store)
if __name__ == '__main__':
Interface(host, port)