# copyright 2007 ETH Zurich, DMATL
# author lorenz textor

from time import strptime, strftime

CONTAINERS = ['Silva Folder','Silva Publication', 'Folder']
CONTENT = ['Silva Document', 'Silva Personal Information', 'Silva VVZ Page', 'IMAP Calendar List', 'Silva Image', 'Silva File']

def checkModified(obj,date):
    """walks through all folders below object obj and checks if something has been modified at or after the given date (YYYY-MM-DD)"""
    # get documents
    def walk(object):
        for item in object.objectValues(CONTAINERS):
            walk(item)
        docs.extend(object.objectValues(CONTENT))

    output = ['<html><head></head><body><p>SilvaDocuments modified at or after %s</p><hr /><ul>' % date]
    docs = []
    count = 0
    compareDate = strptime(date,'%Y-%m-%d')
    walk(obj)
    # check for changed files
    for doc in docs:
        modDate = doc.get_modification_datetime()
        if modDate:
            modDateStr = '%s-%s-%s' % (modDate.year(),modDate.month(),modDate.day())
            modDate = strptime(modDateStr,'%Y-%m-%d')
            if modDate >= compareDate:
                output.append('<li><a href="%s" target="_blank">%s</a> <span style="color:red">%s</span></li>' % (doc.absolute_url(),doc.absolute_url_path(),strftime('%Y-%m-%d',modDate)))
                count += 1
        else:
            output.append('<li><a href="%s" target="_blank">%s</a> <span style="color:red">no modification time</span></li>' % (doc.absolute_url(),doc.absolute_url_path()))
    output.append("<p><b>%d SilvaDocuments scanned (%d newer than %s).</b></p>" % (len(docs),count,date))
    output.append('</ul></body></html>')
    return ''.join(output)
