Fully rework display with CSS style inclusion

This commit is contained in:
Grégory Soutadé 2014-11-27 19:38:41 +01:00
parent 02233f2f37
commit d2b0d0dae6
6 changed files with 176 additions and 104 deletions

View File

@ -1,47 +1,109 @@
import os
class DisplayHTMLBlock(object):
class DisplayHTMLRaw(object):
def __init__(self, title=''):
self.title = title
def build(self, f):
pass
class DisplayHTMLRawBlock(DisplayHTMLBlock):
def __init__(self, title=''):
super(DisplayHTMLRawBlock, self).__init__(title)
self.html = ''
def __init__(self, html=''):
self.html = html
def setRawHTML(self, html):
self.html = html
def _build(self, f, html):
if html: f.write(html)
def build(self, f):
f.write(self.html)
self._build(self.html)
class DisplayHTMLBlock(DisplayHTMLRaw):
def __init__(self, title=''):
super(DisplayHTMLBlock, self).__init__(html='')
self.title = title
self.cssclass = 'iwla_block'
self.title_cssclass = 'iwla_block_title'
self.value_cssclass = 'iwla_block_value'
def getTitle(self):
return self.title
def setTitle(self, value):
self.title = value
def setCSSClass(self, cssclass):
self.cssclass = cssclass
def setTitleCSSClass(self, cssclass):
self.title_cssclass = cssclass
def setValueCSSClass(self, cssclass):
self.value_cssclass = cssclass
def build(self, f):
html = '<div class="%s">' % (self.cssclass)
if self.title:
html += '<div class="%s">%s</div>' % (self.title_cssclass, self.title)
html += '<div class="%s">%s</div>' % (self.value_cssclass, self.html)
html += '</div>'
self._build(f, html)
class DisplayHTMLBlockTable(DisplayHTMLBlock):
def __init__(self, title, cols):
super(DisplayHTMLBlockTable, self).__init__(title)
super(DisplayHTMLBlockTable, self).__init__(title=title)
self.cols = cols
self.rows = []
self.cols_cssclasses = ['' for e in cols]
self.rows_cssclasses = []
def appendRow(self, row):
self.rows.append(listToStr(row))
self.rows_cssclasses.append(['' for e in row])
def getCellValue(row, col):
if row < 0 or col < 0 or\
row >= len(self.rows) or col >= len(self.cols):
raise ValueError('Invalid indices')
return self.rows[row][col]
def setCellValue(row, col, value):
if row < 0 or col < 0 or\
row >= len(self.rows) or col >= len(self.cols):
raise ValueError('Invalid indices')
return self.rows[row][col]
def setCellCSSClass(row, col, value):
if row < 0 or col < 0 or\
row >= len(self.rows) or col >= len(self.cols):
raise ValueError('Invalid indices')
self.rows_cssclasses[row][col] = value
def setRowCSSClass(row, value):
if row < 0 or row >= len(self.rows):
raise ValueError('Invalid indice')
for i in range(0, self.rows_cssclasses[row]):
self.rows_cssclasses[row][i] = value
def build(self, f):
f.write('<table>')
f.write('<tr>')
for title in self.cols:
f.write('<th>%s</th>' % (title))
f.write('</tr>')
for row in self.rows:
f.write('<tr>')
for v in row:
f.write('<td>%s</td>' % (v))
f.write('</tr>')
f.write('</table>')
if not self.html:
html = '<table>'
html += '<tr>'
for title in self.cols:
html += '<th>%s</th>' % (title)
html += '</tr>'
for row in self.rows:
html += '<tr>'
for v in row:
html += '<td>%s</td>' % (v)
html += '</tr>'
html += '</table>'
self.html = html
super(DisplayHTMLBlockTable, self).build(f)
class DisplayHTMLPage(object):
@ -53,6 +115,12 @@ class DisplayHTMLPage(object):
def getFilename(self):
return self.filename;
def getBlock(self, title):
for b in self.blocks:
if title == b.getTitle():
return b
return None
def appendBlock(self, block):
self.blocks.append(block)
@ -64,7 +132,13 @@ class DisplayHTMLPage(object):
os.makedirs(base)
f = open(filename, 'w')
f.write('<html><title>%s</title><body>' % (self.title))
f.write('<!DOCTYPE html>')
f.write('<html>')
f.write('<head>')
f.write('<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />')
if self.title:
f.write('<title>%s</title>' % (self.title))
f.write('</head>')
for block in self.blocks:
block.build(f)
f.write('</body></html>')

