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():
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"])
170