<s> from __future__ import absolute_import , division , unicode_literals <EOL> from flask_restful . reqparse import RequestParser <EOL> from sqlalchemy . exc import IntegrityError <EOL> from werkzeug . datastructures import FileStorage <EOL> from changes . api . base import APIView <EOL> from changes . config import db <EOL> from changes . constants import Result <EOL> from changes . models import Artifact , JobStep <EOL> class JobStepArtifactsAPIView ( APIView ) : <EOL> parser = RequestParser ( ) <EOL> parser . add_argument ( '<STR_LIT:name>' , type = unicode , required = True ) <EOL> parser . add_argument ( '<STR_LIT:file>' , type = FileStorage , dest = '<STR_LIT>' , <EOL> location = '<STR_LIT>' , required = True ) <EOL> def get ( self , step_id ) : <EOL> """<STR_LIT>""" <EOL> if not JobStep . query . get ( step_id ) : <EOL> return self . respond ( { } , status_code = <NUM_LIT> ) <EOL> artifacts = Artifact . query . filter_by ( step_id = step_id ) . all ( ) <EOL> return self . respond ( { '<STR_LIT>' : artifacts } ) <EOL> def post ( self , step_id ) : <EOL> """<STR_LIT>""" <EOL> step = JobStep . query . get ( step_id ) <EOL> if step is None : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> if step . result == Result . aborted : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> args = self . parser . parse_args ( ) <EOL> artifact = Artifact ( <EOL> name = args . name , <EOL> step_id = step . id , <EOL> job_id = step . job_id , <EOL> project_id = step . project_id , <EOL> ) <EOL> try : <EOL> db . session . add ( artifact ) <EOL> db . session . flush ( ) <EOL> except IntegrityError : <EOL> db . session . rollback ( ) <EOL> exists = True <EOL> else : <EOL> exists = False <EOL> if exists : <EOL> existing_msg = { "<STR_LIT:error>" : "<STR_LIT>" } <EOL> return self . respond ( existing_msg , status_code = <NUM_LIT> ) <EOL> step_id = artifact . step_id . hex <EOL> artifact . file . save ( <EOL> args . artifact_file , <EOL> '<STR_LIT>' . format ( <EOL> step_id [ : <NUM_LIT:4> ] , step_id [ <NUM_LIT:4> : ] , <EOL> artifact . id . hex , artifact . name <EOL> ) , <EOL> ) <EOL> db . session . add ( artifact ) <EOL> db . session . commit ( ) <EOL> return { '<STR_LIT:id>' : artifact . id . hex } , <NUM_LIT> </s>
