<s> import time <EOL> from flask import request , Blueprint <EOL> from flask_restful import Resource , abort <EOL> from flask_login import current_user , login_required <EOL> from peewee import DoesNotExist <EOL> from redash import settings <EOL> from redash . tasks import record_event as record_event_task <EOL> from redash . models import ApiUser <EOL> from redash . authentication import current_org <EOL> routes = Blueprint ( '<STR_LIT>' , __name__ , template_folder = settings . fix_assets_path ( '<STR_LIT>' ) ) <EOL> class BaseResource ( Resource ) : <EOL> decorators = [ login_required ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BaseResource , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _user = None <EOL> def dispatch_request ( self , * args , ** kwargs ) : <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> return super ( BaseResource , self ) . dispatch_request ( * args , ** kwargs ) <EOL> @ property <EOL> def current_user ( self ) : <EOL> return current_user . _get_current_object ( ) <EOL> @ property <EOL> def current_org ( self ) : <EOL> return current_org . _get_current_object ( ) <EOL> def record_event ( self , options ) : <EOL> record_event ( self . current_org , self . current_user , options ) <EOL> def record_event ( org , user , options ) : <EOL> if isinstance ( user , ApiUser ) : <EOL> options . update ( { <EOL> '<STR_LIT>' : user . name , <EOL> '<STR_LIT>' : org . id <EOL> } ) <EOL> else : <EOL> options . update ( { <EOL> '<STR_LIT>' : user . id , <EOL> '<STR_LIT>' : org . id <EOL> } ) <EOL> options . update ( { <EOL> '<STR_LIT>' : request . user_agent . string , <EOL> '<STR_LIT>' : request . remote_addr <EOL> } ) <EOL> if '<STR_LIT>' not in options : <EOL> options [ '<STR_LIT>' ] = int ( time . time ( ) ) <EOL> record_event_task . delay ( options ) <EOL> def require_fields ( req , fields ) : <EOL> for f in fields : <EOL> if f not in req : <EOL> abort ( <NUM_LIT> ) <EOL> def get_object_or_404 ( fn , * args , ** kwargs ) : <EOL> try : <EOL> return fn ( * args , ** kwargs ) <EOL> except DoesNotExist : <EOL> abort ( <NUM_LIT> ) <EOL> def org_scoped_rule ( rule ) : <EOL> if settings . MULTI_ORG : <EOL> return "<STR_LIT>" . format ( rule ) <EOL> return rule </s>
