<s> import uuid <EOL> from st2common . constants . triggers import TRIGGER_INSTANCE_STATUSES <EOL> from st2common . util import isotime <EOL> from st2common . models . api . base import BaseAPI <EOL> from st2common . models . api . tag import TagsHelper <EOL> from st2common . models . db . trigger import TriggerTypeDB , TriggerDB , TriggerInstanceDB <EOL> from st2common . models . system . common import ResourceReference <EOL> DATE_FORMAT = '<STR_LIT>' <EOL> class TriggerTypeAPI ( BaseAPI ) : <EOL> model = TriggerTypeDB <EOL> schema = { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT:default>' : None <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT:name>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT:description>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT:default>' : { } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT:default>' : { } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:type>' : '<STR_LIT:object>' } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : False <EOL> } <EOL> @ classmethod <EOL> def to_model ( cls , trigger_type ) : <EOL> name = getattr ( trigger_type , '<STR_LIT:name>' , None ) <EOL> description = getattr ( trigger_type , '<STR_LIT:description>' , None ) <EOL> pack = getattr ( trigger_type , '<STR_LIT>' , None ) <EOL> payload_schema = getattr ( trigger_type , '<STR_LIT>' , { } ) <EOL> parameters_schema = getattr ( trigger_type , '<STR_LIT>' , { } ) <EOL> tags = TagsHelper . to_model ( getattr ( trigger_type , '<STR_LIT>' , [ ] ) ) <EOL> model = cls . model ( name = name , description = description , pack = pack , <EOL> payload_schema = payload_schema , parameters_schema = parameters_schema , <EOL> tags = tags ) <EOL> return model <EOL> @ classmethod <EOL> def from_model ( cls , model , mask_secrets = False ) : <EOL> triggertype = cls . _from_model ( model , mask_secrets = mask_secrets ) <EOL> triggertype [ '<STR_LIT>' ] = TagsHelper . from_model ( model . tags ) <EOL> return cls ( ** triggertype ) <EOL> class TriggerAPI ( BaseAPI ) : <EOL> model = TriggerDB <EOL> schema = { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT:default>' : None <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT:name>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT:type>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' <EOL> } , <EOL> '<STR_LIT:description>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : False <EOL> } <EOL> @ classmethod <EOL> def from_model ( cls , model , mask_secrets = False ) : <EOL> trigger = cls . _from_model ( model , mask_secrets = mask_secrets ) <EOL> trigger . pop ( '<STR_LIT>' , None ) <EOL> return cls ( ** trigger ) <EOL> @ classmethod <EOL> def to_model ( cls , trigger ) : <EOL> name = getattr ( trigger , '<STR_LIT:name>' , None ) <EOL> description = getattr ( trigger , '<STR_LIT:description>' , None ) <EOL> pack = getattr ( trigger , '<STR_LIT>' , None ) <EOL> _type = getattr ( trigger , '<STR_LIT:type>' , None ) <EOL> parameters = getattr ( trigger , '<STR_LIT>' , { } ) <EOL> if _type and not parameters : <EOL> trigger_type_ref = ResourceReference . from_string_reference ( _type ) <EOL> name = trigger_type_ref . name <EOL> if hasattr ( trigger , '<STR_LIT:name>' ) and trigger . name : <EOL> name = trigger . name <EOL> else : <EOL> name = str ( uuid . uuid4 ( ) ) <EOL> model = cls . model ( name = name , description = description , pack = pack , type = _type , <EOL> parameters = parameters ) <EOL> return model <EOL> class TriggerInstanceAPI ( BaseAPI ) : <EOL> model = TriggerInstanceDB <EOL> schema = { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : isotime . ISO8601_UTC_REGEX <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:object>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT:default>' : None , <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT:status>' : { <EOL> '<STR_LIT:type>' : '<STR_LIT:string>' , <EOL> '<STR_LIT:default>' : None , <EOL> '<STR_LIT>' : TRIGGER_INSTANCE_STATUSES <EOL> } <EOL> } , <EOL> '<STR_LIT>' : False <EOL> } <EOL> @ classmethod <EOL> def from_model ( cls , model , mask_secrets = False ) : <EOL> instance = cls . _from_model ( model , mask_secrets = mask_secrets ) <EOL> instance [ '<STR_LIT>' ] = isotime . format ( instance [ '<STR_LIT>' ] , offset = False ) <EOL> return cls ( ** instance ) <EOL> @ classmethod <EOL> def to_model ( cls , instance ) : <EOL> trigger = instance . trigger <EOL> payload = instance . payload <EOL> occurrence_time = isotime . parse ( instance . occurrence_time ) <EOL> status = instance . status <EOL> model = cls . model ( trigger = trigger , payload = payload , occurrence_time = occurrence_time , <EOL> status = status ) <EOL> return model </s>
