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():
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):
184
185
186 @api_ns.route('/coprs/build_status/<build_id>/', methods=["GET"])
187 @api_login_required
188 -def build_status(build_id):
205