<s> import os <EOL> import difflib <EOL> from docutils . core import publish_parts <EOL> from jinja2 import Markup <EOL> from kalapy import db <EOL> from kalapy . web import url_for <EOL> class Page ( db . Model ) : <EOL> name = db . String ( size = <NUM_LIT> , required = True , unique = True ) <EOL> @ property <EOL> def title ( self ) : <EOL> return self . name . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> @ classmethod <EOL> def by_name ( cls , name ) : <EOL> page = cls . all ( ) . filter ( '<STR_LIT>' , name ) . first ( ) <EOL> if page : <EOL> return page . revisions . all ( ) . order ( '<STR_LIT>' ) . first ( ) <EOL> return None <EOL> @ classmethod <EOL> def by_revision ( cls , revision ) : <EOL> return Revision . get ( revision ) <EOL> class Revision ( db . Model ) : <EOL> page = db . ManyToOne ( Page , reverse_name = '<STR_LIT>' ) <EOL> timestamp = db . DateTime ( default_now = True ) <EOL> text = db . Text ( ) <EOL> note = db . String ( size = <NUM_LIT:200> ) <EOL> @ property <EOL> def name ( self ) : <EOL> return self . page . name <EOL> @ property <EOL> def title ( self ) : <EOL> return self . page . title <EOL> @ property <EOL> def time ( self ) : <EOL> return self . timestamp . strftime ( '<STR_LIT>' ) <EOL> def render ( self ) : <EOL> return parse_rst ( self . text ) <EOL> def parse_rst ( markup ) : <EOL> parts = publish_parts ( <EOL> source = markup , <EOL> writer_name = '<STR_LIT>' , <EOL> settings_overrides = { '<STR_LIT>' : True } ) <EOL> return parts [ '<STR_LIT>' ] <EOL> class Pagination ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , query , per_page , page , endpoint ) : <EOL> self . query = query <EOL> self . per_page = per_page <EOL> self . page = page <EOL> self . endpoint = endpoint <EOL> self . _count = None <EOL> @ property <EOL> def entries ( self ) : <EOL> return self . query . fetch ( self . per_page , ( self . page - <NUM_LIT:1> ) * self . per_page ) <EOL> @ property <EOL> def has_previous ( self ) : <EOL> return self . page > <NUM_LIT:1> <EOL> @ property <EOL> def has_next ( self ) : <EOL> return self . page < self . pages <EOL> @ property <EOL> def previous ( self ) : <EOL> return url_for ( self . endpoint , page = self . page - <NUM_LIT:1> ) <EOL> @ property <EOL> def next ( self ) : <EOL> return url_for ( self . endpoint , page = self . page + <NUM_LIT:1> ) <EOL> @ property <EOL> def count ( self ) : <EOL> return self . query . count ( ) <EOL> @ property <EOL> def pages ( self ) : <EOL> return max ( <NUM_LIT:0> , self . count - <NUM_LIT:1> ) // self . per_page + <NUM_LIT:1> </s>
