--- AllAttachmentsMacro-1.0.py	2008-10-25 15:28:00.000000000 -0400
+++ AllAttachments-mine.py	2010-04-21 06:32:38.000000000 -0400
@@ -3,7 +3,7 @@
 # Copyright (C) 2008 Daan van Etten <daan@stuq.nl>
 # All rights reserved.
 # This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
-# To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send 
+# To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send
 # a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
 
 from trac.wiki.macros import WikiMacroBase, parse_args
@@ -14,15 +14,23 @@
     """Shows all attachments on the Trac site
 
        The first argument is the filter for which attachments to show.
-       The filter can have the value 'ticket' or 'wiki'. Omitting the filter argument
-       shows all attachments.
+       The filter can have the value 'ticket' or 'wiki'. 
+       Omitting the filter argument shows all attachments.
 
        Examples:
 
        {{{
-           [[AllAttachments()]]                   # Show all attachments
-           [[AllAttachments(ticket)]]             # Show the attachments that are linked to tickets
-           [[AllAttachments(wiki)]]               # Show the attachments that are linked to wiki pages
+           [[AllAttachments()]]          # Show all attachments
+           [[AllAttachments(ticket)]]    # Show the attachments that 
+                                         # are linked to tickets
+           [[AllAttachments(wiki)]]      # Show the attachments 
+                                         # that are linked to wiki pages
+
+           [[AllAttachments(wiki, page=, order=, sort=)]]
+           # page(optional): choose a wiki page to get attachments from
+           # order(optional): id (wiki page name), filename, size, time, author
+           # sort(optional): ASC/DESC (Ascending or Descending order)
+
        }}}
 
        ''Created by Daan van Etten (http://stuq.nl/software/trac/AllAttachmentsMacro)''
@@ -32,7 +40,15 @@
     url = "http://stuq.nl/software/trac/AllAttachments"
 
     def expand_macro(self, formatter, name, content):
+        args, kw = parse_args(content)
+        page = kw.get('page', '')
+        sort = kw.get('sort', 'DESC')
+        order = kw.get('order', 'time')
+
+        self.env.log.error('sort %s, order %s' % (sort, order))
+
         attachment_type = ""
+        wiki_path = ""
         if content:
             argv = [arg.strip() for arg in content.split(',')]
             if len(argv) > 0:
@@ -41,25 +57,36 @@
         db = self.env.get_db_cnx()
         if db == None:
            return "No DB connection"
-        
+
         attachmentFormattedList=""
 
         cursor = db.cursor()
 
         if attachment_type == None or attachment_type == "":
-            cursor.execute("SELECT type,id,filename,size,time,description,author,ipnr FROM attachment")
+            cursor.execute("SELECT type,id,filename,size,time,description,"
+                           "author,ipnr FROM attachment")
+
+        elif page == None or page == "":
+            cursor.execute("SELECT type,id,filename,size,time,description,"
+                           "author,ipnr FROM attachment WHERE type=%s ORDER "
+                           "BY " + order + " " + sort, (attachment_type,))
+
         else:
-            cursor.execute("SELECT type,id,filename,size,time,description,author,ipnr FROM attachment WHERE type=%s", (attachment_type,))
-        
-        formatters={"wiki": formatter.href.wiki, "ticket": formatter.href.ticket}
+            cursor.execute("SELECT type,id,filename,size,time,description,"
+                           "author,ipnr FROM attachment WHERE type=%s and "
+                           "id=%s ORDER BY " + order + " " + sort, 
+                           (attachment_type, page))
+
+        formatters={"wiki": formatter.href.wiki, 
+                    "ticket": formatter.href.ticket}
         types={"wiki": "", "ticket": "ticket "}
 
         return tag.ul(
                       [tag.li(
-                          tag.a(filename, href=formatter.href.attachment(type + "/" + id + "/" + filename)), 
-                          " (", tag.span(pretty_size(size), title=size), ") - added by ",
+                          tag.a(filename, href=formatter.href.attachment(type + "/" + id + "/" + filename)),
+                          " (", tag.span(pretty_size(size), title=size), ") - ", tag.em((datetime.datetime.fromtimestamp(time)).ctime()), " - added by ",
                           tag.em(author), " to ",
                           tag.a(types[type] + " " + id, href=formatters[type](id)), " ")
                     for type,id,filename,size,time,description,author,ipnr in cursor])
 
-        return attachmentFormattedList
\ No newline at end of file
+        return attachmentFormattedList
