Back to index

moin  1.9.0~rc2
RandomPage.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - RandomPage Macro
00004 
00005     @copyright: 2000 Juergen Hermann <jh@web.de>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import random
00010 random.seed()
00011 
00012 from MoinMoin.Page import Page
00013 
00014 Dependencies = ["time"]
00015 
00016 def macro_RandomPage(macro, links=1):
00017     request = macro.request
00018     links = max(links, 1) # at least 1 link
00019 
00020     # Get full page unfiltered page list - very fast!
00021     all_pages = request.rootpage.getPageList(user='', exists=0)
00022 
00023     # Now select random page from the full list, and if it exists and we
00024     # can read it, save.
00025     pages = []
00026     found = 0
00027     while found < links and all_pages:
00028         # Take one random page from the list
00029         pagename = random.choice(all_pages)
00030         all_pages.remove(pagename)
00031 
00032         # Filter out deleted pages or pages the user may not read.
00033         page = Page(request, pagename)
00034         if page.exists() and request.user.may.read(pagename):
00035             pages.append(pagename)
00036             found += 1
00037 
00038     if not pages:
00039         return ''
00040 
00041     f = macro.formatter
00042 
00043     # return a single page link
00044     if links == 1:
00045         name = pages[0]
00046         return (f.pagelink(1, name, generated=1) +
00047                 f.text(name) +
00048                 f.pagelink(0, name))
00049 
00050     # return a list of page links
00051     pages.sort()
00052     result = []
00053     write = result.append
00054 
00055     write(f.bullet_list(1))
00056     for name in pages:
00057         write(f.listitem(1))
00058         write(f.pagelink(1, name, generated=1))
00059         write(f.text(name))
00060         write(f.pagelink(0, name))
00061         write(f.listitem(0))
00062     write(f.bullet_list(0))
00063 
00064     result = ''.join(result)
00065     return result
00066