<s> import os <EOL> import tornado . web <EOL> from datetime import datetime <EOL> from amonone . core import settings <EOL> from amonone . web . apps . core . models import server_model <EOL> from amonone . web . libs . session import MongoDBSession <EOL> from amonone . web . template import render as jinja_render <EOL> from amonone . web . apps . auth . acl import ( <EOL> check_permissions , <EOL> all_servers_for_user , <EOL> ) <EOL> class BaseView ( tornado . web . RequestHandler ) : <EOL> def initialize ( self ) : <EOL> self . session = self . _create_session ( ) <EOL> self . now = datetime . utcnow ( ) <EOL> self . acl = settings . ACL <EOL> try : <EOL> current_page = self . current_page <EOL> except : <EOL> current_page = '<STR_LIT>' <EOL> self . template_vars = { <EOL> "<STR_LIT:user>" : self . current_user , <EOL> "<STR_LIT:url>" : self . request . uri , <EOL> "<STR_LIT>" : current_page , <EOL> "<STR_LIT>" : self . xsrf_form_html ( ) , <EOL> } <EOL> super ( BaseView , self ) . initialize ( ) <EOL> def check_xsrf_cookie ( self ) : <EOL> try : <EOL> test = os . environ [ '<STR_LIT>' ] <EOL> except : <EOL> super ( BaseView , self ) . check_xsrf_cookie ( ) <EOL> def get_current_user ( self ) : <EOL> try : <EOL> if os . environ [ '<STR_LIT>' ] == '<STR_LIT:True>' : <EOL> return { "<STR_LIT:username>" : "<STR_LIT>" , "<STR_LIT:type>" : "<STR_LIT>" } <EOL> except : <EOL> pass <EOL> current_page = self . request . uri . split ( '<STR_LIT:?>' ) <EOL> try : <EOL> url = current_page [ <NUM_LIT:0> ] <EOL> except : <EOL> url = self . request . uri <EOL> id = False <EOL> list = [ '<STR_LIT:/>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if url in list : <EOL> try : <EOL> url_params = self . request . uri . split ( '<STR_LIT:=>' ) <EOL> id = url_params [ - <NUM_LIT:1> ] <EOL> id = False if id in list else id <EOL> except : <EOL> id = False <EOL> try : <EOL> user = self . session [ '<STR_LIT:user>' ] <EOL> except KeyError : <EOL> return None <EOL> permissions = check_permissions ( id , url , user ) <EOL> if permissions is True : <EOL> return user <EOL> return None <EOL> def write_error ( self , status_code , ** kwargs ) : <EOL> error_trace = None <EOL> if "<STR_LIT>" in kwargs : <EOL> import traceback <EOL> error_trace = "<STR_LIT>" <EOL> for line in traceback . format_exception ( * kwargs [ "<STR_LIT>" ] ) : <EOL> error_trace += line <EOL> self . render ( "<STR_LIT>" , <EOL> status_code = status_code , <EOL> error_trace = error_trace , <EOL> unread_values = None ) <EOL> def finish ( self , chunk = None ) : <EOL> if self . session is not None and self . session . _delete_cookie : <EOL> self . clear_cookie ( '<STR_LIT>' ) <EOL> elif self . session is not None : <EOL> self . session . refresh ( ) <EOL> self . set_secure_cookie ( '<STR_LIT>' , self . session . session_id , expires_days = None , expires = self . session . expires ) <EOL> super ( BaseView , self ) . finish ( chunk = chunk ) <EOL> def _create_session ( self ) : <EOL> session_id = self . get_secure_cookie ( '<STR_LIT>' ) <EOL> kw = { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : self . settings [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : self . request . remote_ip , <EOL> '<STR_LIT>' : self . request . headers . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . settings [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> } <EOL> new_session = None <EOL> old_session = None <EOL> old_session = MongoDBSession . load ( session_id ) <EOL> if old_session is None or old_session . _is_expired ( ) : <EOL> new_session = MongoDBSession ( ** kw ) <EOL> if old_session is not None : <EOL> if old_session . _should_regenerate ( ) : <EOL> old_session . refresh ( new_session_id = True ) <EOL> return old_session <EOL> return new_session <EOL> def get_session_key_and_delete ( self , key ) : <EOL> value = self . get_session_key ( key ) <EOL> self . delete_session_key ( key ) <EOL> return value <EOL> def get_session_key ( self , key ) : <EOL> try : <EOL> return self . session [ key ] <EOL> except : <EOL> return None <EOL> def delete_session_key ( self , key ) : <EOL> try : <EOL> del self . session [ key ] <EOL> except : <EOL> pass <EOL> def paginate ( self , data , page = None ) : <EOL> page_size = <NUM_LIT:100> <EOL> page = <NUM_LIT:1> if page == None else int ( page ) <EOL> page = <NUM_LIT:1> if page == <NUM_LIT:0> else page <EOL> rows = len ( data ) <EOL> total_pages = rows / page_size <EOL> total_pages = <NUM_LIT:1> if total_pages == <NUM_LIT:0> else total_pages <EOL> page = total_pages if page > total_pages else page <EOL> from_ = page_size * ( page - <NUM_LIT:1> ) <EOL> to = from_ + page_size <EOL> result = data [ from_ : to ] <EOL> pagination = { <EOL> "<STR_LIT>" : total_pages , <EOL> "<STR_LIT>" : page , <EOL> "<STR_LIT:result>" : result <EOL> } <EOL> return pagination <EOL> def render ( self , template , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = self . template_vars <EOL> rendered_template = jinja_render ( template , * args , ** kwargs ) <EOL> self . write ( rendered_template ) </s>
