// description of your code here
def getAcceptedClassifieds(self, session):
context = utils.context(self)
ret = []
for elem in session:
if elem['status'] == 'True':
el = getattr(context, elem['id'].split('/')[-1], None)
if el is not None:
yield el
def __call__(self, *args, **kwargs):
""" call method """
from StringIO import StringIO
from logging import getLogger
log = getLogger('xtg')
context = utils.context(self)
request = self.request
session = request.SESSION
response = request.response
xtg = StringIO()
xtg.write('<v2.00><e1>\r')
classifieds = self.getAcceptedClassifieds(request.form['ids'])
class2 = self.getAcceptedClassifieds(request.form['ids'])
import time
from sailkatuak import prepareBrainsForDict
start = time.time()
classified_list = prepareBrainsForDict(classifieds, obj=1)
xtg_new = self.convertToXtg(classified_list)
end = time.time()
log.info('Berria: %s' % str(end - start))
def ordena (x,y):
if x.getSaila()==y.getSaila():
return cmp(self.xtg_testua(x).lower(), self.xtg_testua(y).lower())
else:
return cmp(x.getSaila(), y.getSaila())
classs = list(class2)
start = time.time()
classs.sort(ordena)
for classified in classs:
classified_xtg = self.getClassifiedXtg(classified)
xtg.write(classified_xtg)
end = time.time()
log.info('Zaharra: %s' % str(end - start))
def convertToXtg(self, classifieds):
def sailarenIzena(datua):
return datua.split('-')
from StringIO import StringIO
xtg = StringIO()
xtg.write('<v2.00><e1>\r')
for section, secinfo in classifieds:
section_s = section.split('-', 1)
xtg.write(GOI_IZENBURU_ESTILOA)
xtg.write(section_s[0])
xtg.write(GOI_IZENBURU_TARTEKO_MARRA)
xtg.write(section_s[1])
xtg.write(LERRO_SALTOA)
for subsection, announces in secinfo:
subsection_s = subsection.split('-', 1)
xtg.write(BEHE_IZENBURU_AURRETIK)
xtg.write(LERRO_SALTOA)
xtg.write(BEHE_IZENBURU_ESTILOA)
xtg.write(subsection_s[0])
xtg.write('. ')
xtg.write(subsection_s[1])
xtg.write(BEHE_IZENBURU_BUKAERA)
xtg.write(LERRO_SALTOA)
for announce in announces:
if announces.index(announce) == 0:
xtg.write(TESTU_LEHEN_LERROA_ESTILOA)
elif announces.index(announce) == 1:
xtg.write(TESTU_LERROA_ESTILOA)
if announces.index(announce) == 1:
xtg.write(self.xtg_berria_testua(announce))
else:
xtg.write(self.xtg_testua(announce))
xtg.write(LERRO_SALTOA)
return xtg
def getClassifiedXtg(self, classified):
def sailarenIzena(datua):
return datua.split('-')
"""
self.lehen_lerroa='Bai'
self.ataleko_lehen_subsaila='Bai'
self.goi_izenburu_zaharra='Bai'
self.izenburu_zaharra='Bai'
self.lehena=0
"""
p = ''
sail_izena = sailarenIzena(classified.getSaila())
if self.goi_izenburu_zaharra!=sail_izena[1]:
if self.lehena==1:
p=p+GOI_IZENBURU_AURRETIK+LERRO_SALTOA
p=p+GOI_IZENBURU_ESTILOA+sail_izena[0][0]+GOI_IZENBURU_TARTEKO_MARRA+sail_izena[1]+LERRO_SALTOA
self.izenburu_zaharra='Goiena'
self.goi_izenburu_zaharra=sail_izena[1]
self.lehena=1
if self.izenburu_zaharra!=sail_izena[0]:
p=p+BEHE_IZENBURU_AURRETIK+LERRO_SALTOA
p=p+BEHE_IZENBURU_ESTILOA+sail_izena[0]+'. '+sail_izena[2]+BEHE_IZENBURU_BUKAERA+LERRO_SALTOA
self.izenburu_zaharra=sail_izena[0]
self.lehen_lerroa='1'
if self.lehen_lerroa=='1':
p=p+TESTU_LEHEN_LERROA_ESTILOA+self.xtg_testua(classified)+LERRO_SALTOA
self.lehen_lerroa='2'
elif self.lehen_lerroa == '2' :
p=p+TESTU_LERROA_ESTILOA+self.xtg_berria_testua(classified)+LERRO_SALTOA
self.lehen_lerroa='3'
else:
p=p+self.xtg_testua(classified)+LERRO_SALTOA
return p
def xtg_testua(self, classified):
titulua = classified.Title()
deskribapena = classified.Description().strip()
kontaktua = classified.getTelephone1()
testua=''
if len(deskribapena)>0:
if deskribapena[-1]!='.':
deskribapena=deskribapena+'.'
if len(kontaktua)>0:
if kontaktua[-1]!='.':
kontaktua=kontaktua+'.'
testua='<B>'+titulua+'<$>'+' '+deskribapena+' '+kontaktua
return testua
def xtg_berria_testua(self, classified):
TESTU_LEHEN_HITZA_ESTILOA='@TXT-Sailaktuak/Lehena/Hitza'
xtg_t = self.xtg_testua(classified)
xtg_t = xtg_t[3:]
return TESTU_LEHEN_HITZA_ESTILOA + xtg_t
def prepareBrainsForDict(brains, sectionname=0, obj=0):
""" Prepare a dict to get the list we need to return """
dic = {}
for brain in brains:
if obj:
if sectionname:
section = brain.Title()
else:
section = brain.getSaila()
else:
if sectionname:
section = brain.Title
else:
section = brain.getSaila
type, subtype, name = section.split('-')
tip = type[0] + '-' + subtype
subtip = type + '-' + name
dict_type = dic.get(tip, None)
if dict_type is not None:
dict_type_subtype = dict_type.get(subtip, None)
if dict_type_subtype is not None:
dic[tip][subtip].append(brain)
else:
dic[tip][subtip] = [brain]
else:
dic[tip] = {subtip: [brain]}
ret = []
for k,v in dic.items():
l = []
for i,j in v.items():
l.append((i,j))
ret.append((k,l))
def sortBySection(c1, c2):
return cmp(c1[0], c2[0])
ret2 = []
for k,v in ret:
v.sort(sortBySection)
ret2.append((k,v))
ret2.sort(sortBySection)
return ret2