1 import flask
2 from . import query_params, get_copr, pagination, Paginator, GET, POST, PUT, DELETE
3 from .json2form import get_form_compatible_data, get_input_dict
4 from coprs import db, models, forms
5 from coprs.views.misc import api_login_required
6 from coprs.views.apiv3_ns import apiv3_ns
7 from coprs.logic.coprs_logic import CoprsLogic, CoprChrootsLogic, MockChrootsLogic
8 from coprs.logic.complex_logic import ComplexLogic
9 from coprs.exceptions import (DuplicateException, NonAdminCannotCreatePersistentProject,
10 NonAdminCannotDisableAutoPrunning, ActionInProgressException,
11 InsufficientRightsException, BadRequest, ObjectNotFound)
15 return {
16 "id": copr.id,
17 "name": copr.name,
18 "ownername": copr.owner_name,
19 "full_name": copr.full_name,
20 "homepage": copr.homepage,
21 "contact": copr.contact,
22 "description": copr.description,
23 "instructions": copr.instructions,
24 "devel_mode": copr.devel_mode,
25 "persistent": copr.persistent,
26 "unlisted_on_hp": copr.unlisted_on_hp,
27 "auto_prune": copr.auto_prune,
28 "chroot_repos": CoprsLogic.get_yum_repos(copr, empty=True),
29 "additional_repos": copr.repos_list,
30 "enable_net": copr.build_enable_net,
31 "use_bootstrap_container": copr.use_bootstrap_container,
32 }
33
36 replace = {
37 "devel_mode": "disable_createrepo",
38 "additional_repos": "repos",
39 }
40 output = input.copy()
41 for from_name, to_name in replace.items():
42 if from_name not in output:
43 continue
44 output[to_name] = output.pop(from_name)
45 return output
46
49 inserted = set(input["chroots"] or [])
50 allowed = {x.name for x in allowed_chroots}
51 unexpected = inserted - allowed
52 if unexpected:
53 raise BadRequest("Unexpected chroot: {}".format(", ".join(unexpected)))
54
55
56 @apiv3_ns.route("/project", methods=GET)
57 @query_params()
58 -def get_project(ownername, projectname):
61
62
63 @apiv3_ns.route("/project/list", methods=GET)
64 @pagination()
65 @query_params()
66 -def get_project_list(ownername=None, **kwargs):
81
82
83 @apiv3_ns.route("/project/search", methods=GET)
84 @pagination()
85 @query_params()
86
87 -def search_projects(query, **kwargs):
95
96
97 @apiv3_ns.route("/project/add/<ownername>", methods=POST)
98 @api_login_required
99 -def add_project(ownername):
100 data = rename_fields(get_form_compatible_data())
101 form = forms.CoprFormFactory.create_form_cls()(data, meta={'csrf': False})
102
103 if not form.validate_on_submit():
104 raise BadRequest(form.errors)
105 validate_chroots(get_input_dict(), MockChrootsLogic.get_multiple())
106
107 group = None
108 if ownername[0] == "@":
109 group = ComplexLogic.get_group_by_name_safe(ownername[1:])
110
111 try:
112 copr = CoprsLogic.add(
113 name=form.name.data.strip(),
114 repos=" ".join(form.repos.data.split()),
115 user=flask.g.user,
116 selected_chroots=form.selected_chroots,
117 description=form.description.data,
118 instructions=form.instructions.data,
119 check_for_duplicates=True,
120 unlisted_on_hp=form.unlisted_on_hp.data,
121 build_enable_net=form.enable_net.data,
122 group=group,
123 persistent=form.persistent.data,
124 auto_prune=form.auto_prune.data,
125 use_bootstrap_container=form.use_bootstrap_container.data,
126 homepage=form.homepage.data,
127 contact=form.contact.data,
128 disable_createrepo=form.disable_createrepo.data,
129 delete_after_days=form.delete_after_days.data,
130 )
131 db.session.commit()
132 except (DuplicateException,
133 NonAdminCannotCreatePersistentProject,
134 NonAdminCannotDisableAutoPrunning) as err:
135 db.session.rollback()
136 raise err
137 return flask.jsonify(to_dict(copr))
138
139
140 @apiv3_ns.route("/project/edit/<ownername>/<projectname>", methods=PUT)
141 @api_login_required
142 -def edit_project(ownername, projectname):
143 copr = get_copr(ownername, projectname)
144 data = rename_fields(get_form_compatible_data())
145 form = forms.CoprModifyForm(data, meta={'csrf': False})
146
147 if not form.validate_on_submit():
148 raise BadRequest(form.errors)
149 validate_chroots(get_input_dict(), MockChrootsLogic.get_multiple())
150
151 for field in form:
152 if field.data is None or field.name in ["csrf_token", "chroots"]:
153 continue
154 if field.name not in data.keys():
155 continue
156 setattr(copr, field.name, field.data)
157
158 if form.chroots.data:
159 CoprChrootsLogic.update_from_names(
160 flask.g.user, copr, form.chroots.data)
161
162 try:
163 CoprsLogic.update(flask.g.user, copr)
164 if copr.group:
165 _ = copr.group.id
166 db.session.commit()
167 except (ActionInProgressException,
168 InsufficientRightsException,
169 NonAdminCannotDisableAutoPrunning) as ex:
170 db.session.rollback()
171 raise ex
172
173 return flask.jsonify(to_dict(copr))
174
175
176 @apiv3_ns.route("/project/fork/<ownername>/<projectname>", methods=PUT)
177 @api_login_required
178 -def fork_project(ownername, projectname):
207
208
209 @apiv3_ns.route("/project/delete/<ownername>/<projectname>", methods=DELETE)
210 @api_login_required
211 -def delete_project(ownername, projectname):
228