<s> from __future__ import absolute_import <EOL> import json <EOL> from flask_restful import reqparse <EOL> from freight . api . base import ApiView <EOL> from freight . api . serializer import serialize <EOL> from freight . checks . utils import parse_checks_config <EOL> from freight . config import db <EOL> from freight . environments . utils import parse_environments_config <EOL> from freight . models import App , Repository , TaskConfig , TaskConfigType <EOL> from freight . notifiers . utils import parse_notifiers_config <EOL> from freight . providers . utils import parse_provider_config <EOL> class AppIndexApiView ( ApiView ) : <EOL> get_parser = reqparse . RequestParser ( ) <EOL> get_parser . add_argument ( '<STR_LIT:name>' , location = '<STR_LIT:args>' ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> args = self . get_parser . parse_args ( ) <EOL> qs_filters = [ ] <EOL> if args . name : <EOL> qs_filters . append ( App . name == args . name ) <EOL> app_qs = App . query . filter ( * qs_filters ) . order_by ( App . id . desc ( ) ) <EOL> return self . paginate ( app_qs , on_results = serialize ) <EOL> post_parser = reqparse . RequestParser ( ) <EOL> post_parser . add_argument ( '<STR_LIT:name>' , required = True ) <EOL> post_parser . add_argument ( '<STR_LIT>' , required = True ) <EOL> post_parser . add_argument ( '<STR_LIT>' , required = True ) <EOL> post_parser . add_argument ( '<STR_LIT>' , type = json . loads ) <EOL> post_parser . add_argument ( '<STR_LIT>' , type = json . loads ) <EOL> post_parser . add_argument ( '<STR_LIT>' , type = json . loads ) <EOL> post_parser . add_argument ( '<STR_LIT>' , type = json . loads ) <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> args = self . post_parser . parse_args ( ) <EOL> provider_config = parse_provider_config ( args . provider , args . provider_config or { } ) <EOL> checks_config = parse_checks_config ( args . checks or [ ] ) <EOL> notifiers_config = parse_notifiers_config ( args . notifiers or [ ] ) <EOL> environments_config = parse_environments_config ( args . environments or { } ) <EOL> repo = Repository . query . filter ( <EOL> Repository . url == args . repository , <EOL> ) . first ( ) <EOL> if repo is None : <EOL> repo = Repository ( url = args . repository , vcs = '<STR_LIT>' ) <EOL> db . session . add ( repo ) <EOL> db . session . flush ( ) <EOL> app = App ( <EOL> name = args . name , <EOL> repository_id = repo . id , <EOL> data = { <EOL> '<STR_LIT>' : environments_config , <EOL> } , <EOL> ) <EOL> db . session . add ( app ) <EOL> db . session . flush ( ) <EOL> deploy_config = TaskConfig ( <EOL> app_id = app . id , <EOL> type = TaskConfigType . deploy , <EOL> provider = args . provider , <EOL> data = { <EOL> '<STR_LIT>' : provider_config , <EOL> '<STR_LIT>' : notifiers_config , <EOL> '<STR_LIT>' : checks_config , <EOL> } , <EOL> ) <EOL> db . session . add ( deploy_config ) <EOL> db . session . commit ( ) <EOL> return self . respond ( serialize ( app ) , status_code = <NUM_LIT> ) </s>
