# -*- coding: utf-8 -*- ######################################################################### ## This is a samples controller ## - index is the default action of any application ## - user is required for authentication and authorization ## - download is for downloading files uploaded in the db (does streaming) ## - call exposes all registered services (none by default) ######################################################################### def index(): return dict() def error(action='index',flash='Document not found'): session.flash=flash redirect(url(action)) def search(): if not request.vars.search or len(request.vars.search)<3: error('index',"Your search is too generic") sections = db(db.section.content.like('%'+request.vars.search+'%'))\ .select(db.section.title,db.section.chapter_number,db.section.section_number) if not sections: error('index','Search produced no results') return dict(sections=sections) @cache(request.env.path_info,3600) def section(): if request.vars.revision: section=db(db.section_history.id==request.vars.revision)\ .select().first() or error() section.id=section.section back = forward = None pages = [] else: try: i,j=int(request.args(0) or 0),int(request.args(1) or 0) section=db(db.section.chapter_number==i)\ (db.section.section_number==j).select().first() except ValueError: error() else: if not section: error() if len(section.content)<100: redirect(URL(r=request,args=(i,j+1))) i0,j0=i,j j0-=1 if j0<0: i0+=1 j0=0 if i0>=0: back = URL(r=request,args=(i0,j0)) else: back = None i1,j1=i,j+1 if not db(db.section.chapter_number==i1)\ (db.section.section_number==j1).count(): i1,j1=i+1,0 if db(db.section.chapter_number==i1)\ (db.section.section_number==j1).count(): forward = URL(r=request,args=(i1,j1)) else: forward=None pages = db(db.wikipage.active==True)\ (db.wikipage.section==section.id)\ .select(db.wikipage.slug,db.wikipage.title) content = wiki(section.content) return dict(section=section,content=content,back=back,forward=forward,pages=pages) def get_docstring(command): try: lines = eval('%s.__doc__' % command).split('\n') except: return None pad = min([len(x)-len(x.lstrip()) for x in lines if x.strip()]) return '\n'.join([line[pad:] for line in lines]) @cache(request.env.path_info,3600) def docstring(): if len(request.args)>2: error() command = '.'.join(request.args) if request.vars.revision: docstring=db(db.docstring_history.id==request.vars.revision)\ .select().first() or error() id = docstring.id=docstring.docstring content = wiki(docstring.content) else: import cStringIO, sys pydoc=local_import('pydoc') _s=cStringIO.StringIO() #if len(request.args)!=1: error() docstring= db(db.docstring.title==command).select().first() if not ALWAYS_RELOAD_DOCSTRING and docstring: id = docstring.id content = wiki(docstring.content) else: db(db.docstring.title==command).delete() docstring = get_docstring(command) if docstring: id=db.docstring.insert(title=command,content=docstring) else: id,docstring = 0,"No Information" content = wiki(docstring) attributes={} try: for key in eval('dir(%s)' % command): if key[:2]!='__': attributes[key]=get_docstring('%s.%s' % (command,key)) or '' except: pass return dict(id=id,content=content,command=command,attributes=attributes) def get_wiki(): if str(request.args(0)).isdigit(): page = db(db.wikipage.id==request.args(0)).select().first() else: page = db(db.wikipage.slug==request.args(0)).select().first() if not page: error() return page @cache(request.env.path_info,3600) def wikipages(): pages = db(db.wikipage.id>0).select(db.wikipage.title, db.wikipage.slug, db.wikipage.active, orderby=db.wikipage.title) return dict(pages=pages) @cache(request.env.path_info,3600) def wikipage(): if request.vars.revision: page=db(db.wikipage_history.id==request.vars.revision).select().first() \ or error() page.id=page.wikipage else: page = get_wiki() section = db.section[page.section] content = wiki(page.content) return dict(page=page, content=content, section=section) @auth.requires_login() def edit_section(): if not auth.user.editor: redirect(url('requires_editor')) section=db(db.section.chapter_number==request.args(0))\ (db.section.section_number==request.args(1))\ .select().first() or error() form = crud.update(db.section,section, onvalidation=lambda form: differ(db.section,form), onaccept=lambda form:clone(db.section,form.record), next=url('section',request.args)) form.element(_name='content')['_cols']=75 form.element(_name='content')['_rows']=40 return dict(form=form) @auth.requires_login() def edit_docstring(): if not auth.user.editor: redirect(url('requires_editor')) docstring=db(db.docstring.title=='.'.join(request.args or []))\ .select().first() or error() form = crud.update(db.docstring,docstring, onvalidation=lambda form: differ(db.docstring,form), onaccept=lambda form:clone(db.docstring,form.record), next=url('docstring',request.args)) form.element(_name='content')['_cols']=75 form.element(_name='content')['_rows']=40 return dict(form=form) @auth.requires_login() def create_wikipage(): db.wikipage.section.default=request.args(0) form = crud.create(db.wikipage, onvalidation=lambda form: \ form.vars.update(dict(slug=IS_SLUG.urlify(form.vars.title))), next='wikipage/[id]') form.element(_name='content')['_cols']=75 form.element(_name='content')['_rows']=40 return dict(form=form) @auth.requires_login() def edit_wikipage(): form = crud.update(db.wikipage,get_wiki(), onvalidation=lambda form: differ(db.wikipage,form), onaccept = lambda form: clone(db.wikipage,form.record), next='wikipage/[id]') form.element(_name='content')['_cols']=75 form.element(_name='content')['_rows']=40 return dict(form=form) @auth.requires_login() def log(): if not auth.user.editor: redirect(url('requires_editor')) tablename = request.args(0) if not tablename+'_history' in db.tables: error() id = request.args(1) table = db[tablename] record = table[id] or error() table_history = db[tablename+'_history'] if 'restore' in request.vars: orecord = db(table_history.id==request.vars.restore)\ (table_history[tablename]==id).select().first() if orecord: clone(table,record) orecord.created_on = request.now orecord.created_by = auth.user.id orecord.active = True del orecord['id'] del orecord[tablename] record.update_record(**dict(orecord)) redirect(url('log',request.args)) if request.post_vars: for key in request.post_vars: if key[:1]=='h': db(table_history.id==key[1:])\ (table_history[tablename]==id).delete() history = db(table_history[tablename]==id).select( table_history.id, table_history.title, table_history.changelog, table_history.active, table_history.diff, table_history.created_by, table_history.created_on, orderby=~table_history.created_on) return dict(tablename=tablename,record=record,history=history) @auth.requires_login() def documents(): if auth.user.editor and request.vars.delete: del db.document[request.vars.delete] redirect(url()) form=crud.create(db.document,request.args(0)) documents = db(db.document.id>0).select(orderby=db.document.title) return dict(form=form,documents=documents) def api(): return dict() def requires_editor(): return dict() def user(): """ exposes: http://..../[app]/default/user/login http://..../[app]/default/user/logout http://..../[app]/default/user/register http://..../[app]/default/user/profile http://..../[app]/default/user/retrieve_password http://..../[app]/default/user/change_password use @auth.requires_login() @auth.requires_membership('group name') @auth.requires_permission('read','table name',record_id) to decorate functions that need access control """ return dict(form=auth()) def download(): """ allows downloading of uploaded files http://..../[app]/default/download/[filename] """ return response.download(request,db) def call(): """ exposes services. for example: http://..../[app]/default/call/jsonrpc decorate with @services.jsonrpc the functions to expose supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv """ session.forget() return service()