def edit_project(ownername, projectname): copr = get_copr(ownername, projectname) data = rename_fields(get_form_compatible_data()) form = forms.CoprModifyForm(data, meta={'csrf': False}) if not form.validate_on_submit(): raise BadRequest(form.errors) validate_chroots(get_input_dict(), MockChrootsLogic.get_multiple()) for field in form: if field.data is None or field.name in ["csrf_token", "chroots"]: continue if field.name not in data.keys(): continue setattr(copr, field.name, field.data) if form.chroots.data: CoprChrootsLogic.update_from_names(flask.g.user, copr, form.chroots.data) try: CoprsLogic.update(flask.g.user, copr) if copr.group: # load group.id _ = copr.group.id db.session.commit() except (ActionInProgressException, InsufficientRightsException, NonAdminCannotDisableAutoPrunning) as ex: db.session.rollback() raise ex return flask.jsonify(to_dict(copr))
def comment_chroot(chrootname, comment): """ Add comment to a mock_chroot. """ chroot = MockChrootsLogic.get_from_name(chrootname).first() if not chroot: print("There is no mock chroot named {0}.".format(chrootname)) return chroot.comment = comment db.session.commit()
def list_chroots(): chroots = MockChrootsLogic.active_names_with_comments() response = {} for chroot, comment in chroots: if comment: response[chroot] = html2text(comment).strip("\n") else: response[chroot] = "" return flask.jsonify(response)
def __call__(self, form, field): # Allow it to be truly optional and has None value if not field.data: return selected = set(field.data.split()) enabled = set(MockChrootsLogic.active_names()) if selected - enabled: raise wtforms.ValidationError( "Such chroot is not available: {}".format(", ".join(selected - enabled)))
def render_copr_edit(copr, form, view): if not form: form = forms.CoprFormFactory.create_form_cls(copr.mock_chroots, copr=copr)(obj=copr) comments = {} for chroot in MockChrootsLogic.get_multiple(active_only=True): comments[chroot.name] = chroot.comment return flask.render_template("coprs/detail/settings/edit.html", copr=copr, form=form, view=view, comments=comments)
def copr_add(username=None, group_name=None): form = forms.CoprFormFactory.create_form_cls()() comments = {} for chroot in MockChrootsLogic.get_multiple(active_only=True): comments[chroot.name] = chroot.comment if group_name: group = ComplexLogic.get_group_by_name_safe(group_name) return flask.render_template("coprs/group_add.html", form=form, group=group, comments=comments) return flask.render_template("coprs/add.html", form=form, comments=comments)
def add_project(ownername): data = rename_fields(get_form_compatible_data()) form = forms.CoprFormFactory.create_form_cls()(data, meta={'csrf': False}) if not form.validate_on_submit(): raise BadRequest(form.errors) validate_chroots(get_input_dict(), MockChrootsLogic.get_multiple()) group = None if ownername[0] == "@": group = ComplexLogic.get_group_by_name_safe(ownername[1:]) try: copr = CoprsLogic.add( name=form.name.data.strip(), repos=" ".join(form.repos.data.split()), user=flask.g.user, selected_chroots=form.selected_chroots, description=form.description.data, instructions=form.instructions.data, check_for_duplicates=True, unlisted_on_hp=form.unlisted_on_hp.data, build_enable_net=form.enable_net.data, group=group, persistent=form.persistent.data, auto_prune=form.auto_prune.data, use_bootstrap_container=form.use_bootstrap_container.data, homepage=form.homepage.data, contact=form.contact.data, disable_createrepo=form.disable_createrepo.data, delete_after_days=form.delete_after_days.data, multilib=form.multilib.data, module_hotfixes=form.module_hotfixes.data, ) db.session.commit() except (DuplicateException, NonAdminCannotCreatePersistentProject, NonAdminCannotDisableAutoPrunning) as err: db.session.rollback() raise err return flask.jsonify(to_dict(copr))
def validate_chroot_blacklist(form, field): if field.data: string = field.data fields = [x.lstrip().rstrip() for x in string.split(',')] for field in fields: pattern = r'^[a-z0-9-*]+$' if not re.match(pattern, field): raise wtforms.ValidationError( 'Pattern "{0}" does not match "{1}"'.format( field, pattern)) matched = set() all_chroots = MockChrootsLogic.active_names() for chroot in all_chroots: if fnmatch(chroot, field): matched.add(chroot) if not matched: raise wtforms.ValidationError( 'no chroot matched by pattern "{0}"'.format(field)) if matched == all_chroots: raise wtforms.ValidationError( 'patterns are black-listing all chroots')
def create_form_cls(mock_chroots=None, user=None, group=None, copr=None): class F(FlaskForm): # also use id here, to be able to find out whether user # is updating a copr if so, we don't want to shout # that name already exists id = wtforms.HiddenField() group_id = wtforms.HiddenField() name = wtforms.StringField("Name", validators=[ wtforms.validators.DataRequired(), NameCharactersValidator(), CoprUniqueNameValidator( user=user, group=group), NameNotNumberValidator() ]) homepage = wtforms.StringField("Homepage", validators=[ wtforms.validators.Optional(), wtforms.validators.URL() ]) contact = wtforms.StringField( "Contact", validators=[wtforms.validators.Optional(), EmailOrURL()]) description = wtforms.TextAreaField("Description") instructions = wtforms.TextAreaField("Instructions") delete_after_days = wtforms.IntegerField( "Delete after days", validators=[ wtforms.validators.Optional(), wtforms.validators.NumberRange(min=0, max=60), ], render_kw={'disabled': bool(copr and copr.persistent)}) repos = wtforms.TextAreaField("External Repositories", validators=[UrlRepoListValidator()], filters=[StringListFilter()]) initial_pkgs = wtforms.TextAreaField( "Initial packages to build", validators=[UrlListValidator(), UrlSrpmListValidator()], filters=[StringListFilter()]) disable_createrepo = wtforms.BooleanField( default=False, label="Create repositories manually", description="""Repository meta data is normally refreshed after each build. If you want to do this manually, turn this option on.""", false_values=FALSE_VALUES) unlisted_on_hp = wtforms.BooleanField( "Project will not be listed on home page", default=False, false_values=FALSE_VALUES) persistent = wtforms.BooleanField( "Protect project and its builds against deletion", description="""Project's builds and the project itself cannot be deleted by any means. This option is set once and for all (this option can not be changed after project is created).""", render_kw={'disabled': bool(copr)}, default=False, false_values=FALSE_VALUES) auto_prune = wtforms.BooleanField( "Old builds will be deleted automatically", default=True, false_values=FALSE_VALUES, description="""Build will be deleted only if there is a newer build (with respect to package version) and it is older than 14 days""") use_bootstrap_container = wtforms.BooleanField( "Enable mock's use_bootstrap_container experimental feature", description="""This will make the build slower but it has an advantage that the dnf _from_ the given chroot will be used to setup the chroot (otherwise host system dnf and rpm is used)""", default=False, false_values=FALSE_VALUES) follow_fedora_branching = wtforms.BooleanField( "Follow Fedora branching", description="""When Fedora is branched from rawhide, the respective chroots for the new branch are automatically created for you (as soon as they are available) as rawhide chroot forks.""", default=True, false_values=FALSE_VALUES) # Deprecated, use `enable_net` instead build_enable_net = wtforms.BooleanField( "Enable internet access during builds", default=False, false_values=FALSE_VALUES) enable_net = wtforms.BooleanField( "Enable internet access during builds", default=False, false_values=FALSE_VALUES) @property def selected_chroots(self): selected = [] for ch in self.chroots_list: if getattr(self, ch).data: selected.append(ch) return selected def validate(self): if not super(F, self).validate(): return False if not self.validate_mock_chroots_not_empty(): self.errors["chroots"] = [ "At least one chroot must be selected" ] return False if self.persistent.data and self.delete_after_days.data: self.delete_after_days.errors.append( "'delete after' can not be combined with persistent") return False return True def validate_mock_chroots_not_empty(self): have_any = False for c in self.chroots_list: if getattr(self, c).data: have_any = True return have_any F.chroots_list = MockChrootsLogic.active_names() F.chroots_list.sort() # sets of chroots according to how we should print them in columns F.chroots_sets = {} for ch in F.chroots_list: checkbox_default = False if mock_chroots and ch in [x.name for x in mock_chroots]: checkbox_default = True setattr( F, ch, wtforms.BooleanField(ch, default=checkbox_default, false_values=FALSE_VALUES)) if ch[0] in F.chroots_sets: F.chroots_sets[ch[0]].append(ch) else: F.chroots_sets[ch[0]] = [ch] return F
def final_prunerepo_done(): chroots_pruned = flask.request.get_json() return flask.jsonify(MockChrootsLogic.prunerepo_finished(chroots_pruned))
def chroots_prunerepo_status(): return flask.jsonify(MockChrootsLogic.chroots_prunerepo_status())