<?xml version="1.0" encoding="UTF-8"?>
<?python
  import template
?>
<html xmlns:py="http://purl.org/kid/ns#"
      py:layout="template">

<div py:def="buttons(_,rows,show)" style="text-align: center">
  <?python
    prev_page=max(0, int(REQ.get('offset',0)) - show)
    next_page=int(REQ.get('offset',0)) + show
    if rows<int(show):
      next_page = 0
    try:
      query='&'.join(["%s=%s" % (x,y) for x,y in REQ.items() if x!="offset"])
    except Exception,e:
      print "index.kid:", e
  ?>
  <a href="index.kid?offset=${prev_page}&amp;${query}" class="submit"
     py:if="int(REQ.get('offset', 0))>0">&lt;</a>
  <input name="view" class="submit" type="submit"
         py:attrs="{'value': _('View')}"/>
  <input name="recheck" class="submit" type="submit"
         py:attrs="{'value': _('Recheck')}"/>
  <a href="index.kid?offset=${next_page}&amp;${query}" class="submit"
     py:if="next_page">&gt;</a>
</div>

<div py:def="body(_)">
<?python
  try:
    QS="SELECT qname,datetime,virname,level,sender,size,status,recipient"\
       " FROM log"
    QA=[]
    # conditions
    WS,WA=[],[]
    cond=condition(WA)
    WS.append(cond.like('virname', REQ.get('virname')))
    WS.append(cond.like('sender', REQ.get('sender')))
    WS.append(cond.like('status', REQ.get('status', '!SENT')))
    WS.append(cond.date('begin', REQ.get('datetime'), '>='))
    WS.append(cond.date('end', REQ.get('datetime'), '<='))
    if 'A' in PERMS:
      WS.append(cond.like('recipient', REQ.get('recipient')))
    else:
      WS.append(cond.eq('recipient', REMOTE_USER))
    WS=[x for x in WS if x]
    if WS:
      QS+=" WHERE "+" AND ".join(WS)
      QA=WA
    QS+=" ORDER BY datetime DESC LIMIT %s OFFSET %s"
    QA.append(SHOW_ROWS)
    QA.append(int(REQ.get('offset', 0)))
    result=DB.query(QS,QA)
  except Exception,e:
    print "index.kid:", e
    result=[]
?>
<form action="action.kid">
<table>
<tr><td colspan="6">${buttons(_, DB.rowcount, SHOW_ROWS)}</td></tr>
<tr>
  <th></th>
  <th>${_('Date and time')}</th>
  <th>${_('Virus name (level)')}</th>
  <th>${_('Sender')}</th><th>${_('Size(B)')}</th>
  <th>${_('Action')}</th>
  <th py:if="'A' in PERMS">${_('Recipients')}</th>
</tr>
<tr py:for="row in result">
  <td py:if="row[0]">
    <input name="qname" value="${row[0]}" type="checkbox"/>
  </td>
  <td py:if="not row[0]">&nbsp;</td>
  <td>${row[1]}</td>
  <td>${row[2]} (${row[3]})</td>
  <td>${row[4]}</td>
  <td>${row[5]}</td>
  <td>${row[6]}</td>
  <td py:if="'A' in PERMS">${row[7]}</td>
</tr>
<tr><td colspan="6">${buttons(_, DB.rowcount, SHOW_ROWS)}</td></tr>
</table>
</form>
</div>

</html>
