Package coprs :: Package views :: Package api_ns :: Module api_general
[hide private]
[frames] | no frames]

Source Code for Module coprs.views.api_ns.api_general

  1  import datetime 
  2  import time 
  3   
  4  import base64 
  5  import flask 
  6  import urlparse 
  7   
  8  from coprs import db 
  9  from coprs import exceptions 
 10  from coprs import forms 
 11  from coprs import helpers 
 12   
 13  from coprs.views.misc import login_required, api_login_required 
 14   
 15  from coprs.views.api_ns import api_ns 
 16   
 17  from coprs.logic import builds_logic 
 18  from coprs.logic import coprs_logic 
19 20 21 @api_ns.route('/') 22 -def api_home():
23 """ Renders the home page of the api. 24 This page provides information on how to call/use the API. 25 """ 26 return flask.render_template('api.html')
27
28 29 @api_ns.route('/new/', methods=["GET", "POST"]) 30 @login_required 31 -def api_new_token():
32 """ Method use to generate a new API token for the current user. 33 """ 34 user = flask.g.user 35 copr64 = base64.b64encode('copr') + '##' 36 api_login = helpers.generate_api_token( 37 flask.current_app.config['API_TOKEN_LENGTH'] - len(copr64)) 38 user.api_login = api_login 39 user.api_token = helpers.generate_api_token( 40 flask.current_app.config['API_TOKEN_LENGTH']) 41 user.api_token_expiration = datetime.date.today() \ 42 + datetime.timedelta(days=flask.current_app.config['API_TOKEN_EXPIRATION']) 43 db.session.add(user) 44 db.session.commit() 45 return flask.redirect(flask.url_for('api_ns.api_home'))
46
47 48 @api_ns.route('/coprs/<username>/new/', methods=['POST']) 49 @api_login_required 50 -def api_new_copr(username):
51 """ Receive information from the user on how to create its new copr, 52 check their validity and create the corresponding copr. 53 54 :arg name: the name of the copr to add 55 :arg chroots: a comma separated list of chroots to use 56 :kwarg repos: a comma separated list of repository that this copr 57 can use. 58 :kwarg initial_pkgs: a comma separated list of initial packages to 59 build in this new copr 60 61 """ 62 form = forms.CoprFormFactory.create_form_cls()(csrf_enabled=False) 63 httpcode = 200 64 if form.validate_on_submit(): 65 infos = [] 66 try: 67 copr = coprs_logic.CoprsLogic.add( 68 name=form.name.data.strip(), 69 repos=" ".join(form.repos.data.split()), 70 user=flask.g.user, 71 selected_chroots=form.selected_chroots, 72 description=form.description.data, 73 instructions=form.instructions.data, 74 check_for_duplicates=True) 75 infos.append('New project was successfully created.') 76 77 if form.initial_pkgs.data: 78 builds_logic.BuildsLogic.add( 79 user=flask.g.user, 80 pkgs=" ".join(form.initial_pkgs.data.split()), 81 copr=copr) 82 83 infos.append('Initial packages were successfully ' 84 'submitted for building.') 85 86 output = {'output': 'ok', 'message': '\n'.join(infos)} 87 db.session.commit() 88 except exceptions.DuplicateException, err: 89 output = {'output': 'notok', 'error': err} 90 httpcode = 500 91 db.session.rollback() 92 93 else: 94 errormsg = 'Validation error\n' 95 if form.errors: 96 for field, emsgs in form.errors.items(): 97 errormsg += "- {0}: {1}\n".format(field, "\n".join(emsgs)) 98 99 errormsg = errormsg.replace('"', "'") 100 output = {'output': 'notok', 'error': errormsg} 101 httpcode = 500 102 103 jsonout = flask.jsonify(output) 104 jsonout.status_code = httpcode 105 return jsonout
106
107 108 @api_ns.route('/coprs/') 109 @api_ns.route('/coprs/<username>/') 110 -def api_coprs_by_owner(username=None):
111 """ Return the list of coprs owned by the given user. 112 username is taken either from GET params or from the URL itself 113 (in this order). 114 115 :arg username: the username of the person one would like to the 116 coprs of. 117 118 """ 119 username = flask.request.args.get('username', None) or username 120 httpcode = 200 121 if username: 122 query = coprs_logic.CoprsLogic.get_multiple(flask.g.user, 123 user_relation='owned', username=username, with_builds=True) 124 repos = query.all() 125 output = {'output': 'ok', 'repos': []} 126 for repo in repos: 127 yum_repos = {} 128 for build in repo.builds: 129 if build.results: 130 for chroot in repo.active_chroots: 131 release = '{chroot.os_release}-{chroot.os_version}-{chroot.arch}'.format(chroot=chroot) 132 yum_repos[release] = urlparse.urljoin(build.results, release + '/') 133 break 134 135 output['repos'].append({'name': repo.name, 136 'additional_repos': repo.repos, 137 'yum_repos': yum_repos, 138 'description': repo.description, 139 'instructions': repo.instructions}) 140 else: 141 output = {'output': 'notok', 'error': 'Invalid request'} 142 httpcode = 500 143 144 jsonout = flask.jsonify(output) 145 jsonout.status_code = httpcode 146 return jsonout
147
148 @api_ns.route('/coprs/<username>/<coprname>/new_build/', methods=["POST"]) 149 @api_login_required 150 -def copr_new_build(username, coprname):
151 form = forms.BuildForm(csrf_enabled=False) 152 copr = coprs_logic.CoprsLogic.get(flask.g.user, username, 153 coprname).first() 154 httpcode = 200 155 if not copr: 156 output = {'output': 'notok', 'error': 157 'Copr with name {0} does not exist.'.format(coprname)} 158 httpcode = 500 159 160 else: 161 if form.validate_on_submit() and flask.g.user.can_build_in(copr): 162 # we're checking authorization above for now 163 build = builds_logic.BuildsLogic.add( 164 user=flask.g.user, 165 pkgs=form.pkgs.data.replace('\n', ' '), 166 copr=copr) 167 168 if flask.g.user.proven: 169 build.memory_reqs = form.memory_reqs.data 170 build.timeout = form.timeout.data 171 172 db.session.commit() 173 174 output = {'output': 'ok', 175 'id': build.id, 176 'message': 'Build was added to {0}.'.format(coprname)} 177 else: 178 output = {'output': 'notok', 'error': 'Invalid request'} 179 httpcode = 500 180 181 jsonout = flask.jsonify(output) 182 jsonout.status_code = httpcode 183 return jsonout
184
185 186 @api_ns.route('/coprs/build_status/<build_id>/', methods=["GET"]) 187 @api_login_required 188 -def build_status(build_id):
189 if helpers.is_int(build_id): 190 build = builds_logic.BuildsLogic.get(build_id).first() 191 else: 192 build = None 193 194 if build: 195 httpcode = 200 196 output = {'output': 'ok', 197 'status': build.state} 198 else: 199 output = {'output': 'notok', 'error': 'Invalid build'} 200 httpcode = 404 201 202 jsonout = flask.jsonify(output) 203 jsonout.status_code = httpcode 204 return jsonout
205