XMPP form example for XMPPpy
Материал из Linux Wiki
Перейти к навигацииПерейти к поиску
def iq_search_handler(self, iq):
typ = iq.getType()
iq_children = iq.getQueryChildren()
if (typ=='get') and (not iq_children):
repl = iq.buildReply('result')
repl.setQueryPayload(self.get_register_form())
self.jabber.send(repl)
raise NodeProcessed
elif (typ=='set') and iq_children:
self.set_register_form(iq)
raise NodeProcessed
def get_register_form(self):
ft = DataForm('form')
ft.addChild(node=DataField(name='FORM_TYPE',value=NS_SEARCH, typ='hidden'))
ft.addChild(node=DataField(name='city', label='Город', typ='text-single'))
return [ft]
def set_register_form(self, iq):
iq_children = iq.getQueryChildren()
for nod in iq_children:
for k in nod.getChildren():
if k.getAttr('var') == 'city':
for j in k.getChildren():
searchField = j.getData()
if searchField:
searchField='%'+searchField.replace("%","\\%")+'%'
else:
return
if searchField=='%%' or len(searchField)<4:
self.send_bad_request(iq)
return
data = cur.execute("SELECT * FROM cityindex WHERE idx LIKE (?) OR country_en LIKE (?) OR country_ru LIKE (?) OR name_en LIKE (?) OR name_ru LIKE (?) OR keywords LIKE (?) LIMIT 100", (searchField, searchField, searchField, searchField, searchField, searchField))
data = cur.fetchall()
print(data)
repl = iq.buildReply('result')
query = xmpp.Node('query', attrs={'xmlns':xmpp.NS_SEARCH})
rprt = Node('reported', payload=[
DataField(label='JID' ,name='jid' ,typ='jid-single'),
DataField(label='Index' ,name='idx' ,typ='text-single'),
DataField(label='Страна' ,name='cru' ,typ='text-single'),
DataField(label='Регион' ,name='rru' ,typ='text-single'),
DataField(label='Город' ,name='nru' ,typ='text-single'),
DataField(label='Country' ,name='cen' ,typ='text-single'),
DataField(label='Region' ,name='ren' ,typ='text-single'),
DataField(label='City' ,name='nen' ,typ='text-single'),
DataField(label='Lat.' ,name='lat' ,typ='text-single'),
DataField(label='Long.' ,name='lon' ,typ='text-single'),
DataField(label='Alt.' ,name='alt' ,typ='text-single'),
DataField(label='Keywords' ,name='kwr' ,typ='text-single')])
form = DataForm('result')
form.addChild(node=DataField(name='FORM_TYPE', value=NS_SEARCH, typ='hidden'))
form.addChild(node=rprt)
for flds in data:
rpl = Node('item', payload=[
DataField(name='jid', value=JID(node=unicode(str(flds[0]), "utf-8"), domain=self.domain)),
DataField(name='idx', value=flds[0]),
DataField(name='cru', value=flds[1]),
DataField(name='rru', value=flds[2]),
DataField(name='cen', value=flds[3]),
DataField(name='ren', value=flds[4]),
DataField(name='nru', value=flds[6]),
DataField(name='nen', value=flds[5]),
DataField(name='lat', value=flds[7]),
DataField(name='lon', value=flds[8]),
DataField(name='alt', value=flds[9]),
DataField(name='kwr', value=flds[10])])
form.addChild(node=rpl)
query.addChild(node=form)
repl.setPayload([query])
self.jabber.send(repl)