<s> from security_monkey . common . PolicyDiff import PolicyDiff <EOL> from security_monkey . views import AuthenticatedService <EOL> from security_monkey . views import __check_auth__ <EOL> from security_monkey . views import REVISION_FIELDS <EOL> from security_monkey . views import REVISION_COMMENT_FIELDS <EOL> from security_monkey . views import ITEM_FIELDS <EOL> from security_monkey . datastore import Item <EOL> from security_monkey . datastore import Account <EOL> from security_monkey . datastore import Technology <EOL> from security_monkey . datastore import ItemRevision <EOL> from security_monkey import rbac <EOL> from flask_restful import marshal , reqparse <EOL> from sqlalchemy . sql . expression import cast <EOL> from sqlalchemy import String <EOL> class RevisionGet ( AuthenticatedService ) : <EOL> decorators = [ <EOL> rbac . allow ( [ "<STR_LIT>" ] , [ "<STR_LIT:GET>" ] ) <EOL> ] <EOL> def __init__ ( self ) : <EOL> self . reqparse = reqparse . RequestParser ( ) <EOL> super ( RevisionGet , self ) . __init__ ( ) <EOL> def get ( self , revision_id ) : <EOL> """<STR_LIT>""" <EOL> auth , retval = __check_auth__ ( self . auth_dict ) <EOL> if auth : <EOL> return retval <EOL> query = ItemRevision . query . filter ( ItemRevision . id == revision_id ) <EOL> result = query . first ( ) <EOL> comments = [ ] <EOL> for comment in result . comments : <EOL> comment_marshaled = marshal ( comment , REVISION_COMMENT_FIELDS ) <EOL> comments . append ( dict ( <EOL> comment_marshaled . items ( ) + <EOL> { '<STR_LIT:user>' : comment . user . email } . items ( ) <EOL> ) ) <EOL> revision_marshaled = marshal ( result , REVISION_FIELDS ) <EOL> revision_marshaled = dict ( <EOL> revision_marshaled . items ( ) + <EOL> { '<STR_LIT>' : result . config } . items ( ) + <EOL> { '<STR_LIT>' : self . auth_dict } . items ( ) + <EOL> { '<STR_LIT>' : comments } . items ( ) <EOL> ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = int , default = None , location = '<STR_LIT:args>' ) <EOL> args = self . reqparse . parse_args ( ) <EOL> compare_id = args . pop ( '<STR_LIT>' , None ) <EOL> if compare_id : <EOL> query = ItemRevision . query . filter ( ItemRevision . id == compare_id ) <EOL> compare_result = query . first ( ) <EOL> pdiff = PolicyDiff ( result . config , compare_result . config ) <EOL> revision_marshaled = dict ( <EOL> revision_marshaled . items ( ) + <EOL> { '<STR_LIT>' : pdiff . produceDiffHTML ( ) } . items ( ) <EOL> ) <EOL> return revision_marshaled , <NUM_LIT:200> <EOL> class RevisionList ( AuthenticatedService ) : <EOL> decorators = [ <EOL> rbac . allow ( [ "<STR_LIT>" ] , [ "<STR_LIT:GET>" ] ) <EOL> ] <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reqparse . add_argument ( '<STR_LIT:count>' , type = int , default = <NUM_LIT:30> , location = '<STR_LIT:args>' ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = int , default = <NUM_LIT:1> , location = '<STR_LIT:args>' ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = str , default = None , location = '<STR_LIT:args>' ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = str , default = None , location = '<STR_LIT:args>' ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = str , default = None , location = '<STR_LIT:args>' ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = str , default = None , location = '<STR_LIT:args>' ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = str , default = None , location = '<STR_LIT:args>' ) <EOL> self . reqparse . add_argument ( '<STR_LIT>' , type = str , default = None , location = '<STR_LIT:args>' ) <EOL> args = self . reqparse . parse_args ( ) <EOL> page = args . pop ( '<STR_LIT>' , None ) <EOL> count = args . pop ( '<STR_LIT:count>' , None ) <EOL> for k , v in args . items ( ) : <EOL> if not v : <EOL> del args [ k ] <EOL> query = ItemRevision . query . join ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in args : <EOL> regions = args [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> query = query . filter ( Item . region . in_ ( regions ) ) <EOL> if '<STR_LIT>' in args : <EOL> accounts = args [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> query = query . join ( ( Account , Account . id == Item . account_id ) ) <EOL> query = query . filter ( Account . name . in_ ( accounts ) ) <EOL> if '<STR_LIT>' in args : <EOL> technologies = args [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> query = query . join ( ( Technology , Technology . id == Item . tech_id ) ) <EOL> query = query . filter ( Technology . name . in_ ( technologies ) ) <EOL> if '<STR_LIT>' in args : <EOL> names = args [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> query = query . filter ( Item . name . in_ ( names ) ) <EOL> if '<STR_LIT>' in args : <EOL> active = args [ '<STR_LIT>' ] . lower ( ) == "<STR_LIT:true>" <EOL> query = query . filter ( ItemRevision . active == active ) <EOL> if '<STR_LIT>' in args : <EOL> searchconfig = args [ '<STR_LIT>' ] <EOL> query = query . filter ( cast ( ItemRevision . config , String ) . ilike ( '<STR_LIT>' . format ( searchconfig ) ) ) <EOL> query = query . order_by ( ItemRevision . date_created . desc ( ) ) <EOL> revisions = query . paginate ( page , count ) <EOL> marshaled_dict = { <EOL> '<STR_LIT>' : revisions . page , <EOL> '<STR_LIT>' : revisions . total , <EOL> '<STR_LIT>' : self . auth_dict <EOL> } <EOL> items_marshaled = [ ] <EOL> for revision in revisions . items : <EOL> item_marshaled = marshal ( revision . item . __dict__ , ITEM_FIELDS ) <EOL> revision_marshaled = marshal ( revision . __dict__ , REVISION_FIELDS ) <EOL> account_marshaled = { '<STR_LIT>' : revision . item . account . name } <EOL> technology_marshaled = { '<STR_LIT>' : revision . item . technology . name } <EOL> merged_marshaled = dict ( <EOL> item_marshaled . items ( ) + <EOL> revision_marshaled . items ( ) + <EOL> account_marshaled . items ( ) + <EOL> technology_marshaled . items ( ) ) <EOL> items_marshaled . append ( merged_marshaled ) <EOL> marshaled_dict [ '<STR_LIT>' ] = items_marshaled <EOL> marshaled_dict [ '<STR_LIT:count>' ] = len ( items_marshaled ) <EOL> return marshaled_dict , <NUM_LIT:200> </s>
