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