Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.macro.RandomPage Namespace Reference

Functions

def macro_RandomPage

Variables

list Dependencies = ["time"]

Function Documentation

def MoinMoin.macro.RandomPage.macro_RandomPage (   macro,
  links = 1 
)

Definition at line 16 of file RandomPage.py.

00016 
00017 def macro_RandomPage(macro, links=1):
00018     request = macro.request
00019     links = max(links, 1) # at least 1 link
00020 
00021     # Get full page unfiltered page list - very fast!
00022     all_pages = request.rootpage.getPageList(user='', exists=0)
00023 
00024     # Now select random page from the full list, and if it exists and we
00025     # can read it, save.
00026     pages = []
00027     found = 0
00028     while found < links and all_pages:
00029         # Take one random page from the list
00030         pagename = random.choice(all_pages)
00031         all_pages.remove(pagename)
00032 
00033         # Filter out deleted pages or pages the user may not read.
00034         page = Page(request, pagename)
00035         if page.exists() and request.user.may.read(pagename):
00036             pages.append(pagename)
00037             found += 1
00038 
00039     if not pages:
00040         return ''
00041 
00042     f = macro.formatter
00043 
00044     # return a single page link
00045     if links == 1:
00046         name = pages[0]
00047         return (f.pagelink(1, name, generated=1) +
00048                 f.text(name) +
00049                 f.pagelink(0, name))
00050 
00051     # return a list of page links
00052     pages.sort()
00053     result = []
00054     write = result.append
00055 
00056     write(f.bullet_list(1))
00057     for name in pages:
00058         write(f.listitem(1))
00059         write(f.pagelink(1, name, generated=1))
00060         write(f.text(name))
00061         write(f.pagelink(0, name))
00062         write(f.listitem(0))
00063     write(f.bullet_list(0))
00064 
00065     result = ''.join(result)
00066     return result
00067 

Variable Documentation

Definition at line 14 of file RandomPage.py.