View File

@ -42,6 +42,11 @@ class IWLADisplayAllVisits(IPlugin):
display.addPage(page)
index = self.iwla.getDisplayIndex()
block = DisplayHTMLRawBlock()
block.setRawHTML('<a href=\'%s\'>All visits</a>' % (filename))
index.appendBlock(block)
link = '<a href=\'%s\'>All visits</a>' % (filename)
block = index.getBlock('Top visitors')
if block:
block.setTitle('%s - %s' % (block.getTitle(), link))
else:
block = DisplayHTMLRawBlock()
block.setRawHTML(link)
index.appendBlock(block)

View File

@ -29,29 +29,10 @@ class IWLADisplayReferers(IPlugin):
top_key_phrases = key_phrases.items()
top_key_phrases = sorted(top_key_phrases, key=lambda t: t[1], reverse=True)
# Top referers in index
cur_time = self.iwla.getCurTime()
index = self.iwla.getDisplayIndex()
table = DisplayHTMLBlockTable('Connexion from', ['Origin', 'Pages', 'Hits'])
table.appendRow(['<b>Search Engine</b>', '', ''])
for r,_ in top_search_engine_referers[:10]:
row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
table.appendRow(row)
table.appendRow(['<b>External URL</b>', '', ''])
for r,_ in top_referers[:10]:
row = [r, referers[r]['pages'], referers[r]['hits']]
table.appendRow(row)
table.appendRow(['<b>External URL (robot)</b>', '', ''])
for r,_ in top_robots_referers[:10]:
row = [r, robots_referers[r]['pages'], robots_referers[r]['hits']]
table.appendRow(row)
index.appendBlock(table)
# All referers in a file
cur_time = self.iwla.getCurTime()
title = time.strftime('Connexion from - %B %Y', cur_time)
filename = 'referers.html'
@ -80,14 +61,27 @@ class IWLADisplayReferers(IPlugin):
display = self.iwla.getDisplay()
display.addPage(page)
block = DisplayHTMLRawBlock()
block.setRawHTML('<a href=\'%s\'>All referers</a>' % (filename))
index.appendBlock(block)
link = '<a href=\'%s\'>All referers</a>' % (filename)
# Top referers in index
title = '%s - %s' % ('Connexion from', link)
table = DisplayHTMLBlockTable(title, ['Origin', 'Pages', 'Hits'])
table.appendRow(['<b>Search Engine</b>', '', ''])
for r,_ in top_search_engine_referers[:10]:
row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
table.appendRow(row)
table.appendRow(['<b>External URL</b>', '', ''])
for r,_ in top_referers[:10]:
row = [r, referers[r]['pages'], referers[r]['hits']]
table.appendRow(row)
table.appendRow(['<b>External URL (robot)</b>', '', ''])
for r,_ in top_robots_referers[:10]:
row = [r, robots_referers[r]['pages'], robots_referers[r]['hits']]
table.appendRow(row)
# Top key phrases in index
table = DisplayHTMLBlockTable('Top key phrases', ['Key phrase', 'Search'])
for phrase in top_key_phrases[:10]:
table.appendRow([phrase[0], phrase[1]])
index.appendBlock(table)
# All key phrases in a file
@ -104,6 +98,11 @@ class IWLADisplayReferers(IPlugin):
display.addPage(page)
block = DisplayHTMLRawBlock()
block.setRawHTML('<a href=\'%s\'>All key phrases</a>' % (filename))
index.appendBlock(block)
link = '<a href=\'%s\'>All key phrases</a>' % (filename)
# Top key phrases in index
title = '%s - %s' % ('Top key phrases', link)
table = DisplayHTMLBlockTable(title, ['Key phrase', 'Search'])
for phrase in top_key_phrases[:10]:
table.appendRow([phrase[0], phrase[1]])
index.appendBlock(table)

View File

