Package coprs :: Package rest_api :: Package resources :: Module build_task
[hide private]
[frames] | no frames]

Source Code for Module coprs.rest_api.resources.build_task

  1  # coding: utf-8 
  2   
  3  from flask import url_for 
  4  from flask_restful import Resource 
  5  from flask_restful import Resource, reqparse 
  6  from flask_restful.reqparse import Argument 
  7   
  8  from marshmallow import pprint 
  9  from marshmallow import Schema, fields 
 10  from marshmallow import Schema, fields, validates_schema, ValidationError, validate 
 11   
 12  from coprs.helpers import StatusEnum 
 13  from coprs.rest_api.common import render_build_task 
 14  from ...exceptions import MalformedArgumentException 
 15  from ...logic.builds_logic import BuildsLogic, BuildChrootsLogic 
 16  from ..exceptions import MalformedRequest 
 17  from ..util import get_one_safe, get_request_parser 
18 19 20 # todo: add redirect from /build_tasks/<int:build_id> -> /build_tasks?build_id=<build_id> 21 # class BuildTaskListRedirectBuildIdR(Resource): 22 # def get(self, build_id): 23 # resp = make_response("", 302) 24 # resp.headers["Location"] == url_for(".buildtasklistr", build_id=build_id) 25 26 27 -class BuildTaskListR(Resource):
28 state_choices = StatusEnum.vals.keys() 29
30 - def get(self):
31 32 parser = get_request_parser() 33 34 parser.add_argument('owner', type=str,) 35 parser.add_argument('project_id', type=int) 36 parser.add_argument('build_id', type=int) 37 parser.add_argument('group', type=str) 38 39 parser.add_argument('limit', type=int) 40 parser.add_argument('offset', type=int) 41 42 parser.add_argument( 43 'state', type=str, choices=self.state_choices, 44 help=u"allowed states: {}".format(" ".join(self.state_choices))) 45 46 req_args = parser.parse_args() 47 48 self_params = dict(req_args) 49 50 query = BuildChrootsLogic.get_multiply() 51 if self_params.get("build_id") is not None: 52 query = BuildChrootsLogic.filter_by_build_id( 53 query, self_params["build_id"]) 54 elif self_params.get("project_id") is not None: 55 query = BuildChrootsLogic.filter_by_project_id( 56 query, self_params["project_id"]) 57 elif self_params.get("owner") is not None: 58 query = BuildChrootsLogic.filter_by_project_user_name( 59 query, self_params["owner"]) 60 elif self_params.get("group") is not None: 61 query = BuildChrootsLogic.filter_by_group_name(query, req_args["group"]) 62 63 state = self_params.get("state") 64 if state: 65 query = BuildChrootsLogic.filter_by_state(query, state) 66 67 if req_args["limit"] is not None: 68 limit = req_args["limit"] 69 if limit <= 0 or limit > 100: 70 limit = 100 71 else: 72 limit = 100 73 self_params["limit"] = limit 74 query = query.limit(limit) 75 76 if "offset" in self_params is not None: 77 query = query.offset(self_params["offset"]) 78 79 build_chroots = query.all() 80 return { 81 "build_tasks": [ 82 render_build_task(chroot) 83 for chroot in build_chroots 84 ], 85 "_links": { 86 "self": {"href": url_for(".buildtasklistr", **self_params)} 87 } 88 }
89
90 91 -class BuildTaskR(Resource):
92 93 @staticmethod
94 - def _get_chroot_safe(build_id, name):
95 try: 96 chroot = get_one_safe( 97 BuildChrootsLogic.get_by_build_id_and_name(build_id, name), 98 "Build task {} for build {} not found" 99 ) 100 except MalformedArgumentException as err: 101 raise MalformedRequest("Bad mock chroot name: {}".format(err)) 102 return chroot
103
104 - def get(self, build_id, name):
105 chroot = self._get_chroot_safe(build_id, name) 106 return render_build_task(chroot)
107 108 # todo: add put method: allows only to pass status: cancelled to cancel build 109