<s> import six <EOL> from mongoengine . queryset import Q <EOL> from st2common import log as logging <EOL> from st2api . controllers import resource <EOL> from st2common . models . api . rule import RuleViewAPI <EOL> from st2common . models . api . base import jsexpose <EOL> from st2common . models . system . common import ResourceReference <EOL> from st2common . persistence . action import Action <EOL> from st2common . persistence . rule import Rule <EOL> from st2common . persistence . trigger import TriggerType , Trigger <EOL> from st2common . rbac . types import PermissionType <EOL> from st2common . rbac . decorators import request_user_has_permission <EOL> from st2common . rbac . decorators import request_user_has_resource_db_permission <EOL> http_client = six . moves . http_client <EOL> LOG = logging . getLogger ( __name__ ) <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class RuleViewController ( resource . ContentPackResourceController ) : <EOL> """<STR_LIT>""" <EOL> model = RuleViewAPI <EOL> access = Rule <EOL> supported_filters = { <EOL> '<STR_LIT:name>' : '<STR_LIT:name>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> query_options = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:name>' ] <EOL> } <EOL> include_reference = True <EOL> @ request_user_has_permission ( permission_type = PermissionType . RULE_LIST ) <EOL> @ jsexpose ( ) <EOL> def get_all ( self , ** kwargs ) : <EOL> rules = self . _get_all ( ** kwargs ) <EOL> return self . _append_view_properties ( rules ) <EOL> @ request_user_has_resource_db_permission ( permission_type = PermissionType . RULE_VIEW ) <EOL> @ jsexpose ( arg_types = [ str ] ) <EOL> def get_one ( self , ref_or_id ) : <EOL> rule = self . _get_one ( ref_or_id ) <EOL> return self . _append_view_properties ( [ rule ] ) [ <NUM_LIT:0> ] <EOL> def _append_view_properties ( self , rules ) : <EOL> action_by_refs , trigger_by_refs , trigger_type_by_refs = self . _get_referenced_models ( rules ) <EOL> for rule in rules : <EOL> action_db = action_by_refs . get ( rule . action [ '<STR_LIT>' ] , None ) <EOL> rule . action [ '<STR_LIT:description>' ] = action_db . description if action_db else '<STR_LIT>' <EOL> trigger_db = trigger_by_refs . get ( rule . trigger [ '<STR_LIT>' ] , None ) <EOL> rule . trigger [ '<STR_LIT:description>' ] = trigger_db . description if trigger_db else '<STR_LIT>' <EOL> if not rule . trigger [ '<STR_LIT:description>' ] : <EOL> trigger_type_db = trigger_type_by_refs . get ( rule . trigger [ '<STR_LIT:type>' ] , None ) <EOL> rule . trigger [ '<STR_LIT:description>' ] = trigger_type_db . description if trigger_type_db else '<STR_LIT>' <EOL> return rules <EOL> def _get_referenced_models ( self , rules ) : <EOL> """<STR_LIT>""" <EOL> action_refs = set ( ) <EOL> trigger_refs = set ( ) <EOL> trigger_type_refs = set ( ) <EOL> for rule in rules : <EOL> action_refs . add ( rule . action [ '<STR_LIT>' ] ) <EOL> trigger_refs . add ( rule . trigger [ '<STR_LIT>' ] ) <EOL> trigger_type_refs . add ( rule . trigger [ '<STR_LIT:type>' ] ) <EOL> action_by_refs = { } <EOL> trigger_by_refs = { } <EOL> trigger_type_by_refs = { } <EOL> def ref_query_args ( ref ) : <EOL> return { '<STR_LIT>' : ref } <EOL> def name_pack_query_args ( ref ) : <EOL> resource_ref = ResourceReference . from_string_reference ( ref = ref ) <EOL> return { '<STR_LIT:name>' : resource_ref . name , '<STR_LIT>' : resource_ref . pack } <EOL> action_dbs = self . _get_entities ( model_persistence = Action , <EOL> refs = action_refs , <EOL> query_args = ref_query_args ) <EOL> for action_db in action_dbs : <EOL> action_by_refs [ action_db . ref ] = action_db <EOL> trigger_dbs = self . _get_entities ( model_persistence = Trigger , <EOL> refs = trigger_refs , <EOL> query_args = name_pack_query_args ) <EOL> for trigger_db in trigger_dbs : <EOL> trigger_by_refs [ trigger_db . get_reference ( ) . ref ] = trigger_db <EOL> trigger_type_dbs = self . _get_entities ( model_persistence = TriggerType , <EOL> refs = trigger_type_refs , <EOL> query_args = name_pack_query_args ) <EOL> for trigger_type_db in trigger_type_dbs : <EOL> trigger_type_by_refs [ trigger_type_db . get_reference ( ) . ref ] = trigger_type_db <EOL> return ( action_by_refs , trigger_by_refs , trigger_type_by_refs ) <EOL> def _get_entities ( self , model_persistence , refs , query_args ) : <EOL> """<STR_LIT>""" <EOL> q = None <EOL> for ref in refs : <EOL> if not q : <EOL> q = Q ( ** query_args ( ref ) ) <EOL> else : <EOL> q |= Q ( ** query_args ( ref ) ) <EOL> if q : <EOL> return model_persistence . _get_impl ( ) . model . objects ( q ) <EOL> return [ ] </s>