@ -12,20 +12,12 @@ class IWLADisplayTopDownloads(IPlugin):
def hook(self):
top_downloads = self.iwla.getMonthStats()['top_downloads']
top_downloads = sorted(top_downloads.items(), key=lambda t: t[1], reverse=True)
index = self.iwla.getDisplayIndex()
table = DisplayHTMLBlockTable('Top Downloads', ['URI', 'Hits'])
for (uri, entrance) in top_downloads[:10]:
table.appendRow([uri, entrance])
index.appendBlock(table)
title = time.strftime('Top Downloads - %B %Y', self.iwla.getCurTime())
# All in a file
filename = 'top_downloads.html'
path = self.iwla.getCurDisplayPath(filename)
title = time.strftime('All Downloads - %B %Y', self.iwla.getCurTime())
page = DisplayHTMLPage(title, path)
table = DisplayHTMLBlockTable('All Downloads', ['URI', 'Hit'])
@ -33,9 +25,15 @@ class IWLADisplayTopDownloads(IPlugin):
table.appendRow([uri, entrance])
page.appendBlock(table)
display = self.iwla.getDisplay()
display.addPage(page)
self.iwla.getDisplay().addPage(page)
block = DisplayHTMLRawBlock()
block.setRawHTML('<a href=\'%s\'>All Downloads</a>' % (filename))
index.appendBlock(block)
link = '<a href=\'%s\'>All Downloads</a>' % (filename)
title = '%s - %s' % ('Top Downloads', link)
# Top in index
index = self.iwla.getDisplayIndex()
table = DisplayHTMLBlockTable(title, ['URI', 'Hits'])
for (uri, entrance) in top_downloads[:10]:
table.appendRow([uri, entrance])
index.appendBlock(table)

View File

@ -12,30 +12,28 @@ class IWLADisplayTopHits(IPlugin):
def hook(self):
top_hits = self.iwla.getMonthStats()['top_hits']
top_hits = sorted(top_hits.items(), key=lambda t: t[1], reverse=True)
index = self.iwla.getDisplayIndex()
table = DisplayHTMLBlockTable('Top Hits', ['URI', 'Entrance'])
for (uri, entrance) in top_hits[:10]:
table.appendRow([uri, entrance])
index.appendBlock(table)
# All in a file
title = time.strftime('All Hits - %B %Y', self.iwla.getCurTime())
filename = 'top_hits.html'
path = self.iwla.getCurDisplayPath(filename)
page = DisplayHTMLPage(title, path)
table = DisplayHTMLBlockTable('Top Hits', ['URI', 'Entrance'])
table = DisplayHTMLBlockTable('All Hits', ['URI', 'Entrance'])
for (uri, entrance) in top_hits:
table.appendRow([uri, entrance])
page.appendBlock(table)
display = self.iwla.getDisplay()
display.addPage(page)
self.iwla.getDisplay().addPage(page)
block = DisplayHTMLRawBlock()
block.setRawHTML('<a href=\'%s\'>All hits</a>' % (filename))
index.appendBlock(block)
link = '<a href=\'%s\'>All hits</a>' % (filename)
title = '%s - %s' % ('Top Hits', link)
# Top in index
index = self.iwla.getDisplayIndex()
table = DisplayHTMLBlockTable(title, ['URI', 'Entrance'])
for (uri, entrance) in top_hits[:10]:
table.appendRow([uri, entrance])
index.appendBlock(table)

View File

@ -12,30 +12,28 @@ class IWLADisplayTopPages(IPlugin):
def hook(self):
top_pages = self.iwla.getMonthStats()['top_pages']
top_pages = sorted(top_pages.items(), key=lambda t: t[1], reverse=True)
index = self.iwla.getDisplayIndex()
table = DisplayHTMLBlockTable('Top Pages', ['URI', 'Entrance'])
for (uri, entrance) in top_pages[:10]:
table.appendRow([uri, entrance])
index.appendBlock(table)
# All in a page
title = time.strftime('All Pages - %B %Y', self.iwla.getCurTime())
filename = 'top_pages.html'
path = self.iwla.getCurDisplayPath(filename)
page = DisplayHTMLPage(title, path)
table = DisplayHTMLBlockTable('Top Pages', ['URI', 'Entrance'])
table = DisplayHTMLBlockTable('All Pages', ['URI', 'Entrance'])
for (uri, entrance) in top_pages:
table.appendRow([uri, entrance])
page.appendBlock(table)
display = self.iwla.getDisplay()
display.addPage(page)
self.iwla.getDisplay().addPage(page)
block = DisplayHTMLRawBlock()
block.setRawHTML('<a href=\'%s\'>All pages</a>' % (filename))
index.appendBlock(block)
link = '<a href=\'%s\'>All pages</a>' % (filename)
title = '%s - %s' % ('Top Pages', link)
# Top in index
index = self.iwla.getDisplayIndex()
table = DisplayHTMLBlockTable(title, ['URI', 'Entrance'])
for (uri, entrance) in top_pages[:10]:
table.appendRow([uri, entrance])
index.appendBlock(table)