iwla/plugins/display/all_visits.py
Grégory Soutadé ccab40b4e1 Replace g.gettext by iwla._
Add createCurTitle() to factorize code
2014-12-17 20:31:59 +01:00

80 lines
2.4 KiB
Python

import time
from iwla import IWLA
from iplugin import IPlugin
from display import *
#
# Display hook
#
# Create All visits page
#
# Plugin requirements :
# None
#
# Conf values needed :
# display_visitor_ip*
#
# Output files :
# OUTPUT_ROOT/year/month/all_visits.html
# OUTPUT_ROOT/year/month/index.html
#
# Statistics creation :
# None
#
# Statistics update :
# None
#
# Statistics deletion :
# None
#
class IWLADisplayAllVisits(IPlugin):
def __init__(self, iwla):
super(IWLADisplayAllVisits, self).__init__(iwla)
self.API_VERSION = 1
def hook(self):
display = self.iwla.getDisplay()
hits = self.iwla.getValidVisitors()
display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
last_access = sorted(hits.values(), key=lambda t: t['last_access'], reverse=True)
title = time.strftime(self.iwla._(u'All visits') + u' - %B %Y', self.iwla.getCurTime())
filename = 'all_visits.html'
path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Last seen'), [self.iwla._(u'Host'), self.iwla._(u'Pages'), self.iwla._(u'Hits'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')])
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', ''])
for super_hit in last_access:
address = super_hit['remote_addr']
if display_visitor_ip and\
super_hit.get('dns_name_replaced', False):
address = '%s [%s]' % (address, super_hit['remote_ip'])
row = [
address,
super_hit['viewed_pages'],
super_hit['viewed_hits'],
bytesToStr(super_hit['bandwidth']),
time.asctime(super_hit['last_access'])
]
table.appendRow(row)
page.appendBlock(table)
display.addPage(page)
index = self.iwla.getDisplayIndex()
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All visits'))
block = index.getBlock(self.iwla._(u'Top visitors'))
if block:
block.setTitle('%s - %s' % (block.getTitle(), link))
else:
block = display.createBlock(DisplayHTMLRawBlock)
block.setRawHTML(link)
index.appendBlock(block)